use of org.datanucleus.samples.annotations.one_one.unidir.LoginAccount in project tests by datanucleus.
the class SQLQueryTest method testSQLResultAliased.
/**
* Test of an SQL query using a result set mapping giving two entities (Login + LoginAccount) and
* using aliasing of columns.
*/
public void testSQLResultAliased() {
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.ID AS THISID, P.FIRSTNAME AS FN, P.LASTNAME, P.LOGIN_ID, " + "L.ID AS IDLOGIN, L.USERNAME AS UN, L.PASSWORD FROM " + "JPA_AN_LOGINACCOUNT P, JPA_AN_LOGIN L", "AN_LOGIN_PLUS_ACCOUNT_ALIAS").getResultList();
assertEquals(1, result.size());
// Check the results
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", ((LoginAccount) obj[0]).getLastName());
assertEquals("flintstone", ((Login) obj[1]).getUserName());
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);
}
}
use of org.datanucleus.samples.annotations.one_one.unidir.LoginAccount in project tests by datanucleus.
the class JPQLQueryTest method testLeftOuterJoinQuery.
/**
* Test for Left Outer Join.
*/
public void testLeftOuterJoinQuery() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
LoginAccount acct = new LoginAccount(1, "Fred", "Flintstone");
Login login = new Login("fred", "yabbadabbadoo");
acct.setLogin(login);
em.persist(acct);
em.flush();
List result = em.createQuery("SELECT A FROM " + LoginAccount.class.getName() + " A LEFT OUTER JOIN A.login L WHERE L.userName = 'fred'").getResultList();
assertEquals(1, result.size());
tx.rollback();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(Login.class);
clean(LoginAccount.class);
}
}
use of org.datanucleus.samples.annotations.one_one.unidir.LoginAccount in project tests by datanucleus.
the class JPQLQueryTest method testLeftOuterJoinOnQuery.
/**
* Test for Left Outer Join with additional ON.
*/
public void testLeftOuterJoinOnQuery() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
LoginAccount acct = new LoginAccount(1, "Fred", "Flintstone");
Login login = new Login("fred", "yabbadabbadoo");
acct.setLogin(login);
em.persist(acct);
em.flush();
List result = em.createQuery("SELECT A FROM " + LoginAccount.class.getName() + " A LEFT OUTER JOIN A.login L ON L.userName = 'fred'").getResultList();
assertEquals(1, result.size());
tx.rollback();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(Login.class);
clean(LoginAccount.class);
}
}
use of org.datanucleus.samples.annotations.one_one.unidir.LoginAccount in project tests by datanucleus.
the class SQLQueryTest method testSQLResultAliased2.
/**
* Test of an SQL query using a result set mapping giving two entities (Login + LoginAccount) and
* using aliasing of columns.
*/
public void testSQLResultAliased2() {
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.ID AS THISID, P.FIRSTNAME AS FN, P.LASTNAME AS LN, P.LOGIN_ID AS LID, " + "L.ID, L.USERNAME, L.PASSWORD FROM " + "JPA_AN_LOGINACCOUNT P, JPA_AN_LOGIN L", "AN_LOGIN_PLUS_ACCOUNT_ALIAS2").getResultList();
assertEquals(1, result.size());
// Check the results
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", ((LoginAccount) obj[0]).getLastName());
assertEquals("flintstone", ((Login) obj[1]).getUserName());
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