Search in sources :

Example 16 with EntityTransaction

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

the class TreatKeywordTest method testTreatWithRestrictionOnAbstractClass.

@Test
@TestForIssue(jiraKey = "HHH-9411")
public void testTreatWithRestrictionOnAbstractClass() {
    EntityManager em = getOrCreateEntityManager();
    EntityTransaction entityTransaction = em.getTransaction();
    entityTransaction.begin();
    Greyhound greyhound = new Greyhound();
    Dachshund dachshund = new Dachshund();
    em.persist(greyhound);
    em.persist(dachshund);
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<TreatAnimal> criteriaQuery = cb.createQuery(TreatAnimal.class);
    Root<TreatAnimal> animal = criteriaQuery.from(TreatAnimal.class);
    Root<Dog> dog = cb.treat(animal, Dog.class);
    // only fast dogs
    criteriaQuery.where(cb.isTrue(dog.<Boolean>get("fast")));
    List<TreatAnimal> results = em.createQuery(criteriaQuery).getResultList();
    // we should only have a single Greyhound here, not slow long dogs!
    assertEquals(Arrays.asList(greyhound), results);
    entityTransaction.commit();
    em.close();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 17 with EntityTransaction

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

the class EntitySuperclassCollectionTest method createPerson.

private PersonBase createPerson(PersonBase person, String address) {
    EntityManager em = createEntityManager();
    EntityTransaction tx = em.getTransaction();
    tx.begin();
    person.addresses.add(new Address(address));
    person = em.merge(person);
    tx.commit();
    return person;
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager)

Example 18 with EntityTransaction

use of javax.persistence.EntityTransaction 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 19 with EntityTransaction

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

Example 20 with EntityTransaction

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

Aggregations

EntityTransaction (javax.persistence.EntityTransaction)56 EntityManager (javax.persistence.EntityManager)36 Test (org.junit.Test)12 EntityManagerFactory (javax.persistence.EntityManagerFactory)9 Query (javax.persistence.Query)8 UserTestDbo (org.webpieces.plugins.hibernate.app.dbo.UserTestDbo)6 InumSqlEntry (org.gluu.oxtrust.model.sql.InumSqlEntry)4 Person (org.hibernate.userguide.model.Person)3 TransactionStatus (org.springframework.transaction.TransactionStatus)3 Transactional (com.google.inject.persist.Transactional)2 Priority (org.hibernate.envers.test.Priority)2 DefaultTransactionAttribute (org.springframework.transaction.interceptor.DefaultTransactionAttribute)2 UserDbo (WEBPIECESxPACKAGE.base.libs.UserDbo)1 UserRole (WEBPIECESxPACKAGE.base.libs.UserRole)1 FileType (aserg.gtf.model.FileType)1 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1