Search in sources :

Example 31 with TestForIssue

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

the class CastTest method testCastToString.

@Test
@SkipForDialect(value = DerbyDialect.class, comment = "Derby does not support cast from INTEGER to VARCHAR")
@TestForIssue(jiraKey = "HHH-5755")
public void testCastToString() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    Product product = new Product();
    product.setId("product1");
    product.setPrice(1.23d);
    product.setQuantity(QUANTITY);
    product.setPartNumber(((long) Integer.MAX_VALUE) + 1);
    product.setRating(1.999f);
    product.setSomeBigInteger(BigInteger.valueOf(987654321));
    product.setSomeBigDecimal(BigDecimal.valueOf(987654.321));
    em.persist(product);
    em.getTransaction().commit();
    em.close();
    em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Product> criteria = builder.createQuery(Product.class);
    Root<Product> root = criteria.from(Product.class);
    criteria.where(builder.equal(root.get(Product_.quantity).as(String.class), builder.literal(String.valueOf(QUANTITY))));
    List<Product> result = em.createQuery(criteria).getResultList();
    Assert.assertEquals(1, result.size());
    em.getTransaction().commit();
    em.close();
    em = getOrCreateEntityManager();
    em.getTransaction().begin();
    em.createQuery("delete Product").executeUpdate();
    em.getTransaction().commit();
    em.close();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Product(org.hibernate.jpa.test.metamodel.Product) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest) TestForIssue(org.hibernate.testing.TestForIssue)

Example 32 with TestForIssue

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

the class CorrelatedSubqueryTest method testCorrelatedJoinsFromSubquery.

@Test
@TestForIssue(jiraKey = "HHH-8556")
public void testCorrelatedJoinsFromSubquery() {
    CriteriaBuilder builder = entityManagerFactory().getCriteriaBuilder();
    CriteriaQuery<Customer> cquery = builder.createQuery(Customer.class);
    Root<Customer> customer = cquery.from(Customer.class);
    cquery.select(customer);
    Subquery<Order> sq = cquery.subquery(Order.class);
    Join<Customer, Order> sqo = sq.correlate(customer.join(Customer_.orders));
    sq.select(sqo);
    Set<Join<?, ?>> cJoins = sq.getCorrelatedJoins();
    // ensure the join is returned in #getCorrelatedJoins
    assertEquals(cJoins.size(), 1);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Order(org.hibernate.jpa.test.metamodel.Order) Customer(org.hibernate.jpa.test.metamodel.Customer) Join(javax.persistence.criteria.Join) CollectionJoin(javax.persistence.criteria.CollectionJoin) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest) TestForIssue(org.hibernate.testing.TestForIssue)

Example 33 with TestForIssue

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

the class CriteriaLiteralInSelectExpressionTest method testNullLiteral.

@Test
@TestForIssue(jiraKey = "HHH-10861")
public void testNullLiteral() throws Exception {
    final EntityManager entityManager = getOrCreateEntityManager();
    try {
        entityManager.getTransaction().begin();
        final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        final CriteriaQuery<MyEntityDTO> query = criteriaBuilder.createQuery(MyEntityDTO.class);
        final Root<MyEntity> entity = query.from(MyEntity.class);
        query.multiselect(criteriaBuilder.literal(false), criteriaBuilder.nullLiteral(String.class));
        final List<MyEntityDTO> dtos = entityManager.createQuery(query).getResultList();
        assertThat(dtos.size(), is(1));
        assertThat(dtos.get(0).active, is(false));
        assertThat(dtos.get(0).name, nullValue());
        assertThat(dtos.get(0).surname, nullValue());
        entityManager.getTransaction().commit();
    } catch (Exception e) {
        if (entityManager.getTransaction().isActive()) {
            entityManager.getTransaction().rollback();
        }
        throw e;
    } finally {
        entityManager.close();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 34 with TestForIssue

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

the class NullLiteralExpression method testNullLiteralExpressionInCriteriaUpdate.

@Test
@TestForIssue(jiraKey = "HHH-11159")
public void testNullLiteralExpressionInCriteriaUpdate() {
    EntityManager entityManager = getOrCreateEntityManager();
    try {
        entityManager.getTransaction().begin();
        CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        CriteriaUpdate<Person> criteriaUpdate = builder.createCriteriaUpdate(Person.class);
        criteriaUpdate.from(Person.class);
        criteriaUpdate.set(Person_.subject, builder.nullLiteral(Subject.class));
        entityManager.createQuery(criteriaUpdate).executeUpdate();
        entityManager.getTransaction().commit();
    } catch (Exception e) {
        if (entityManager.getTransaction().isActive()) {
            entityManager.getTransaction().rollback();
        }
        throw e;
    } finally {
        entityManager.close();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 35 with TestForIssue

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

the class FlushTest method testFlushOnDetached.

@Test
@TestForIssue(jiraKey = "EJBTHREE-722")
public void testFlushOnDetached() throws Exception {
    EntityManager manager = getOrCreateEntityManager();
    manager.getTransaction().begin();
    Pet p1 = createCat("Toonses", 15.0, 9, manager);
    manager.flush();
    manager.clear();
    Pet p2 = createCat("Sox", 10.0, 5, manager);
    manager.flush();
    manager.clear();
    Pet p3 = createDog("Winnie", 70.0, 5, manager);
    manager.flush();
    manager.clear();
    Pet p4 = createDog("Junior", 11.0, 1, manager);
    manager.flush();
    manager.clear();
    Decorate d1 = createDecorate("Test", p1, manager);
    manager.flush();
    manager.clear();
    Decorate d2 = createDecorate("Test2", p2, manager);
    manager.flush();
    manager.clear();
    List l = findByWeight(14.0, manager);
    manager.flush();
    manager.clear();
    for (Object o : l) {
        Assert.assertTrue(names.contains(((Pet) o).getName()));
    }
    Collection<Decorate> founds = getDecorate(manager);
    manager.flush();
    manager.clear();
    for (Decorate value : founds) {
        Assert.assertTrue(names.contains(value.getPet().getName()));
    }
    manager.getTransaction().rollback();
    manager.close();
}
Also used : EntityManager(javax.persistence.EntityManager) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

TestForIssue (org.hibernate.testing.TestForIssue)649 Test (org.junit.Test)647 Session (org.hibernate.Session)357 EntityManager (javax.persistence.EntityManager)97 List (java.util.List)91 Transaction (org.hibernate.Transaction)88 MetadataSources (org.hibernate.boot.MetadataSources)47 ArrayList (java.util.ArrayList)38 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)38 Query (org.hibernate.Query)28 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)25 Metadata (org.hibernate.boot.Metadata)24 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)24 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)23 Map (java.util.Map)22 CollectionEntry (org.hibernate.engine.spi.CollectionEntry)19 HashMap (java.util.HashMap)18 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)18 PersistentClass (org.hibernate.mapping.PersistentClass)18 HibernateException (org.hibernate.HibernateException)16