use of javax.persistence.EntityTransaction in project tests by datanucleus.
the class EntityGraphTest method testQueryMultipleLevelsUsingLoadGraph.
public void testQueryMultipleLevelsUsingLoadGraph() {
try {
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
GraphBase base = new GraphBase(1, "First Base");
GraphRelated related = new GraphRelated(101);
base.setRelation(related);
GraphRelatedNext next = new GraphRelatedNext(201);
next.setName("First Next Relation");
related.setRelationNext(next);
em.persist(base);
tx.commit();
} catch (Exception e) {
LOG.error("Exception on persist+query", e);
fail("Exception in test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
if (emf.getCache() != null) {
emf.getCache().evictAll();
}
em = emf.createEntityManager();
tx = em.getTransaction();
List<GraphBase> results = null;
try {
tx.begin();
EntityGraph<GraphBase> eg = em.createEntityGraph(GraphBase.class);
eg.addAttributeNodes("name", "relation");
Subgraph<GraphRelated> relationGraph = eg.addSubgraph("relation", GraphRelated.class);
relationGraph.addAttributeNodes("nextRelation");
Subgraph<GraphRelatedNext> cityGraph = relationGraph.addSubgraph("nextRelation", GraphRelatedNext.class);
cityGraph.addAttributeNodes("name");
Query q = em.createQuery("SELECT b FROM GraphBase b");
q.setHint("javax.persistence.loadgraph", eg);
results = q.getResultList();
tx.commit();
} catch (Exception e) {
LOG.error("Exception on persist+query", e);
fail("Exception in test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
// Will now be detached, so check detachment
assertEquals(1, results.size());
// Test if everything was loaded as per JPA Spec 3.2.7
assertEquals("First Next Relation", results.get(0).getRelation().getRelationNext().getName());
} finally {
clean(GraphBase.class);
clean(GraphRelated.class);
clean(GraphRelatedNext.class);
}
}
use of javax.persistence.EntityTransaction in project tests by datanucleus.
the class EntityManagerFactoryTest method testPersistenceUnitUtilGetIdentifier.
/**
* Test for emf.getPersistenceUnitUtil.getIdentifier() method
*/
public void testPersistenceUnitUtilGetIdentifier() {
try {
PersistenceUnitUtil util = emf.getPersistenceUnitUtil();
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Person p = new Person(101, "Fred", "Flintstone", "fred.flintstone@jpox.com");
p.setGlobalNum("First");
em.persist(p);
assertTrue(util.getIdentifier(p) instanceof Person.PK);
VersionedPerson vp = new VersionedPerson(1, 1);
em.persist(vp);
Object vpId = util.getIdentifier(vp);
assertTrue(vpId instanceof Long && ((Long) vpId) == 1);
tx.rollback();
} catch (Exception e) {
LOG.error(">> Exception on persist before serialisation", e);
fail("Exception on persist : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
// No cleanup needed
}
}
use of javax.persistence.EntityTransaction in project tests by datanucleus.
the class EntityManagerTest method testPersistEntityWithInterfaceField.
/**
* Test of persistence/retrieval of object with an interface field.
*/
public void testPersistEntityWithInterfaceField() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
ShapeHolder holder = new ShapeHolder(100);
holder.setShape1(new Rectangle(200, 45, 55));
Circle circ = new Circle(300, 25.8);
holder.getShapeSet1().add(circ);
em.persist(holder);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
// Retrieve it
em = getEM();
tx = em.getTransaction();
try {
tx.begin();
ShapeHolder holder = em.find(ShapeHolder.class, 100);
assertNotNull(holder);
Shape shape = holder.getShape1();
assertNotNull(shape);
assertTrue(shape instanceof Rectangle);
Rectangle rect = (Rectangle) shape;
assertEquals(45.0, rect.getWidth(), 0.1);
assertEquals(55.0, rect.getLength(), 0.1);
Set<Shape> shapes = holder.getShapeSet1();
assertNotNull(shapes);
assertEquals(1, shapes.size());
Shape setShape = shapes.iterator().next();
assertTrue(setShape instanceof Circle);
Circle circ = (Circle) setShape;
assertEquals(25.8, circ.getRadius(), 0.1);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(ShapeHolder.class);
clean(Rectangle.class);
clean(Circle.class);
}
}
use of javax.persistence.EntityTransaction in project tests by datanucleus.
the class EntityManagerTest method testMarkForRollbackOnError.
/**
* Test of marking of current txn for rollback on exception.
*/
public void testMarkForRollbackOnError() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
try {
em.find(Account.class, new Long(123));
} catch (EntityNotFoundException enfe) {
if (!tx.getRollbackOnly()) {
fail("Transaction wasn't marked for rollback after exception on object find()");
}
}
tx.rollback();
} catch (Exception e) {
e.printStackTrace();
} finally {
em.close();
}
} finally {
clean(Account.class);
}
}
use of javax.persistence.EntityTransaction in project tests by datanucleus.
the class EntityManagerTest method testPersistThenDetach.
/**
* Test of EntityManager.persist() and detach().
*/
public void testPersistThenDetach() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Person p = new Person(101, "Fred", "Flintstone", "fred.flintstone@jpox.com");
p.setGlobalNum("First");
em.persist(p);
em.detach(p);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
em.close();
} finally {
clean(Person.class);
}
}
Aggregations