use of javax.persistence.EntityTransaction in project tests by datanucleus.
the class RelationshipsTest method testOneToManyWithOrphanRemovalAndDeleting.
/**
* Test of 1-N List of entity elements and use of orphan removal flag when deleting the owner.
*/
public void testOneToManyWithOrphanRemovalAndDeleting() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
ListHolder holder1 = new ListHolder(1);
holder1.getJoinListPC().add(new PCFKListElement(1, "First"));
holder1.getJoinListPC().add(new PCFKListElement(2, "Second"));
holder1.getJoinListPC().add(new PCFKListElement(3, "Third"));
em.persist(holder1);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown while creating data");
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
// Check the contents of the datastore and trigger the delete
em = getEM();
tx = em.getTransaction();
try {
tx.begin();
ListHolder holder1 = em.find(ListHolder.class, new Long(1));
assertEquals("Number of list elements is wrong", 3, holder1.getJoinListPC().size());
// Delete holder which should trigger the orphan removal on the elements
em.remove(holder1);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown while retrieving data");
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
// Check the contents of the datastore
em = getEM();
tx = em.getTransaction();
try {
tx.begin();
ListHolder holder1 = em.find(ListHolder.class, new Long(1));
assertNull("Holder should have been deleted but wasn't", holder1);
PCFKListElement el1 = em.find(PCFKListElement.class, new Long(1));
assertNull("Element1 should have been deleted but wasn't", el1);
PCFKListElement el2 = em.find(PCFKListElement.class, new Long(2));
assertNull("Element2 should have been deleted but wasn't", el2);
PCFKListElement el3 = em.find(PCFKListElement.class, new Long(3));
assertNull("Element3 should have been deleted but wasn't", el3);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown while retrieving data");
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(ListHolder.class);
clean(PCFKListElement.class);
}
}
use of javax.persistence.EntityTransaction in project tests by datanucleus.
the class RelationshipsTest method testOneToManyBidirMappedByDotNotation.
/**
* Test of 1-N bidir with element having embedded PC, and 1-1 bidir with embedded PC, and using "mappedBy" with DOT notation.
*/
public void testOneToManyBidirMappedByDotNotation() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Contact c1 = new Contact("John Doe");
Document d1 = new Document("Doc1", c1);
Document d2 = new Document("Doc2", c1);
c1.setMainDocument(d1);
d1.getDetails().setOwner(c1);
c1.getDocuments().add(d2);
d2.getDetails().setOwner(c1);
em.persist(c1);
em.persist(d1);
em.persist(d2);
Contact c2 = new Contact("Jane Dose");
em.persist(c2);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown while creating data");
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
// Query the datastore and check the results
em = getEM();
tx = em.getTransaction();
List<Document> documents = null;
try {
tx.begin();
TypedQuery<Document> query = em.createQuery("SELECT d FROM Document d JOIN FETCH d.details.owner owner_1 WHERE d.name = 'Doc2'", Document.class);
documents = query.getResultList();
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown while retrieving data");
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
assertEquals(1, documents.size());
assertEquals("Doc2", documents.get(0).getName());
assertEquals("John Doe", documents.get(0).getDetails().getOwner().getName());
em = getEM();
tx = em.getTransaction();
try {
tx.begin();
// Null out the relations
TypedQuery<Contact> query = em.createQuery("SELECT c FROM " + Contact.class.getName() + " c", Contact.class);
List<Contact> contacts = query.getResultList();
for (Contact c : contacts) {
c.setMainDocument(null);
c.getDocuments().clear();
}
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown while retrieving data");
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(Contact.class);
clean(Document.class);
}
}
use of javax.persistence.EntityTransaction in project tests by datanucleus.
the class RelationshipsTest method testOneToManyJoinTableIndexedList.
/**
* Test of 1-N JoinTable indexed list (JPA2).
*/
public void testOneToManyJoinTableIndexedList() {
EntityManagerFactory emf2 = getEMF(1, "JPATest", null);
try {
EntityManager em = emf2.createEntityManager();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
UserGroup grp = new UserGroup(1, "JDO Expert Group");
GroupMember member1 = new GroupMember(1, "Craig Russell");
GroupMember member2 = new GroupMember(2, "David Jordan");
List<GroupMember> members = new ArrayList<>();
members.add(member1);
members.add(member2);
grp.setMembers(members);
em.persist(grp);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown while creating UserGroup+GroupMembers");
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
// Check the contents of the datastore
em = emf2.createEntityManager();
tx = em.getTransaction();
try {
tx.begin();
List result = em.createQuery("SELECT Object(T) FROM " + UserGroup.class.getName() + " T").getResultList();
assertEquals(1, result.size());
UserGroup grp = (UserGroup) result.get(0);
Collection<GroupMember> members = grp.getMembers();
assertEquals("Number of GroupMembers in UserGroup is incorrect", 2, members.size());
Iterator<GroupMember> iter = members.iterator();
boolean hasMem1 = false;
boolean hasMem2 = false;
GroupMember mem1 = iter.next();
GroupMember mem2 = iter.next();
if (mem1.getName().equals("Craig Russell")) {
hasMem1 = true;
}
if (mem2.getName().equals("David Jordan")) {
hasMem2 = true;
}
assertTrue("First member of user group is not present (in right place)", hasMem1);
assertTrue("Second member of user group is not present (in right place)", hasMem2);
tx.rollback();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown while retrieving UserGroup+GroupMembers");
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(emf2, UserGroup.class);
clean(emf2, GroupMember.class);
}
emf2.close();
}
use of javax.persistence.EntityTransaction in project tests by datanucleus.
the class RelationshipsTest method testOneToManyFKList.
/**
* Test of 1-N FK relation with ordered list
*/
public void testOneToManyFKList() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Farm farm1 = new Farm("Giles Farm");
Animal an1 = new Animal("Spot the Dog");
farm1.getAnimals().add(an1);
an1.setFarm(farm1);
Animal an2 = new Animal("Rosa the Rhino");
farm1.getAnimals().add(an2);
an2.setFarm(farm1);
em.persist(farm1);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown while creating Farm and Animals");
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
// Check the contents of the datastore
em = getEM();
tx = em.getTransaction();
try {
tx.begin();
List result = em.createQuery("SELECT Object(T) FROM " + Farm.class.getName() + " T").getResultList();
assertEquals(1, result.size());
Farm farm = (Farm) result.get(0);
List<Animal> animals = farm.getAnimals();
assertEquals("Number of animals in Farm is incorrect", 2, animals.size());
Animal an1 = animals.get(0);
Animal an2 = animals.get(1);
assertEquals("First Animal in list has incorrect name", "Rosa the Rhino", an1.getName());
assertEquals("Second Animal in list has incorrect name", "Spot the Dog", an2.getName());
tx.rollback();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown while retrieving Farm and Animals");
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(Farm.class);
clean(Animal.class);
}
}
use of javax.persistence.EntityTransaction in project tests by datanucleus.
the class ValueGeneratorTest method testUUID.
public void testUUID() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
String uid = null;
try {
tx.begin();
CustomUUIDHolder holder = new CustomUUIDHolder();
holder.setNameField("First holder");
em.persist(holder);
tx.commit();
uid = holder.getUid();
assertNotNull(uid);
try {
UUID.fromString(uid);
} catch (IllegalArgumentException iae) {
fail("The generated uid was not a real UUID : " + iae.getMessage());
}
} catch (Exception e) {
LOG.error(">> Exception thrown during persist when using ValueGenerator", e);
fail("Failure on persist with ValueGenerator : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
if (emf.getCache() != null) {
emf.getCache().evictAll();
}
em = getEM();
tx = em.getTransaction();
try {
tx.begin();
CustomUUIDHolder h1 = em.find(CustomUUIDHolder.class, uid);
assertNotNull(h1);
assertEquals("First holder", h1.getName());
assertEquals(uid, h1.getUid());
tx.commit();
} catch (Exception e) {
LOG.error(">> Exception thrown during retrieve when using ValueGenerator", e);
fail("Failure on retrieve with ValueGenerator : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(CustomUUIDHolder.class);
}
}
Aggregations