Search in sources :

Example 66 with SkipForDialect

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

the class ASTParserLoadingTest method testAggregation.

@Test
@SkipForDialect(value = SybaseASE15Dialect.class, jiraKey = "HHH-6424")
public void testAggregation() {
    Session s = openSession();
    s.beginTransaction();
    Human h = new Human();
    h.setBodyWeight((float) 74.0);
    h.setHeightInches(120.5);
    h.setDescription("Me");
    h.setName(new Name("Gavin", 'A', "King"));
    h.setNickName("Oney");
    s.persist(h);
    Double sum = (Double) s.createQuery("select sum(h.bodyWeight) from Human h").uniqueResult();
    // uses custom read and write for column
    Double avg = (Double) s.createQuery("select avg(h.heightInches) from Human h").uniqueResult();
    assertEquals(sum.floatValue(), 74.0, 0.01);
    assertEquals(avg.doubleValue(), 120.5, 0.01);
    Long id = (Long) s.createQuery("select max(a.id) from Animal a").uniqueResult();
    assertNotNull(id);
    s.delete(h);
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.beginTransaction();
    h = new Human();
    h.setFloatValue(2.5F);
    h.setIntValue(1);
    s.persist(h);
    Human h2 = new Human();
    h2.setFloatValue(2.5F);
    h2.setIntValue(2);
    s.persist(h2);
    Object[] results = (Object[]) s.createQuery("select sum(h.floatValue), avg(h.floatValue), sum(h.intValue), avg(h.intValue) from Human h").uniqueResult();
    // spec says sum() on a float or double value should result in double
    assertTrue(Double.class.isInstance(results[0]));
    assertEquals(5D, results[0]);
    // avg() should return a double
    assertTrue(Double.class.isInstance(results[1]));
    assertEquals(2.5D, results[1]);
    // spec says sum() on short, int or long should result in long
    assertTrue(Long.class.isInstance(results[2]));
    assertEquals(3L, results[2]);
    // avg() should return a double
    assertTrue(Double.class.isInstance(results[3]));
    if (getDialect() instanceof SQLServer2008Dialect)
        assertEquals(1.0D, results[3]);
    else
        assertEquals(1.5D, results[3]);
    s.delete(h);
    s.delete(h2);
    s.getTransaction().commit();
    s.close();
}
Also used : SQLServer2008Dialect(org.hibernate.dialect.SQLServer2008Dialect) Session(org.hibernate.Session) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Example 67 with SkipForDialect

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

the class ASTParserLoadingTest method testImplicitPolymorphism.

@Test
@SkipForDialect(value = IngresDialect.class, jiraKey = "HHH-4976", comment = "Ingres 9.3 does not support sub-selects in the select list")
public void testImplicitPolymorphism() {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Product product = new Product();
    product.setDescription("My Product");
    product.setNumberAvailable(10);
    product.setPrice(new BigDecimal(123));
    product.setProductId("4321");
    s.save(product);
    List list = s.createQuery("from java.lang.Comparable").list();
    assertEquals(list.size(), 0);
    list = s.createQuery("from java.lang.Object").list();
    assertEquals(list.size(), 1);
    s.delete(product);
    list = s.createQuery("from java.lang.Object").list();
    assertEquals(list.size(), 0);
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Product(org.hibernate.test.cid.Product) List(java.util.List) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) Session(org.hibernate.Session) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Example 68 with SkipForDialect

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

the class OneToManyTest method testOneToManyLinkTable.

@SuppressWarnings({ "unchecked", "UnusedAssignment" })
@Test
@SkipForDialect(value = CUBRIDDialect.class, comment = "As of verion 8.4.1 CUBRID doesn't support temporary tables. This test fails with" + "HibernateException: cannot doAfterTransactionCompletion multi-table deletes using dialect not supporting temp tables")
public void testOneToManyLinkTable() {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Child c = new Child();
    c.setName("Child One");
    Parent p = new Parent();
    p.setName("Parent");
    p.getChildren().add(c);
    c.setParent(p);
    s.save(p);
    s.flush();
    p.getChildren().remove(c);
    c.setParent(null);
    s.flush();
    p.getChildren().add(c);
    c.setParent(p);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    c.setParent(null);
    s.update(c);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    c.setParent(p);
    s.update(c);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    c = (Child) s.createQuery("from Child").uniqueResult();
    s.createQuery("from Child c left join fetch c.parent").list();
    s.createQuery("from Child c inner join fetch c.parent").list();
    s.clear();
    p = (Parent) s.createQuery("from Parent p left join fetch p.children").uniqueResult();
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    s.createQuery("delete from Child").executeUpdate();
    s.createQuery("delete from Parent").executeUpdate();
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Example 69 with SkipForDialect

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

the class CriteriaLiteralInSelectExpressionTest method testStringLiteral2.

@Test
@TestForIssue(jiraKey = "HHH-9021")
@SkipForDialect(value = { Oracle8iDialect.class, DB2Dialect.class, SQLServerDialect.class, SybaseDialect.class })
public void testStringLiteral2() {
    final EntityManager entityManager = getOrCreateEntityManager();
    try {
        final CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        final CriteriaQuery<Tuple> criteriaQuery = builder.createQuery(Tuple.class);
        criteriaQuery.from(MyEntity.class);
        criteriaQuery.multiselect(builder.equal(builder.literal(1), builder.literal(2)));
        final TypedQuery<Tuple> typedQuery = entityManager.createQuery(criteriaQuery);
        final List<Tuple> results = typedQuery.getResultList();
        assertThat(results.size(), is(1));
        assertThat(results.get(0).getElements().size(), is(1));
        assertThat(results.get(0).get(0), is(false));
    } finally {
        entityManager.close();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Tuple(javax.persistence.Tuple) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 70 with SkipForDialect

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

the class QueryAndSQLTest method testCache.

@Test
@SkipForDialect(value = { PostgreSQL81Dialect.class, PostgreSQLDialect.class }, comment = "postgresql jdbc driver does not implement the setQueryTimeout method")
public void testCache() throws Exception {
    Session s;
    Transaction tx;
    s = openSession();
    tx = s.beginTransaction();
    Plane plane = new Plane();
    plane.setNbrOfSeats(5);
    s.persist(plane);
    tx.commit();
    s.close();
    sessionFactory().getStatistics().clear();
    sessionFactory().getStatistics().setStatisticsEnabled(true);
    s = openSession();
    tx = s.beginTransaction();
    Query query = s.getNamedQuery("plane.byId").setParameter("id", plane.getId());
    plane = (Plane) query.uniqueResult();
    assertEquals(1, sessionFactory().getStatistics().getQueryCachePutCount());
    plane = (Plane) s.getNamedQuery("plane.byId").setParameter("id", plane.getId()).uniqueResult();
    assertEquals(1, sessionFactory().getStatistics().getQueryCacheHitCount());
    tx.commit();
    s.close();
    s = openSession();
    tx = s.beginTransaction();
    s.delete(s.get(Plane.class, plane.getId()));
    tx.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) SQLQuery(org.hibernate.SQLQuery) Query(org.hibernate.Query) Plane(org.hibernate.test.annotations.Plane) 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