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