use of org.datanucleus.samples.annotations.models.company.Department 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 org.datanucleus.samples.annotations.models.company.Department in project tests by datanucleus.
the class EmbeddedTest method testPersistenceAndDeletionWithEmbeddedId.
/**
* Test for basic persistence and deletion with a class using @EmbeddedId.
*/
public void testPersistenceAndDeletionWithEmbeddedId() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
// Persist the object and query it
tx.begin();
Department d = new Department("Bureaucracy");
DepartmentPK pk = new DepartmentPK(Integer.valueOf(1), "1");
d.setPrimaryKey(pk);
em.persist(d);
List result = em.createQuery("SELECT T FROM " + Department.class.getName() + " T").getResultList();
assertEquals(1, result.size());
assertEquals(pk.getIdString(), ((Department) result.get(0)).getPrimaryKey().getIdString());
tx.commit();
// Retrieve the object and delete it
tx.begin();
result = em.createQuery("SELECT T FROM " + Department.class.getName() + " T").getResultList();
d = (Department) result.get(0);
em.remove(d);
em.flush();
tx.commit();
} catch (Exception e) {
LOG.error("Exception during persist and query", e);
fail("Exception during persist + query : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(Department.class);
}
}
Aggregations