Search in sources :

Example 81 with SkipForDialect

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

the class QueryTest method testNativeQueryNullPositionalParameter.

@Test
@SkipForDialect(value = Oracle8iDialect.class, jiraKey = "HHH-10161", comment = "Cannot convert untyped null (assumed to be BINARY type) to NUMBER")
@SkipForDialect(value = PostgreSQL9Dialect.class, jiraKey = "HHH-10312", comment = "Cannot convert untyped null (assumed to be bytea type) to bigint")
@SkipForDialect(value = PostgresPlusDialect.class, jiraKey = "HHH-10312", comment = "Cannot convert untyped null (assumed to be bytea type) to bigint")
@SkipForDialect(value = SybaseDialect.class, comment = "Null == null on Sybase")
public void testNativeQueryNullPositionalParameter() throws Exception {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    try {
        Item item = new Item("Mouse", "Micro$oft mouse");
        em.persist(item);
        // native queries don't seem to flush by default ?!?
        em.flush();
        Query q = em.createNativeQuery("select * from Item i where i.intVal=?");
        q.setParameter(1, null);
        List results = q.getResultList();
        // null != null
        assertEquals(0, results.size());
        q = em.createNativeQuery("select * from Item i where i.intVal is null and ? is null");
        q.setParameter(1, null);
        results = q.getResultList();
        assertEquals(1, results.size());
        q = em.createNativeQuery("select * from Item i where i.intVal is null or i.intVal = ?");
        q.setParameter(1, null);
        results = q.getResultList();
        assertEquals(1, results.size());
    } finally {
        if (em.getTransaction() != null && em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }
}
Also used : Item(org.hibernate.jpa.test.Item) EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) ArrayList(java.util.ArrayList) List(java.util.List) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Example 82 with SkipForDialect

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

the class CriteriaQueryTest method testSubselectWithComponent.

@Test
@SkipForDialect(value = SybaseASE15Dialect.class, strictMatching = true, jiraKey = "HHH-3032", comment = "I was told this is fixed in Sybase ASE 15.7")
public void testSubselectWithComponent() {
    Session session = openSession();
    Transaction t = session.beginTransaction();
    Course course = new Course();
    course.setCourseCode("HIB");
    course.setDescription("Hibernate Training");
    session.persist(course);
    CityState odessaWa = new CityState("Odessa", "WA");
    Student gavin = new Student();
    gavin.setName("Gavin King");
    gavin.setStudentNumber(232);
    gavin.setCityState(odessaWa);
    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("cityState").eq(odessaWa)).setProjection(Property.forName("cityState"));
    session.createCriteria(Student.class).add(Subqueries.exists(dc)).list();
    t.commit();
    session.close();
    session = openSession();
    t = session.beginTransaction();
    try {
        session.createCriteria(Student.class).add(Subqueries.propertyEqAll("cityState", dc)).list();
        fail("should have failed because cannot compare subquery results with multiple columns");
    } catch (QueryException ex) {
    // expected
    } finally {
        t.rollback();
        session.close();
    }
    session = openSession();
    t = session.beginTransaction();
    try {
        session.createCriteria(Student.class).add(Property.forName("cityState").eqAll(dc)).list();
        fail("should have failed because cannot compare subquery results with multiple columns");
    } catch (QueryException ex) {
    // expected
    } finally {
        t.rollback();
        session.close();
    }
    session = openSession();
    t = session.beginTransaction();
    try {
        session.createCriteria(Student.class).add(Subqueries.in(odessaWa, dc)).list();
        fail("should have failed because cannot compare subquery results with multiple columns");
    } catch (JDBCException ex) {
    // expected
    } finally {
        t.rollback();
        session.close();
    }
    session = openSession();
    t = session.beginTransaction();
    DetachedCriteria dc2 = DetachedCriteria.forClass(Student.class, "st1").add(Property.forName("st1.cityState").eqProperty("st2.cityState")).setProjection(Property.forName("cityState"));
    try {
        session.createCriteria(Student.class, "st2").add(Subqueries.eq(odessaWa, dc2)).list();
        fail("should have failed because cannot compare subquery results with multiple columns");
    } catch (JDBCException ex) {
    // expected
    } finally {
        t.rollback();
        session.close();
    }
    session = openSession();
    t = session.beginTransaction();
    DetachedCriteria dc3 = DetachedCriteria.forClass(Student.class, "st").createCriteria("enrolments").createCriteria("course").add(Property.forName("description").eq("Hibernate Training")).setProjection(Property.forName("st.cityState"));
    try {
        session.createCriteria(Enrolment.class, "e").add(Subqueries.eq(odessaWa, dc3)).list();
        fail("should have failed because cannot compare subquery results with multiple columns");
    } catch (JDBCException ex) {
    // expected
    } finally {
        t.rollback();
        session.close();
    }
    session = openSession();
    t = session.beginTransaction();
    session.delete(enrolment2);
    session.delete(gavin);
    session.delete(course);
    t.commit();
    session.close();
}
Also used : QueryException(org.hibernate.QueryException) Transaction(org.hibernate.Transaction) JDBCException(org.hibernate.JDBCException) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) Session(org.hibernate.Session) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Example 83 with SkipForDialect

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

