Search in sources :

Example 11 with SkipForDialect

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

the class QueryTest method testNullNamedParameter.

@Test
@SkipForDialect(value = SybaseDialect.class, comment = "Null == null on Sybase")
public void testNullNamedParameter() throws Exception {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    try {
        Item item = new Item("Mouse", "Micro$oft mouse");
        em.persist(item);
        Query q = em.createQuery("from Item i where i.intVal=:iVal");
        q.setParameter("iVal", null);
        List results = q.getResultList();
        // null != null
        assertEquals(0, results.size());
        q = em.createQuery("from Item i where i.intVal is null and :iVal is null");
        q.setParameter("iVal", null);
        results = q.getResultList();
        assertEquals(1, results.size());
        q = em.createQuery("from Item i where i.intVal is null or i.intVal = :iVal");
        q.setParameter("iVal", 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 12 with SkipForDialect

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

the class QueryTest method testNullNamedParameterParameter.

@Test
@SkipForDialect(value = SybaseDialect.class, comment = "Null == null on Sybase")
public void testNullNamedParameterParameter() throws Exception {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    try {
        Item item = new Item("Mouse", "Micro$oft mouse");
        em.persist(item);
        Query q = em.createQuery("from Item i where i.intVal=:iVal");
        Parameter p = new Parameter() {

            @Override
            public String getName() {
                return "iVal";
            }

            @Override
            public Integer getPosition() {
                return null;
            }

            @Override
            public Class getParameterType() {
                return Integer.class;
            }
        };
        q.setParameter(p, null);
        List results = q.getResultList();
        // null != null
        assertEquals(0, results.size());
        q = em.createQuery("from Item i where i.intVal is null and :iVal is null");
        q.setParameter(p, null);
        results = q.getResultList();
        assertEquals(1, results.size());
        q = em.createQuery("from Item i where i.intVal is null or i.intVal = :iVal");
        q.setParameter(p, 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) Parameter(javax.persistence.Parameter) ArrayList(java.util.ArrayList) List(java.util.List) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Example 13 with SkipForDialect

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

the class QueryTest method testNativeQueryNullPositionalParameterParameter.

@Test
@TestForIssue(jiraKey = "HHH-10161")
@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 = Oracle8iDialect.class, comment = "ORA-00932: inconsistent datatypes: expected NUMBER got BINARY")
@SkipForDialect(value = SybaseDialect.class, comment = "Null == null on Sybase")
public void testNativeQueryNullPositionalParameterParameter() 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=?");
        Parameter p = new Parameter() {

            @Override
            public String getName() {
                return null;
            }

            @Override
            public Integer getPosition() {
                return 1;
            }

            @Override
            public Class getParameterType() {
                return Integer.class;
            }
        };
        q.setParameter(p, null);
        Parameter pGotten = q.getParameter(p.getPosition());
        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(p, 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(p, 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) Parameter(javax.persistence.Parameter) ArrayList(java.util.ArrayList) List(java.util.List) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 14 with SkipForDialect

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

the class QueryTest method testNativeQueryNullNamedParameterParameter.

@Test
@TestForIssue(jiraKey = "HHH-10161")
@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 = Oracle8iDialect.class, comment = "ORA-00932: inconsistent datatypes: expected NUMBER got BINARY")
@SkipForDialect(value = SybaseDialect.class, comment = "Null == null on Sybase")
public void testNativeQueryNullNamedParameterParameter() 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=:iVal");
        Parameter p = new Parameter() {

            @Override
            public String getName() {
                return "iVal";
            }

            @Override
            public Integer getPosition() {
                return null;
            }

            @Override
            public Class getParameterType() {
                return Integer.class;
            }
        };
        q.setParameter(p, null);
        Parameter pGotten = q.getParameter(p.getName());
        List results = q.getResultList();
        assertEquals(0, results.size());
        q = em.createNativeQuery("select * from Item i where (i.intVal is null) and (:iVal is null)");
        q.setParameter(p, null);
        results = q.getResultList();
        assertEquals(1, results.size());
        q = em.createNativeQuery("select * from Item i where i.intVal is null or i.intVal = :iVal");
        q.setParameter(p, 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) Parameter(javax.persistence.Parameter) ArrayList(java.util.ArrayList) List(java.util.List) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 15 with SkipForDialect

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

the class EntityTest method testTemporalType.

@Test
@SkipForDialect(value = Oracle10gDialect.class, comment = "oracle12c returns time in getDate.  For now, skip.")
public void testTemporalType() throws Exception {
    Flight airFrance = doInHibernate(this::sessionFactory, session -> {
        Flight _airFrance = new Flight();
        _airFrance.setId(Long.valueOf(747));
        _airFrance.setName("Paris-Amsterdam");
        _airFrance.setDuration(Long.valueOf(10));
        _airFrance.setDepartureDate(Date.from(LocalDate.of(2005, 06, 21).atStartOfDay(ZoneId.systemDefault()).toInstant()));
        _airFrance.setAlternativeDepartureDate(new GregorianCalendar(2006, 02, 03, 10, 00));
        _airFrance.getAlternativeDepartureDate().setTimeZone(TimeZone.getTimeZone("GMT"));
        _airFrance.setBuyDate(new java.sql.Timestamp(122367443));
        _airFrance.setFactor(25);
        session.persist(_airFrance);
        return _airFrance;
    });
    doInHibernate(this::sessionFactory, session -> {
        Query q = session.createQuery("from Flight f where f.departureDate = :departureDate");
        q.setParameter("departureDate", airFrance.getDepartureDate(), StandardBasicTypes.DATE);
        Flight copyAirFrance = (Flight) q.uniqueResult();
        assertNotNull(copyAirFrance);
        assertEquals(Date.from(LocalDate.of(2005, 06, 21).atStartOfDay(ZoneId.systemDefault()).toInstant()), copyAirFrance.getDepartureDate());
        assertEquals(df.format(airFrance.getBuyDate()), df.format(copyAirFrance.getBuyDate()));
        session.delete(copyAirFrance);
    });
}
Also used : Query(org.hibernate.query.Query) GregorianCalendar(java.util.GregorianCalendar) 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