Search in sources :

Example 1 with LoginAccountComplete

use of org.datanucleus.samples.annotations.one_one.unidir.LoginAccountComplete 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);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) LoginAccount(org.datanucleus.samples.annotations.one_one.unidir.LoginAccount) LoginAccountComplete(org.datanucleus.samples.annotations.one_one.unidir.LoginAccountComplete) Iterator(java.util.Iterator) List(java.util.List) Login(org.datanucleus.samples.annotations.one_one.unidir.Login)

Aggregations

Iterator (java.util.Iterator)1 List (java.util.List)1 EntityManager (javax.persistence.EntityManager)1 EntityTransaction (javax.persistence.EntityTransaction)1 Login (org.datanucleus.samples.annotations.one_one.unidir.Login)1 LoginAccount (org.datanucleus.samples.annotations.one_one.unidir.LoginAccount)1 LoginAccountComplete (org.datanucleus.samples.annotations.one_one.unidir.LoginAccountComplete)1