the class LongInElementsTest method testLongInElementsByCriteria.

@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 testLongInElementsByCriteria() {
    Session session = openSession();
    Transaction t = session.beginTransaction();
    StateProvince beijing = new StateProvince();
    beijing.setIsoCode("100089");
    beijing.setName("beijing");
    session.persist(beijing);
    session.flush();
    session.clear();
    Criteria criteria = session.createCriteria(StateProvince.class);
    criteria.add(Restrictions.in("id", createLotsOfElements()));
    List list = criteria.list();
    session.flush();
    session.clear();
    assertEquals(1, list.size());
    session.delete(beijing);
    t.commit();
    session.close();
}
Also used : Transaction(org.hibernate.Transaction) StateProvince(org.hibernate.test.hql.StateProvince) ArrayList(java.util.ArrayList) List(java.util.List) Criteria(org.hibernate.Criteria) Session(org.hibernate.Session) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 84 with SkipForDialect

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

the class CompositeUserTypeTest method testCustomColumnReadAndWrite.

@Test
@SkipForDialect(value = { SybaseASE15Dialect.class }, jiraKey = "HHH-6788")
@SkipForDialect(value = { DB2Dialect.class }, jiraKey = "HHH-6867")
public void testCustomColumnReadAndWrite() {
    Session s = openSession();
    org.hibernate.Transaction t = s.beginTransaction();
    final BigDecimal AMOUNT = new BigDecimal(73000000d);
    final BigDecimal AMOUNT_MILLIONS = AMOUNT.divide(new BigDecimal(1000000d));
    MutualFund f = new MutualFund();
    f.setHoldings(new MonetoryAmount(AMOUNT, Currency.getInstance("USD")));
    s.persist(f);
    s.flush();
    // Test value conversion during insert
    BigDecimal amountViaSql = (BigDecimal) s.createSQLQuery("select amount_millions from MutualFund").uniqueResult();
    assertEquals(AMOUNT_MILLIONS.doubleValue(), amountViaSql.doubleValue(), 0.01d);
    // Test projection
    BigDecimal amountViaHql = (BigDecimal) s.createQuery("select f.holdings.amount from MutualFund f").uniqueResult();
    assertEquals(AMOUNT.doubleValue(), amountViaHql.doubleValue(), 0.01d);
    // Test restriction and entity load via criteria
    BigDecimal one = new BigDecimal(1);
    f = (MutualFund) s.createCriteria(MutualFund.class).add(Restrictions.between("holdings.amount", AMOUNT.subtract(one), AMOUNT.add(one))).uniqueResult();
    assertEquals(AMOUNT.doubleValue(), f.getHoldings().getAmount().doubleValue(), 0.01d);
    // Test predicate and entity load via HQL
    f = (MutualFund) s.createQuery("from MutualFund f where f.holdings.amount between ? and ?").setBigDecimal(0, AMOUNT.subtract(one)).setBigDecimal(1, AMOUNT.add(one)).uniqueResult();
    assertEquals(AMOUNT.doubleValue(), f.getHoldings().getAmount().doubleValue(), 0.01d);
    s.delete(f);
    t.commit();
    s.close();
}
Also used : BigDecimal(java.math.BigDecimal) Session(org.hibernate.Session) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Example 85 with SkipForDialect

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

the class ReadOnlyCriteriaQueryTest method testSubselect.

