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();
}
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();
}
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();
}
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();
}
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();
}
Aggregations