use of org.datanucleus.samples.annotations.models.company.Project in project tests by datanucleus.
the class AnnotationTest method testBasic.
/**
* Test of basic JPA annotations reading capability
*/
public void testBasic() {
NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
ClassLoaderResolver clr = new ClassLoaderResolverImpl();
// Checks for Department
ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Department.class.getName(), clr);
String prefix = cmd1.getFullClassName() + " : ";
assertEquals(prefix + "detachable is wrong", cmd1.isDetachable(), true);
assertEquals(prefix + "identity-type is wrong", cmd1.getIdentityType(), IdentityType.APPLICATION);
assertEquals(prefix + "embedded-only is wrong", cmd1.isEmbeddedOnly(), false);
assertEquals(prefix + "requires-extent is wrong", cmd1.isRequiresExtent(), true);
assertEquals(prefix + "catalog is wrong", cmd1.getCatalog(), null);
assertEquals(prefix + "schema is wrong", cmd1.getSchema(), null);
assertEquals(prefix + "table is wrong", cmd1.getTable(), "JPA_AN_DEPARTMENT");
assertEquals(prefix + "has incorrect number of persistent fields", cmd1.getNoOfManagedMembers(), 4);
InheritanceMetaData inhmd1 = cmd1.getInheritanceMetaData();
assertEquals("Inheritance strategy is incorrect", InheritanceStrategy.NEW_TABLE, inhmd1.getStrategy());
// "projects"
AbstractMemberMetaData fmd = cmd1.getMetaDataForMember("projects");
assertNotNull(prefix + "doesnt have required field", fmd);
assertEquals(prefix + "should be persistent", fmd.getPersistenceModifier(), FieldPersistenceModifier.PERSISTENT);
assertFalse(prefix + "pk is wrong", fmd.isPrimaryKey());
assertFalse(prefix + "dfg is wrong", fmd.isDefaultFetchGroup());
assertTrue(prefix + "has no container specified!", fmd.getCollection() != null);
assertEquals(prefix + "should have collection of Project elements but hasnt", fmd.getCollection().getElementType(), Project.class.getName());
assertEquals(prefix + "shouldnt have collection of serialised elements but has", fmd.getCollection().isSerializedElement(), false);
assertEquals(prefix + "shouldnt have collection of dependent elements but has", fmd.getCollection().isDependentElement(), false);
// Checks for Project
ClassMetaData cmd2 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Project.class.getName(), clr);
prefix = cmd2.getFullClassName() + " : ";
assertEquals(prefix + "detachable is wrong", true, cmd2.isDetachable());
assertEquals(prefix + "identity-type is wrong", cmd2.getIdentityType(), IdentityType.APPLICATION);
assertEquals(prefix + "objectid-class is wrong", "org.datanucleus.identity.StringId", cmd2.getObjectidClass());
assertEquals(prefix + "embedded-only is wrong", cmd2.isEmbeddedOnly(), false);
assertEquals(prefix + "requires-extent is wrong", cmd2.isRequiresExtent(), true);
assertEquals(prefix + "catalog is wrong", cmd2.getCatalog(), null);
assertEquals(prefix + "schema is wrong", cmd2.getSchema(), null);
assertEquals(prefix + "table is wrong", "JPA_AN_PROJECT", cmd2.getTable());
assertEquals(prefix + "has incorrect number of persistent fields", cmd2.getNoOfManagedMembers(), 2);
InheritanceMetaData inhmd2 = cmd2.getInheritanceMetaData();
assertEquals("Inheritance strategy is incorrect", InheritanceStrategy.NEW_TABLE, inhmd2.getStrategy());
// "name"
fmd = cmd2.getMetaDataForMember("name");
assertNotNull(prefix + "doesnt have required field", fmd);
assertTrue(prefix + "pk is wrong", fmd.isPrimaryKey());
assertTrue(prefix + "dfg is wrong", fmd.isDefaultFetchGroup());
assertEquals(prefix + "should be persistent", fmd.getPersistenceModifier(), FieldPersistenceModifier.PERSISTENT);
// "budget"
fmd = cmd2.getMetaDataForMember("budget");
assertNotNull(prefix + "doesnt have required field", fmd);
assertEquals(prefix + "has incorrect persistent field", fmd.getName(), "budget");
assertFalse(prefix + "pk is wrong", fmd.isPrimaryKey());
assertTrue(prefix + "dfg is wrong", fmd.isDefaultFetchGroup());
assertEquals(prefix + "should be persistent", fmd.getPersistenceModifier(), FieldPersistenceModifier.PERSISTENT);
}
use of org.datanucleus.samples.annotations.models.company.Project 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.Project in project tests by datanucleus.
the class JPQLQueryTest method testJoinRootOn.
/**
* Test use of JPQL JOIN to another root using ON (DataNucleus Extension).
*/
public void testJoinRootOn() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Project prj1 = new Project("DataNucleus", 1000000);
em.persist(prj1);
Project prj2 = new Project("JPOX", 50000);
em.persist(prj2);
Account acct1 = new Account();
acct1.setUsername("DataNucleus");
acct1.setId(1);
em.persist(acct1);
em.flush();
/**
*This will generate the following
*
*QueryCompilation:
* [result:PrimaryExpression{p.name},PrimaryExpression{p.budget}]
* [from:ClassExpression(alias=p join=JoinExpression{JOIN_INNER PrimaryExpression{Account} alias=a on=DyadicExpression{PrimaryExpression{p.name} = PrimaryExpression{a.username}}})]
* [symbols: p type=org.datanucleus.samples.annotations.models.company.Project, a type=org.datanucleus.samples.annotations.models.company.Account]
*
*SELECT P."NAME",P.BUDGET FROM JPA_AN_PROJECT P INNER JOIN JPA_AN_ACCOUNT A ON P."NAME" = A.USERNAME
*/
Query q = em.createQuery("SELECT p.name, p.budget FROM " + Project.class.getName() + " p JOIN Account a ON p.name = a.username");
List<Object[]> results = q.getResultList();
assertNotNull(results);
assertEquals(1, results.size());
for (Object[] row : results) {
assertEquals(2, row.length);
assertEquals("DataNucleus", row[0]);
assertEquals(new Long(1000000), row[1]);
}
// TODO Add some asserts, or choose a good example for this
tx.rollback();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(Account.class);
clean(Project.class);
}
}
use of org.datanucleus.samples.annotations.models.company.Project in project tests by datanucleus.
the class EntityManagerTest method testMergeOfTransientAsAttach.
/**
* Test of EntityManager.merge() of a transient object that represents one in the datastore
*/
public void testMergeOfTransientAsAttach() {
try {
EntityManager em = emf.createEntityManager();
em.setProperty(PropertyNames.PROPERTY_ALLOW_ATTACH_OF_TRANSIENT, "true");
EntityTransaction tx = em.getTransaction();
try {
// Persist an object
tx.begin();
Project p1 = new Project("DataNucleus Xenon", 125000);
em.persist(p1);
tx.commit();
// Merge the detached object
Project p2 = new Project("DataNucleus Xenon", 150000);
tx.begin();
Project p = em.merge(p2);
assertEquals("Project budget not merged in returned object", 150000, p.getBudget());
tx.commit();
} catch (Exception e) {
LOG.error("Exception thrown on merge of transient", e);
fail("Exception using merge of transient " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
em.close();
emf.getCache().evictAll();
em = emf.createEntityManager();
tx = em.getTransaction();
try {
tx.begin();
Project p = (Project) em.find(Project.class, "DataNucleus Xenon");
assertEquals("Budget is incorrect. Merge didn't succeed", 150000, p.getBudget());
tx.commit();
} catch (Exception e) {
LOG.error("Exception thrown on retrieval", e);
fail("Exception using retrieval " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
em.close();
} finally {
clean(Project.class);
}
}
Aggregations