Search in sources :

Example 21 with SkipForDialect

use of org.hibernate.testing.SkipForDialect in project hibernate-orm by hibernate.

the class AuctionTest method testLazy.

@Test
@SuppressWarnings({ "unchecked" })
@SkipForDialect(value = { PostgreSQL81Dialect.class, PostgreSQLDialect.class }, comment = "doesn't like boolean=1")
public void testLazy() {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Auction a = new Auction();
    a.setDescription("an auction for something");
    a.setEnd(new Date());
    Bid b = new Bid();
    b.setAmount(new BigDecimal(123.34).setScale(19, BigDecimal.ROUND_DOWN));
    b.setSuccessful(true);
    b.setDatetime(new Date());
    b.setItem(a);
    a.getBids().add(b);
    a.setSuccessfulBid(b);
    s.persist(b);
    t.commit();
    s.close();
    Long aid = a.getId();
    Long bid = b.getId();
    s = openSession();
    t = s.beginTransaction();
    b = (Bid) s.load(Bid.class, bid);
    assertFalse(Hibernate.isInitialized(b));
    a = (Auction) s.get(Auction.class, aid);
    assertFalse(Hibernate.isInitialized(a.getBids()));
    assertTrue(Hibernate.isInitialized(a.getSuccessfulBid()));
    assertSame(a.getBids().iterator().next(), b);
    assertSame(b, a.getSuccessfulBid());
    assertTrue(Hibernate.isInitialized(b));
    assertTrue(b.isSuccessful());
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    b = (Bid) s.load(Bid.class, bid);
    assertFalse(Hibernate.isInitialized(b));
    a = (Auction) s.createQuery("from Auction a left join fetch a.bids").uniqueResult();
    assertTrue(Hibernate.isInitialized(b));
    assertTrue(Hibernate.isInitialized(a.getBids()));
    assertSame(b, a.getSuccessfulBid());
    assertSame(a.getBids().iterator().next(), b);
    assertTrue(b.isSuccessful());
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    b = (Bid) s.load(Bid.class, bid);
    a = (Auction) s.load(Auction.class, aid);
    assertFalse(Hibernate.isInitialized(b));
    assertFalse(Hibernate.isInitialized(a));
    s.createQuery("from Auction a left join fetch a.successfulBid").list();
    assertTrue(Hibernate.isInitialized(b));
    assertTrue(Hibernate.isInitialized(a));
    assertSame(b, a.getSuccessfulBid());
    assertFalse(Hibernate.isInitialized(a.getBids()));
    assertSame(a.getBids().iterator().next(), b);
    assertTrue(b.isSuccessful());
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    b = (Bid) s.load(Bid.class, bid);
    a = (Auction) s.load(Auction.class, aid);
    assertFalse(Hibernate.isInitialized(b));
    assertFalse(Hibernate.isInitialized(a));
    assertSame(s.get(Bid.class, bid), b);
    assertTrue(Hibernate.isInitialized(b));
    assertSame(s.get(Auction.class, aid), a);
    assertTrue(Hibernate.isInitialized(a));
    assertSame(b, a.getSuccessfulBid());
    assertFalse(Hibernate.isInitialized(a.getBids()));
    assertSame(a.getBids().iterator().next(), b);
    assertTrue(b.isSuccessful());
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Date(java.util.Date) BigDecimal(java.math.BigDecimal) Session(org.hibernate.Session) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Example 22 with SkipForDialect

use of org.hibernate.testing.SkipForDialect in project hibernate-orm by hibernate.

the class Ejb3XmlTest method testEjb3Xml.

@Test
@SkipForDialect(value = { PostgreSQL81Dialect.class, PostgreSQLDialect.class }, comment = "postgresql jdbc driver does not implement the setQueryTimeout method")
@SkipForDialect(value = TeradataDialect.class, jiraKey = "HHH-8190", comment = "uses Teradata reserved word - year")
public void testEjb3Xml() throws Exception {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    CarModel model = new CarModel();
    model.setYear(new Date());
    Manufacturer manufacturer = new Manufacturer();
    //s.persist( manufacturer );
    model.setManufacturer(manufacturer);
    manufacturer.getModels().add(model);
    s.persist(model);
    s.flush();
    s.clear();
    model.setYear(new Date());
    manufacturer = (Manufacturer) s.get(Manufacturer.class, manufacturer.getId());
    @SuppressWarnings("unchecked") List<Model> cars = s.getNamedQuery("allModelsPerManufacturer").setParameter("manufacturer", manufacturer).list();
    assertEquals(1, cars.size());
    for (Model car : cars) {
        assertNotNull(car.getManufacturer());
        s.delete(manufacturer);
        s.delete(car);
    }
    tx.rollback();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Date(java.util.Date) Session(org.hibernate.Session) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Example 23 with SkipForDialect

use of org.hibernate.testing.SkipForDialect in project hibernate-orm by hibernate.

the class CompositeIdTest method testDistinctCountOfEntityWithCompositeId.

@Test
@SkipForDialect(value = Oracle8iDialect.class, comment = "Cannot count distinct over multiple columns in Oracle")
public void testDistinctCountOfEntityWithCompositeId() {
    // today we do not account for Dialects supportsTupleDistinctCounts() is false.  though really the only
    // "option" there is to throw an error.
    final HQLQueryPlan plan = sessionFactory().getQueryPlanCache().getHQLQueryPlan("select count(distinct o) from Order o", false, Collections.EMPTY_MAP);
    assertEquals(1, plan.getTranslators().length);
    final QueryTranslator translator = plan.getTranslators()[0];
    final String generatedSql = translator.getSQLString();
    System.out.println("Generated SQL : " + generatedSql);
    final int countExpressionListStart = generatedSql.indexOf("count(");
    final int countExpressionListEnd = generatedSql.indexOf(")", countExpressionListStart);
    final String countExpressionFragment = generatedSql.substring(countExpressionListStart + 6, countExpressionListEnd + 1);
    assertTrue(countExpressionFragment.startsWith("distinct"));
    assertTrue(countExpressionFragment.contains(","));
    Session s = openSession();
    s.beginTransaction();
    Customer c = new Customer();
    c.setCustomerId("1");
    c.setAddress("123 somewhere");
    c.setName("Brett");
    Order o1 = new Order(c);
    o1.setOrderDate(Calendar.getInstance());
    Order o2 = new Order(c);
    o2.setOrderDate(Calendar.getInstance());
    s.persist(c);
    s.persist(o1);
    s.persist(o2);
    s.getTransaction().commit();
    s.clear();
    s.beginTransaction();
    try {
        long count = (Long) s.createQuery("select count(distinct o) FROM Order o").uniqueResult();
        if (!getDialect().supportsTupleDistinctCounts()) {
            fail("expected SQLGrammarException");
        }
        assertEquals(2l, count);
    } catch (SQLGrammarException e) {
        if (getDialect().supportsTupleDistinctCounts()) {
            throw e;
        }
    }
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.beginTransaction();
    s.createQuery("delete from Order").executeUpdate();
    s.createQuery("delete from Customer").executeUpdate();
    s.getTransaction().commit();
    s.close();
}
Also used : HQLQueryPlan(org.hibernate.engine.query.spi.HQLQueryPlan) SQLGrammarException(org.hibernate.exception.SQLGrammarException) QueryTranslator(org.hibernate.hql.spi.QueryTranslator) Session(org.hibernate.Session) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Example 24 with SkipForDialect

use of org.hibernate.testing.SkipForDialect in project hibernate-orm by hibernate.

the class CriteriaQueryTest method testSubselect.

@Test
@SkipForDialect(value = SybaseASE15Dialect.class, strictMatching = true, jiraKey = "HHH-3032", comment = "I was told this is fixed in Sybase ASE 15.7")
public void testSubselect() {
    Session session = openSession();
    Transaction t = session.beginTransaction();
    Course course = new Course();
    course.setCourseCode("HIB");
    course.setDescription("Hibernate Training");
    session.persist(course);
    Student gavin = new Student();
    gavin.setName("Gavin King");
    gavin.setStudentNumber(232);
    session.persist(gavin);
    Enrolment enrolment2 = new Enrolment();
    enrolment2.setCourse(course);
    enrolment2.setCourseCode(course.getCourseCode());
    enrolment2.setSemester((short) 3);
    enrolment2.setYear((short) 1998);
    enrolment2.setStudent(gavin);
    enrolment2.setStudentNumber(gavin.getStudentNumber());
    gavin.getEnrolments().add(enrolment2);
    session.persist(enrolment2);
    DetachedCriteria dc = DetachedCriteria.forClass(Student.class).add(Property.forName("studentNumber").eq(new Long(232))).setProjection(Property.forName("name"));
    session.createCriteria(Student.class).add(Subqueries.propertyEqAll("name", dc)).list();
    session.createCriteria(Student.class).add(Subqueries.exists(dc)).list();
    session.createCriteria(Student.class).add(Property.forName("name").eqAll(dc)).list();
    session.createCriteria(Student.class).add(Subqueries.in("Gavin King", dc)).list();
    DetachedCriteria dc2 = DetachedCriteria.forClass(Student.class, "st").add(Property.forName("st.studentNumber").eqProperty("e.studentNumber")).setProjection(Property.forName("name"));
    session.createCriteria(Enrolment.class, "e").add(Subqueries.eq("Gavin King", dc2)).list();
    DetachedCriteria dc3 = DetachedCriteria.forClass(Student.class, "st").createCriteria("enrolments").createCriteria("course").add(Property.forName("description").eq("Hibernate Training")).setProjection(Property.forName("st.name"));
    session.createCriteria(Enrolment.class, "e").add(Subqueries.eq("Gavin King", dc3)).list();
    DetachedCriteria dc4 = DetachedCriteria.forClass(Student.class, "st").setProjection(Property.forName("name").as("stname"));
    dc4.getExecutableCriteria(session).list();
    // SQL Server and Oracle doesn't normally support ORDER BY in subqueries...
    if (!(getDialect() instanceof SQLServerDialect) && !(getDialect() instanceof Oracle8iDialect)) {
        dc4.getExecutableCriteria(session).addOrder(Order.asc("stname")).list();
    }
    session.createCriteria(Enrolment.class, "e").add(Subqueries.eq("Gavin King", dc4)).list();
    session.delete(enrolment2);
    session.delete(gavin);
    session.delete(course);
    t.commit();
    session.close();
}
Also used : SQLServerDialect(org.hibernate.dialect.SQLServerDialect) Transaction(org.hibernate.Transaction) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) Session(org.hibernate.Session) Oracle8iDialect(org.hibernate.dialect.Oracle8iDialect) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Example 25 with SkipForDialect

use of org.hibernate.testing.SkipForDialect in project hibernate-orm by hibernate.

the class LongInElementsTest method testLongInElementsByHQL.

@Test
@TestForIssue(jiraKey = "HHH-2166")
@SkipForDialect(value = { SQLServerDialect.class, Oracle8iDialect.class, TeradataDialect.class, SybaseDialect.class }, comment = "this test fails on oracle and ms sql server, for more info, see HHH-1123")
public void testLongInElementsByHQL() {
    Session session = openSession();
    Transaction t = session.beginTransaction();
    StateProvince beijing = new StateProvince();
    beijing.setIsoCode("100089");
    beijing.setName("beijing");
    session.persist(beijing);
    session.flush();
    session.clear();
    Query query = session.createQuery("from org.hibernate.test.hql.StateProvince sp where sp.id in ( :idList )");
    query.setParameterList("idList", createLotsOfElements());
    List list = query.list();
    session.flush();
    session.clear();
    assertEquals(1, list.size());
    session.delete(beijing);
    t.commit();
    session.close();
}
Also used : Transaction(org.hibernate.Transaction) Query(org.hibernate.Query) StateProvince(org.hibernate.test.hql.StateProvince) ArrayList(java.util.ArrayList) List(java.util.List) Session(org.hibernate.Session) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

SkipForDialect (org.hibernate.testing.SkipForDialect)87 Test (org.junit.Test)86 Session (org.hibernate.Session)66 Transaction (org.hibernate.Transaction)36 List (java.util.List)26 ArrayList (java.util.ArrayList)20 TestForIssue (org.hibernate.testing.TestForIssue)16 EntityManager (javax.persistence.EntityManager)15 Query (javax.persistence.Query)11 Item (org.hibernate.jpa.test.Item)10 Query (org.hibernate.Query)7 Parameter (javax.persistence.Parameter)6 BigDecimal (java.math.BigDecimal)5 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)4 SQLQuery (org.hibernate.SQLQuery)4 ScrollableResults (org.hibernate.ScrollableResults)4 Connection (java.sql.Connection)3 SQLException (java.sql.SQLException)3 Date (java.util.Date)3 DetachedCriteria (org.hibernate.criterion.DetachedCriteria)3