@Test
@SkipForDialect(value = SybaseASE15Dialect.class, jiraKey = "HHH-3032", strictMatching = true)
public void testSubselect() {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Course course = new Course();
    course.setCourseCode("HIB");
    course.setDescription("Hibernate Training");
    s.persist(course);
    Course coursePreferred = new Course();
    coursePreferred.setCourseCode("JBOSS");
    coursePreferred.setDescription("JBoss");
    s.persist(coursePreferred);
    Student gavin = new Student();
    gavin.setName("Gavin King");
    gavin.setStudentNumber(232);
    gavin.setPreferredCourse(coursePreferred);
    s.persist(gavin);
    Enrolment enrolment = new Enrolment();
    enrolment.setCourse(course);
    enrolment.setCourseCode(course.getCourseCode());
    enrolment.setSemester((short) 3);
    enrolment.setYear((short) 1998);
    enrolment.setStudent(gavin);
    enrolment.setStudentNumber(gavin.getStudentNumber());
    gavin.getEnrolments().add(enrolment);
    s.persist(enrolment);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    DetachedCriteria dc = DetachedCriteria.forClass(Student.class).add(Property.forName("studentNumber").eq(new Long(232))).setProjection(Property.forName("name"));
    gavin = (Student) s.createCriteria(Student.class).add(Subqueries.exists(dc)).setReadOnly(true).uniqueResult();
    assertFalse(s.isDefaultReadOnly());
    assertTrue(s.isReadOnly(gavin));
    assertFalse(Hibernate.isInitialized(gavin.getPreferredCourse()));
    checkProxyReadOnly(s, gavin.getPreferredCourse(), true);
    assertFalse(Hibernate.isInitialized(gavin.getPreferredCourse()));
    Hibernate.initialize(gavin.getPreferredCourse());
    assertTrue(Hibernate.isInitialized(gavin.getPreferredCourse()));
    checkProxyReadOnly(s, gavin.getPreferredCourse(), true);
    assertFalse(Hibernate.isInitialized(gavin.getEnrolments()));
    Hibernate.initialize(gavin.getEnrolments());
    assertTrue(Hibernate.isInitialized(gavin.getEnrolments()));
    assertEquals(1, gavin.getEnrolments().size());
    enrolment = (Enrolment) gavin.getEnrolments().iterator().next();
    assertFalse(s.isReadOnly(enrolment));
    assertFalse(Hibernate.isInitialized(enrolment.getCourse()));
    checkProxyReadOnly(s, enrolment.getCourse(), false);
    Hibernate.initialize(enrolment.getCourse());
    checkProxyReadOnly(s, enrolment.getCourse(), false);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    DetachedCriteria dc2 = DetachedCriteria.forClass(Student.class, "st").add(Property.forName("st.studentNumber").eqProperty("e.studentNumber")).setProjection(Property.forName("name"));
    enrolment = (Enrolment) s.createCriteria(Enrolment.class, "e").add(Subqueries.eq("Gavin King", dc2)).setReadOnly(true).uniqueResult();
    assertTrue(s.isReadOnly(enrolment));
    assertFalse(Hibernate.isInitialized(enrolment.getCourse()));
    checkProxyReadOnly(s, enrolment.getCourse(), true);
    Hibernate.initialize(enrolment.getCourse());
    assertTrue(Hibernate.isInitialized(enrolment.getCourse()));
    checkProxyReadOnly(s, enrolment.getCourse(), true);
    assertFalse(Hibernate.isInitialized(enrolment.getStudent()));
    checkProxyReadOnly(s, enrolment.getStudent(), true);
    Hibernate.initialize(enrolment.getStudent());
    assertTrue(Hibernate.isInitialized(enrolment.getStudent()));
    checkProxyReadOnly(s, enrolment.getStudent(), true);
    assertFalse(Hibernate.isInitialized(enrolment.getStudent().getPreferredCourse()));
    checkProxyReadOnly(s, enrolment.getStudent().getPreferredCourse(), false);
    Hibernate.initialize(enrolment.getStudent().getPreferredCourse());
    assertTrue(Hibernate.isInitialized(enrolment.getStudent().getPreferredCourse()));
    checkProxyReadOnly(s, enrolment.getStudent().getPreferredCourse(), false);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    DetachedCriteria dc3 = DetachedCriteria.forClass(Student.class, "st").createCriteria("enrolments").createCriteria("course").add(Property.forName("description").eq("Hibernate Training")).setProjection(Property.forName("st.name"));
    enrolment = (Enrolment) s.createCriteria(Enrolment.class, "e").add(Subqueries.eq("Gavin King", dc3)).setReadOnly(true).uniqueResult();
    assertTrue(s.isReadOnly(enrolment));
    assertFalse(Hibernate.isInitialized(enrolment.getCourse()));
    checkProxyReadOnly(s, enrolment.getCourse(), true);
    Hibernate.initialize(enrolment.getCourse());
    assertTrue(Hibernate.isInitialized(enrolment.getCourse()));
    checkProxyReadOnly(s, enrolment.getCourse(), true);
    assertFalse(Hibernate.isInitialized(enrolment.getStudent()));
    checkProxyReadOnly(s, enrolment.getStudent(), true);
    Hibernate.initialize(enrolment.getStudent());
    assertTrue(Hibernate.isInitialized(enrolment.getStudent()));
    checkProxyReadOnly(s, enrolment.getStudent(), true);
    assertFalse(Hibernate.isInitialized(enrolment.getStudent().getPreferredCourse()));
    checkProxyReadOnly(s, enrolment.getStudent().getPreferredCourse(), false);
    Hibernate.initialize(enrolment.getStudent().getPreferredCourse());
    assertTrue(Hibernate.isInitialized(enrolment.getStudent().getPreferredCourse()));
    checkProxyReadOnly(s, enrolment.getStudent().getPreferredCourse(), false);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    s.delete(gavin.getPreferredCourse());
    s.delete(gavin);
    enrolment = (Enrolment) gavin.getEnrolments().iterator().next();
    s.delete(enrolment.getCourse());
    s.delete(enrolment);
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) Session(org.hibernate.Session) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

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