use of javax.persistence.EntityTransaction in project tests by datanucleus.
the class DatastoreIdentityTest method testBasic.
/**
* Basic test.
*/
public void testBasic() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
Object id = null;
try {
tx.begin();
DSIDHolder holder = new DSIDHolder("First Holder");
em.persist(holder);
em.flush();
id = NucleusJPAHelper.getObjectId(holder);
tx.commit();
} catch (Exception e) {
LOG.error(e);
e.printStackTrace();
fail(e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
emf.getCache().evictAll();
em = getEM();
tx = em.getTransaction();
try {
tx.begin();
Object key = IdentityUtils.getTargetKeyForDatastoreIdentity(id);
DSIDHolder holder = em.find(DSIDHolder.class, key);
assertNotNull(holder);
assertEquals("First Holder", holder.getName());
tx.commit();
} catch (Exception e) {
LOG.error(e);
e.printStackTrace();
fail(e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(DSIDHolder.class);
}
}
use of javax.persistence.EntityTransaction in project tests by datanucleus.
the class EmbeddedTest method testOneToManyEmbeddedElements.
/**
* Test of 1-N relation with embedded elements.
*/
public void testOneToManyEmbeddedElements() {
if (!storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_EMBEDDED_COLLECTION)) {
return;
}
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Processor proc = new Processor(1, "RISC");
Job job1 = new Job("Cron backup", 1);
proc.addJob(job1);
Job job2 = new Job("Jenkins", 2);
proc.addJob(job2);
em.persist(proc);
tx.commit();
} catch (Exception e) {
LOG.error("Exception thrown creating data", e);
fail("Exception thrown while creating data (see log for details) : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
// Check the contents of the datastore
em = getEM();
tx = em.getTransaction();
try {
tx.begin();
Processor proc = em.find(Processor.class, 1);
assertNotNull(proc);
List<Job> jobs = proc.getJobs();
assertNotNull(jobs);
assertEquals(2, jobs.size());
tx.rollback();
} catch (Exception e) {
LOG.error("Exception thrown retrieving data", e);
fail("Exception thrown while retrieving data " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(Processor.class);
}
}
use of javax.persistence.EntityTransaction in project tests by datanucleus.
the class EmbeddedTest method testOneToManyWithEmbeddedId.
/**
* Test of 1-N relation when the owner has an embedded id.
*/
public void testOneToManyWithEmbeddedId() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Department dept = new Department("Marketing");
DepartmentPK deptPK = new DepartmentPK(101, "Mkt");
dept.setPrimaryKey(deptPK);
Project prj1 = new Project("DN 2.0", 100000);
dept.getProjects().add(prj1);
em.persist(dept);
tx.commit();
} catch (Exception e) {
LOG.error("Exception thrown creating data", e);
fail("Exception thrown while creating data (see log for details) : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
// Check the contents of the datastore
em = getEM();
tx = em.getTransaction();
try {
tx.begin();
Query q = em.createQuery("SELECT d FROM " + Department.class.getName() + " d");
List<Department> depts = q.getResultList();
assertNotNull("Returned Department List is null", depts);
assertEquals("Number of Departments is incorrect", 1, depts.size());
tx.rollback();
} catch (Exception e) {
LOG.error("Exception thrown retrieving data", e);
fail("Exception thrown while retrieving data " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(Department.class);
clean(Project.class);
}
}
use of javax.persistence.EntityTransaction in project tests by datanucleus.
the class EntityGraphTest method testQueryNamedEntityGraph_LoadGraph.
public void testQueryNamedEntityGraph_LoadGraph() {
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);
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();
try {
tx.begin();
EntityGraph eg = em.getEntityGraph("baseGraph");
Query q = em.createQuery("SELECT b FROM GraphBase b");
q.setHint("javax.persistence.loadgraph", eg);
List<GraphBase> results = q.getResultList();
// Check internal implementation setting groups to just this group
Set<String> fpgroups = ((JPAQuery) q).getFetchPlan().getGroups();
assertEquals(2, fpgroups.size());
assertTrue(fpgroups.contains("default"));
assertTrue(fpgroups.contains("baseGraph"));
GraphBase result = results.get(0);
PersistenceUnitUtil util = em.getEntityManagerFactory().getPersistenceUnitUtil();
assertTrue(util.isLoaded(result, "id"));
assertTrue(util.isLoaded(result, "name"));
assertTrue(util.isLoaded(result, "relation"));
tx.rollback();
} catch (Exception e) {
LOG.error("Exception on persist+query", e);
fail("Exception in test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(GraphBase.class);
clean(GraphRelated.class);
}
}
use of javax.persistence.EntityTransaction in project tests by datanucleus.
the class EntityGraphTest method testQueryDefinedEntityGraph_FetchGraph.
/**
* Test of specification and registering of a defined EntityGraph.
*/
public void testQueryDefinedEntityGraph_FetchGraph() {
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);
em.persist(base);
tx.commit();
} catch (Exception e) {
LOG.error("Exception on persist", 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();
try {
tx.begin();
EntityGraph<GraphBase> eg = em.createEntityGraph(GraphBase.class);
eg.addAttributeNodes("id");
eg.addAttributeNodes("name");
eg.addAttributeNodes("relation");
assertNull(eg.getName());
Query q = em.createQuery("SELECT b FROM GraphBase b");
q.setHint("javax.persistence.fetchgraph", eg);
List<GraphBase> results = q.getResultList();
LOG.info(">> FetchPlan=" + ((JPAQuery) q).getFetchPlan());
Set<String> fpgroups = ((JPAQuery) q).getFetchPlan().getGroups();
assertEquals(1, fpgroups.size());
LOG.info(">> FPgroups=" + StringUtils.collectionToString(fpgroups));
GraphBase result = results.get(0);
PersistenceUnitUtil util = em.getEntityManagerFactory().getPersistenceUnitUtil();
assertTrue(util.isLoaded(result, "id"));
assertTrue(util.isLoaded(result, "name"));
assertTrue(util.isLoaded(result, "relation"));
tx.rollback();
} catch (Exception e) {
LOG.error("Exception on persist+query", e);
fail("Exception in test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(GraphBase.class);
clean(GraphRelated.class);
}
}
Aggregations