use of org.datanucleus.samples.annotations.one_one.unidir.Login in project tests by datanucleus.
the class RelationshipsTest method testOneToOneUniWithOrphanRemovalAndNulling.
/**
* Test of 1-1 Uni relation, and use of orphanRemoval
*/
public void testOneToOneUniWithOrphanRemovalAndNulling() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
LoginAccount acct = new LoginAccount(1, "Bill", "Gates");
Login login = new Login("billy", "$$$$$$");
login.setId(1);
acct.setLogin(login);
em.persist(acct);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown while creating Login and LoginAccount");
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
// Check the contents of the datastore, and trigger orphanRemoval by nulling
em = getEM();
tx = em.getTransaction();
try {
tx.begin();
LoginAccount acct = em.find(LoginAccount.class, new Long(1));
assertEquals("LoginAccount has incorrect firstName", "Bill", acct.getFirstName());
assertEquals("LoginAccount has incorrect lastName", "Gates", acct.getLastName());
assertNotNull(acct.getLogin());
Login login = acct.getLogin();
assertEquals("Login has incorrect username", "billy", login.getUserName());
assertEquals("Login has incorrect password", "$$$$$$", login.getPassword());
// Null the login field so we can trigger orphanRemoval
acct.setLogin(null);
em.flush();
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown while retrieving Login and LoginAccount");
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
// Check the contents of the datastore
em = getEM();
tx = em.getTransaction();
try {
tx.begin();
Login login = em.find(Login.class, new Long(1));
assertNull("Login should have been deleted but still exists", login);
LoginAccount acct = em.find(LoginAccount.class, new Long(1));
assertEquals("LoginAccount has incorrect firstName", "Bill", acct.getFirstName());
assertEquals("LoginAccount has incorrect lastName", "Gates", acct.getLastName());
assertNull(acct.getLogin());
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown while retrieving Login and LoginAccount");
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(LoginAccount.class);
clean(Login.class);
}
}
use of org.datanucleus.samples.annotations.one_one.unidir.Login in project tests by datanucleus.
the class RelationshipsTest method testOneToOneUniWithOrphanRemovalAndDeleting.
/**
* Test of 1-1 Uni relation, and use of orphanRemoval
*/
public void testOneToOneUniWithOrphanRemovalAndDeleting() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
LoginAccount acct = new LoginAccount(1, "Bill", "Gates");
Login login = new Login("billy", "$$$$$$");
login.setId(1);
acct.setLogin(login);
em.persist(acct);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown while creating Login and LoginAccount");
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
// Check the contents of the datastore, and trigger orphanRemoval by nulling
em = getEM();
tx = em.getTransaction();
try {
tx.begin();
LoginAccount acct = em.find(LoginAccount.class, new Long(1));
assertEquals("LoginAccount has incorrect firstName", "Bill", acct.getFirstName());
assertEquals("LoginAccount has incorrect lastName", "Gates", acct.getLastName());
assertNotNull(acct.getLogin());
Login login = acct.getLogin();
assertEquals("Login has incorrect username", "billy", login.getUserName());
assertEquals("Login has incorrect password", "$$$$$$", login.getPassword());
// Delete the LoginAccount object so we can trigger orphanRemoval
em.remove(acct);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown while retrieving Login and LoginAccount and deleting LoginAccount");
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
// Check the contents of the datastore
em = getEM();
tx = em.getTransaction();
try {
tx.begin();
Login login = em.find(Login.class, new Long(1));
assertNull("Login should have been deleted but still exists", login);
LoginAccount acct = em.find(LoginAccount.class, new Long(1));
assertNull("LoginAccount should have been deleted but still exists", acct);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown while retrieving Login and LoginAccount");
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(LoginAccount.class);
clean(Login.class);
}
}
use of org.datanucleus.samples.annotations.one_one.unidir.Login in project tests by datanucleus.
the class AnnotationTest method testSqlResultSetMapping.
/**
* Test of JPA @SqlResultSetMapping
*/
public void testSqlResultSetMapping() {
NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
ClassLoaderResolver clr = nucleusCtx.getClassLoaderResolver(null);
MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
PersistenceUnitMetaData pumd = getMetaDataForPersistenceUnit(nucleusCtx, "JPATest");
metaDataMgr.loadPersistenceUnit(pumd, null);
ClassMetaData cmd = (ClassMetaData) metaDataMgr.getMetaDataForClass(LoginAccount.class.getName(), clr);
QueryResultMetaData[] queryResultMappings = cmd.getQueryResultMetaData();
assertNotNull("LoginAccount has no QueryResultMetaData!", queryResultMappings);
assertEquals("LoginAccount has incorrect number of query result mappings", 4, queryResultMappings.length);
// Example 1 : Returning 2 entities
QueryResultMetaData qrmd = null;
for (int i = 0; i < queryResultMappings.length; i++) {
QueryResultMetaData md = queryResultMappings[i];
if (md.getName().equals("AN_LOGIN_PLUS_ACCOUNT")) {
qrmd = md;
break;
}
}
if (qrmd == null) {
fail("SQL ResultSet mapping AN_LOGIN_PLUS_ACCOUNT is not present!");
}
String[] scalarCols = qrmd.getScalarColumns();
assertNull("LoginAccount sql mapping has incorrect scalar cols", scalarCols);
PersistentTypeMapping[] sqlMappingEntities = qrmd.getPersistentTypeMappings();
assertNotNull("LoginAccount sql mapping has incorrect entities", sqlMappingEntities);
assertEquals("LoginAccount sql mapping has incorrect number of entities", 2, sqlMappingEntities.length);
// LoginAccount
assertEquals("LoginAccount sql mapping entity 0 has incorrect class", LoginAccount.class.getName(), sqlMappingEntities[0].getClassName());
assertNull("LoginAccount sql mapping entity 0 has incorrect discriminator", sqlMappingEntities[0].getDiscriminatorColumn());
// Login
assertEquals("LoginAccount sql mapping entity 1 has incorrect class", Login.class.getName(), sqlMappingEntities[1].getClassName());
assertNull("LoginAccount sql mapping entity 1 has incorrect discriminator", sqlMappingEntities[1].getDiscriminatorColumn());
// Example 2 : Returning 2 scalars
qrmd = null;
for (int i = 0; i < queryResultMappings.length; i++) {
QueryResultMetaData md = queryResultMappings[i];
if (md.getName().equals("AN_ACCOUNT_NAMES")) {
qrmd = md;
break;
}
}
if (qrmd == null) {
fail("SQL ResultSet mapping AN_ACCOUNT_NAMES is not present!");
}
scalarCols = qrmd.getScalarColumns();
assertNotNull("LoginAccount sql mapping has incorrect scalar cols", scalarCols);
assertEquals("LoginAccount sql mapping has incorrect column name", "FIRSTNAME", scalarCols[0]);
assertEquals("LoginAccount sql mapping has incorrect column name", "LASTNAME", scalarCols[1]);
sqlMappingEntities = qrmd.getPersistentTypeMappings();
assertNull("LoginAccount sql mapping has incorrect entities", sqlMappingEntities);
}
use of org.datanucleus.samples.annotations.one_one.unidir.Login in project tests by datanucleus.
the class SQLQueryTest method testSQLResultConstructor.
/**
* Test of an SQL query using a result set mapping giving two entities (Login + LoginAccount) and
* using aliasing of columns and constructor result.
*/
public void testSQLResultConstructor() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
LoginAccount acct = new LoginAccount(1, "Fred", "Flintstone");
Login login = new Login("flintstone", "pwd");
acct.setLogin(login);
em.persist(login);
em.persist(acct);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
emf.getCache().evictAll();
em = getEM();
tx = em.getTransaction();
try {
tx.begin();
List result = em.createNativeQuery("SELECT P.FIRSTNAME AS FN, P.LASTNAME AS LN, L.USERNAME AS USER, L.PASSWORD AS PWD FROM " + "JPA_AN_LOGINACCOUNT P, JPA_AN_LOGIN L", "AN_LOGIN_PLUS_ACCOUNT_CONSTRUCTOR").getResultList();
assertEquals(1, result.size());
// Check the results
Iterator iter = result.iterator();
while (iter.hasNext()) {
// Should be a LoginAccountComplete
LoginAccountComplete acctCmp = (LoginAccountComplete) iter.next();
assertEquals("Fred", acctCmp.getFirstName());
assertEquals("Flintstone", acctCmp.getLastName());
assertEquals("flintstone", acctCmp.getUserName());
assertEquals("pwd", acctCmp.getPassword());
}
tx.rollback();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(LoginAccount.class);
clean(Login.class);
}
}
use of org.datanucleus.samples.annotations.one_one.unidir.Login in project tests by datanucleus.
the class SQLQueryTest method testSQLResult.
/**
* Test of an SQL query using a result set mapping giving two entities (Login + LoginAccount).
*/
public void testSQLResult() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
LoginAccount acct = new LoginAccount(1, "Fred", "Flintstone");
Login login = new Login("flintstone", "pwd");
acct.setLogin(login);
em.persist(login);
em.persist(acct);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
em = getEM();
tx = em.getTransaction();
try {
tx.begin();
// Check the results
List result = em.createNativeQuery("SELECT P.ID, P.FIRSTNAME, P.LASTNAME, P.LOGIN_ID, L.ID, L.USERNAME, L.PASSWORD " + "FROM JPA_AN_LOGINACCOUNT P, JPA_AN_LOGIN L", "AN_LOGIN_PLUS_ACCOUNT").getResultList();
assertEquals(1, result.size());
Iterator iter = result.iterator();
while (iter.hasNext()) {
// Should be a String (type of "ID" column)
Object[] obj = (Object[]) iter.next();
assertEquals("Fred", ((LoginAccount) obj[0]).getFirstName());
assertEquals("flintstone", ((Login) obj[1]).getUserName());
assertEquals("Fred", ((LoginAccount) obj[0]).getFirstName());
assertTrue(((LoginAccount) obj[0]).getLogin() == ((Login) obj[1]));
}
tx.rollback();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(LoginAccount.class);
clean(Login.class);
}
}
Aggregations