Search in sources :

Example 6 with Login

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

Example 7 with Login

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

Example 8 with Login

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

Example 9 with Login

use of org.datanucleus.samples.annotations.one_one.unidir.Login in project tests by datanucleus.

the class SQLQueryTest method testSQLResultEntity.

/**
 * Test of an SQL query using a result class as an Entity.
 */
public void testSQLResultEntity() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Login login = new Login("flintstone", "pwd");
            em.persist(login);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        em = getEM();
        tx = em.getTransaction();
        try {
            tx.begin();
            // Execute a query
            List<Login> result = em.createNativeQuery("SELECT * FROM JPA_AN_LOGIN", Login.class).getResultList();
            assertEquals(1, result.size());
            Login l = result.get(0);
            assertNotNull(l);
            // Check that the result entity is managed by this EntityManager
            assertTrue(em.contains(l));
            tx.rollback();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(Login.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Login(org.datanucleus.samples.annotations.one_one.unidir.Login)

Example 10 with Login

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

Aggregations

Login (org.datanucleus.samples.annotations.one_one.unidir.Login)10 EntityManager (javax.persistence.EntityManager)9 EntityTransaction (javax.persistence.EntityTransaction)9 LoginAccount (org.datanucleus.samples.annotations.one_one.unidir.LoginAccount)9 List (java.util.List)6 Iterator (java.util.Iterator)4 ArrayList (java.util.ArrayList)2 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)1 NucleusContext (org.datanucleus.NucleusContext)1 PersistenceNucleusContextImpl (org.datanucleus.PersistenceNucleusContextImpl)1 JPAMetaDataManager (org.datanucleus.api.jpa.metadata.JPAMetaDataManager)1 ClassMetaData (org.datanucleus.metadata.ClassMetaData)1 MetaDataManager (org.datanucleus.metadata.MetaDataManager)1 PersistenceUnitMetaData (org.datanucleus.metadata.PersistenceUnitMetaData)1 QueryResultMetaData (org.datanucleus.metadata.QueryResultMetaData)1 PersistentTypeMapping (org.datanucleus.metadata.QueryResultMetaData.PersistentTypeMapping)1 LoginAccountComplete (org.datanucleus.samples.annotations.one_one.unidir.LoginAccountComplete)1