Search in sources :

Example 76 with EntityManager

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

the class ExpressionsTest method prepareTestData.

@Before
public void prepareTestData() {
    builder = entityManagerFactory().getCriteriaBuilder();
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    Product product = new Product();
    product.setId("product1");
    product.setPrice(1.23d);
    product.setQuantity(2);
    product.setPartNumber(((long) Integer.MAX_VALUE) + 1);
    product.setRating(1.999f);
    product.setSomeBigInteger(BigInteger.valueOf(987654321));
    product.setSomeBigDecimal(BigDecimal.valueOf(987654.32));
    em.persist(product);
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) Product(org.hibernate.jpa.test.metamodel.Product) Before(org.junit.Before)

Example 77 with EntityManager

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

the class CallbacksTest method testCallBackListenersHierarchy.

@Test
public void testCallBackListenersHierarchy() throws Exception {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    Television tv = new Television();
    em.persist(tv);
    tv.setName("Myaio");
    tv.init();
    em.flush();
    assertEquals(1, tv.counter);
    em.getTransaction().rollback();
    em.close();
    assertEquals(5, tv.communication);
    assertTrue(tv.isLast);
}
Also used : EntityManager(javax.persistence.EntityManager) Test(org.junit.Test)

Example 78 with EntityManager

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

the class CallbacksTest method testIdNullSetByPrePersist.

@Test
public void testIdNullSetByPrePersist() throws Exception {
    Plant plant = new Plant();
    plant.setName("Origuna plantula gigantic");
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    em.persist(plant);
    em.flush();
    em.getTransaction().rollback();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) Test(org.junit.Test)

Example 79 with EntityManager

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

the class CascadeTest method testNoCascadeAndMerge.

@Test
public void testNoCascadeAndMerge() throws Exception {
    Song e1 = new Song();
    Author e2 = new Author();
    e1.setAuthor(e2);
    EntityManager em = getOrCreateEntityManager();
    EntityTransaction tx = em.getTransaction();
    tx.begin();
    em.persist(e2);
    em.persist(e1);
    tx.commit();
    em.close();
    em = getOrCreateEntityManager();
    e1 = em.find(Song.class, e1.getId());
    tx = em.getTransaction();
    tx.begin();
    em.merge(e1);
    //em.refresh(e1);
    tx.commit();
    em.close();
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Test(org.junit.Test)

Example 80 with EntityManager

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

the class CascadeTest method testCascade.

@Test
public void testCascade() throws Exception {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    Teacher teacher = new Teacher();
    Student student = new Student();
    teacher.setFavoriteStudent(student);
    student.setFavoriteTeacher(teacher);
    teacher.getStudents().add(student);
    student.setPrimaryTeacher(teacher);
    em.persist(teacher);
    em.getTransaction().commit();
    em = getOrCreateEntityManager();
    em.getTransaction().begin();
    Teacher foundTeacher = (Teacher) em.createQuery("select t from Teacher as t").getSingleResult();
    System.out.println(foundTeacher);
    System.out.println(foundTeacher.getFavoriteStudent());
    for (Student fstudent : foundTeacher.getStudents()) {
        System.out.println(fstudent);
        System.out.println(fstudent.getFavoriteTeacher());
        System.out.println(fstudent.getPrimaryTeacher());
    }
    // here *alot* of flushes occur on an object graph that has *Zero* changes.
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) 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