use of org.datanucleus.samples.annotations.one_many.collection.PCFKListElement 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 org.datanucleus.samples.annotations.one_many.collection.PCFKListElement in project tests by datanucleus.
the class JPQLQueryTest method testINDEX.
public void testINDEX() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
ListHolder holder = new ListHolder(1);
holder.getJoinListPC().add(new PCFKListElement(1, "First"));
holder.getJoinListPC().add(new PCFKListElement(2, "Second"));
holder.getJoinListPC().add(new PCFKListElement(3, "Third"));
em.persist(holder);
em.flush();
List result = em.createQuery("SELECT e.name FROM ListHolder l JOIN l.joinListPC e WHERE INDEX(e) = 1").getResultList();
assertEquals("Number of records is incorrect", 1, result.size());
assertEquals("Name of element is incorrect", "Second", result.iterator().next());
tx.rollback();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(ListHolder.class);
clean(PCFKListElement.class);
}
}
use of org.datanucleus.samples.annotations.one_many.collection.PCFKListElement in project tests by datanucleus.
the class RelationshipsTest method testOneToManyOrderColumn.
/**
* Test of 1-N List of entity elements using an order column.
*/
public void testOneToManyOrderColumn() {
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);
ListHolder holder2 = new ListHolder(2);
holder2.getJoinListPC().add(new PCFKListElement(4, "Fourth"));
em.persist(holder2);
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
em = getEM();
tx = em.getTransaction();
try {
tx.begin();
List result1 = em.createQuery("SELECT Object(T) FROM " + ListHolder.class.getName() + " T WHERE id = 1").getResultList();
assertEquals(1, result1.size());
ListHolder holder1 = (ListHolder) result1.get(0);
List<PCFKListElement> elements1 = holder1.getJoinListPC();
assertEquals("Number of elements in List(1) is incorrect", 3, elements1.size());
PCFKListElement elem1 = elements1.get(0);
assertEquals("First element in List(1) is incorrect", "First", elem1.getName());
PCFKListElement elem2 = elements1.get(1);
assertEquals("Second element in List(1) is incorrect", "Second", elem2.getName());
PCFKListElement elem3 = elements1.get(2);
assertEquals("Third element in List(1) is incorrect", "Third", elem3.getName());
List result2 = em.createQuery("SELECT Object(T) FROM " + ListHolder.class.getName() + " T WHERE id = 2").getResultList();
assertEquals(1, result2.size());
ListHolder holder2 = (ListHolder) result2.get(0);
List<PCFKListElement> elements2 = holder2.getJoinListPC();
assertEquals("Number of elements in List(2) is incorrect", 1, elements2.size());
PCFKListElement elem4 = elements2.get(0);
assertEquals("First element in List(2) is incorrect", "Fourth", elem4.getName());
tx.rollback();
} 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 org.datanucleus.samples.annotations.one_many.collection.PCFKListElement in project tests by datanucleus.
the class RelationshipsTest method testOneToManyWithOrphanRemovalAndElementRemoval.
/**
* Test of 1-N List of entity elements and use of orphan removal flag when removing the element from the list.
*/
public void testOneToManyWithOrphanRemovalAndElementRemoval() {
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());
// Remove element from List should delete it
PCFKListElement el2 = em.find(PCFKListElement.class, new Long(2));
holder1.getJoinListPC().remove(el2);
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));
assertNotNull("Holder shouldnt have been deleted but was", holder1);
assertEquals("Number of elements is wrong", 2, holder1.getJoinListPC().size());
PCFKListElement el1 = em.find(PCFKListElement.class, new Long(1));
assertNotNull("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));
assertNotNull("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);
}
}
Aggregations