Search in sources :

Example 81 with EntityManager

use of javax.persistence.EntityManager in project hibernate-orm by hibernate.

the class DetachAndContainsTest method testDetach.

@Test
public void testDetach() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    Tooth tooth = new Tooth();
    Mouth mouth = new Mouth();
    em.persist(mouth);
    em.persist(tooth);
    tooth.mouth = mouth;
    mouth.teeth = new ArrayList<Tooth>();
    mouth.teeth.add(tooth);
    em.getTransaction().commit();
    em.close();
    em = getOrCreateEntityManager();
    em.getTransaction().begin();
    mouth = em.find(Mouth.class, mouth.id);
    assertNotNull(mouth);
    assertEquals(1, mouth.teeth.size());
    tooth = mouth.teeth.iterator().next();
    em.detach(mouth);
    assertFalse(em.contains(tooth));
    em.getTransaction().commit();
    em.close();
    em = getOrCreateEntityManager();
    em.getTransaction().begin();
    em.remove(em.find(Mouth.class, mouth.id));
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) Test(org.junit.Test)

Example 82 with EntityManager

use of javax.persistence.EntityManager in project hibernate-orm by hibernate.

the class BasicCriteriaUsageTest method testDateCompositeCustomType.

@Test
@TestForIssue(jiraKey = "HHH-8283")
public void testDateCompositeCustomType() {
    Payment payment = new Payment();
    payment.setAmount(new BigDecimal(1000));
    payment.setDate(new Date());
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    em.persist(payment);
    CriteriaQuery<Payment> criteria = em.getCriteriaBuilder().createQuery(Payment.class);
    Root<Payment> rp = criteria.from(Payment.class);
    Predicate predicate = em.getCriteriaBuilder().equal(rp.get(Payment_.date), new Date());
    criteria.where(predicate);
    TypedQuery<Payment> q = em.createQuery(criteria);
    List<Payment> payments = q.getResultList();
    assertEquals(1, payments.size());
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) BigDecimal(java.math.BigDecimal) Date(java.util.Date) Predicate(javax.persistence.criteria.Predicate) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 83 with EntityManager

use of javax.persistence.EntityManager in project hibernate-orm by hibernate.

the class BasicCriteriaUsageTest method testFunctionCriteria.

@Test
@TestForIssue(jiraKey = "HHH-8373")
public void testFunctionCriteria() {
    Wall wall = new Wall();
    wall.setColor("yellow");
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    em.persist(wall);
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Wall> query = cb.createQuery(Wall.class);
    Root<Wall> root = query.from(Wall.class);
    query.select(root).where(cb.equal(root.get("color"), cb.lower(cb.literal("YELLOW"))));
    Wall resultItem = em.createQuery(query).getSingleResult();
    assertNotNull(resultItem);
    em.getTransaction().commit();
    em.close();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 84 with EntityManager

use of javax.persistence.EntityManager in project hibernate-orm by hibernate.

the class BasicCriteriaUsageTest method testParameterCollection.

@Test
public void testParameterCollection() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaQuery<Wall> criteria = em.getCriteriaBuilder().createQuery(Wall.class);
    Root<Wall> from = criteria.from(Wall.class);
    ParameterExpression param = em.getCriteriaBuilder().parameter(String.class);
    SingularAttribute<? super Wall, ?> colorAttribute = em.getMetamodel().entity(Wall.class).getDeclaredSingularAttribute("color");
    assertNotNull("metamodel returned null singular attribute", colorAttribute);
    Predicate predicate = em.getCriteriaBuilder().equal(from.get(colorAttribute), param);
    criteria.where(predicate);
    assertEquals(1, criteria.getParameters().size());
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) ParameterExpression(javax.persistence.criteria.ParameterExpression) Predicate(javax.persistence.criteria.Predicate) Test(org.junit.Test)

Example 85 with EntityManager

use of javax.persistence.EntityManager 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)

Aggregations

EntityManager (javax.persistence.EntityManager)1435 Test (org.junit.Test)879 Priority (org.hibernate.envers.test.Priority)249 Query (javax.persistence.Query)211 TestForIssue (org.hibernate.testing.TestForIssue)97 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)87 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)79 List (java.util.List)75 NoResultException (javax.persistence.NoResultException)65 AbstractMetamodelSpecificTest (org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)63 EntityManagerFactory (javax.persistence.EntityManagerFactory)59 HashMap (java.util.HashMap)50 StrTestEntity (org.hibernate.envers.test.entities.StrTestEntity)50 PersistenceException (javax.persistence.PersistenceException)43 ArrayList (java.util.ArrayList)41 EntityNotFoundException (javax.persistence.EntityNotFoundException)39 Transactional (com.google.inject.persist.Transactional)38 EntityTransaction (javax.persistence.EntityTransaction)38 Item (org.hibernate.jpa.test.Item)36 Session (org.hibernate.Session)35