Search in sources :

Example 61 with EntityManager

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();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 62 with EntityManager

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();
}
Also used : EntityManager(javax.persistence.EntityManager) Before(org.junit.Before)

Example 63 with EntityManager

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();
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Test(org.junit.Test)

Example 64 with EntityManager

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();
}
Also used : EntityManager(javax.persistence.EntityManager) Test(org.junit.Test)

Example 65 with EntityManager

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();
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Date(java.util.Date) Test(org.junit.Test)

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