use of javax.persistence.EntityManager in project hibernate-orm by hibernate.
the class QueryTest method testCriteriaBetween.
@Test
@TestForIssue(jiraKey = "HHH-9356")
public void testCriteriaBetween() {
final EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
final CriteriaBuilder cb = em.getCriteriaBuilder();
final CriteriaQuery<Employee> query = cb.createQuery(Employee.class);
final Root<Employee> root = query.from(Employee.class);
query.select(root);
query.where(cb.between(root.<Float>get("salary"), new Float(300f), new Float(400f)));
final List<Employee> result0 = em.createQuery(query).getResultList();
assertEquals(0, result0.size());
query.where(cb.between(root.<Float>get("salary"), new Float(100f), new Float(200f)));
final List<Employee> result1 = em.createQuery(query).getResultList();
assertEquals(0, result1.size());
query.where(cb.between(root.<Float>get("salary"), new Float(200f), new Float(300f)));
final List<Employee> result2 = em.createQuery(query).getResultList();
assertEquals(1, result2.size());
em.getTransaction().commit();
em.close();
}
use of javax.persistence.EntityManager in project hibernate-orm by hibernate.
the class QueryTest method setUpTestData.
@Before
public void setUpTestData() {
EntityManager em = entityManagerFactory().createEntityManager();
em.getTransaction().begin();
em.persist(new Employee(1, new Name("John", "Q.", "Doe"), SALARY));
em.getTransaction().commit();
em.close();
}
use of javax.persistence.EntityManager in project hibernate-orm by hibernate.
the class FetchTest method testTwoLevelDeepPersistOnManyToOne.
@Test
public void testTwoLevelDeepPersistOnManyToOne() throws Exception {
EntityTransaction tx;
EntityManager em = getOrCreateEntityManager();
tx = em.getTransaction();
tx.begin();
Grandson gs = new Grandson();
gs.setParent(new Son());
gs.getParent().setParent(new Parent());
em.persist(gs);
tx.commit();
em.close();
em = getOrCreateEntityManager();
tx = em.getTransaction();
tx.begin();
gs = em.find(Grandson.class, gs.getId());
em.flush();
assertTrue(Hibernate.isInitialized(gs.getParent()));
assertFalse(Hibernate.isInitialized(gs.getParent().getParent()));
em.remove(gs);
tx.commit();
em.close();
}
use of javax.persistence.EntityManager in project hibernate-orm by hibernate.
the class FetchTest method testCascadeAndFetchEntity.
@Test
public void testCascadeAndFetchEntity() throws Exception {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
Troop disney = new Troop();
disney.setName("Disney");
Soldier mickey = new Soldier();
mickey.setName("Mickey");
disney.addSoldier(mickey);
em.persist(disney);
em.getTransaction().commit();
em.close();
em = getOrCreateEntityManager();
em.getTransaction().begin();
Soldier soldier = em.find(Soldier.class, mickey.getId());
assertFalse(Hibernate.isInitialized(soldier.getTroop()));
em.getTransaction().commit();
assertFalse(Hibernate.isInitialized(soldier.getTroop()));
em.close();
em = getOrCreateEntityManager();
em.getTransaction().begin();
Troop troop = em.find(Troop.class, disney.getId());
em.remove(troop);
//Fail because of HHH-1187
em.getTransaction().commit();
em.close();
}
use of javax.persistence.EntityManager in project hibernate-orm by hibernate.
the class FetchTest method testTwoLevelDeepPersist.
@Test
public void testTwoLevelDeepPersist() throws Exception {
EntityTransaction tx;
EntityManager em = getOrCreateEntityManager();
tx = em.getTransaction();
tx.begin();
Conference jbwBarcelona = new Conference();
jbwBarcelona.setDate(new Date());
ExtractionDocumentInfo info = new ExtractionDocumentInfo();
info.setConference(jbwBarcelona);
jbwBarcelona.setExtractionDocument(info);
info.setLastModified(new Date());
ExtractionDocument doc = new ExtractionDocument();
doc.setDocumentInfo(info);
info.setDocuments(new ArrayList<ExtractionDocument>());
info.getDocuments().add(doc);
doc.setBody(new byte[] { 'c', 'f' });
em.persist(jbwBarcelona);
tx.commit();
em.close();
em = getOrCreateEntityManager();
tx = em.getTransaction();
tx.begin();
jbwBarcelona = em.find(Conference.class, jbwBarcelona.getId());
assertTrue(Hibernate.isInitialized(jbwBarcelona));
assertTrue(Hibernate.isInitialized(jbwBarcelona.getExtractionDocument()));
assertFalse(Hibernate.isInitialized(jbwBarcelona.getExtractionDocument().getDocuments()));
em.flush();
assertTrue(Hibernate.isInitialized(jbwBarcelona));
assertTrue(Hibernate.isInitialized(jbwBarcelona.getExtractionDocument()));
assertFalse(Hibernate.isInitialized(jbwBarcelona.getExtractionDocument().getDocuments()));
em.remove(jbwBarcelona);
tx.commit();
em.close();
}
Aggregations