Search in sources :

Example 1 with MapJoinKey

use of org.datanucleus.samples.annotations.one_many.map_join.MapJoinKey in project tests by datanucleus.

the class JPQLQueryTest method testMapJoinWithKEYandVALUEInFROM.

/**
 * Test for KEY and VALUE use with Map field in the FROM clause.
 */
public void testMapJoinWithKEYandVALUEInFROM() {
    if (!storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_EMBEDDED_MAP)) {
        return;
    }
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            MapJoinHolder holder = new MapJoinHolder(1);
            holder.setName("First Holder");
            for (int i = 0; i < 3; i++) {
                MapJoinKey key = new MapJoinKey(i, "Map key " + i, "Key description " + i);
                MapJoinValue val = new MapJoinValue(i, "Map value " + i, "Value description " + i);
                holder.getMap4().put(key, val);
            }
            em.persist(holder);
            em.flush();
            /**
             *This will generate
             *QueryCompilation:
             *  [result:PrimaryExpression{k.description},PrimaryExpression{v.description}]
             *  [from:ClassExpression(alias=h join=
             *    JoinExpression{JOIN_LEFT_OUTER PrimaryExpression{h.map4#VALUE} alias=v join=
             *    JoinExpression{JOIN_LEFT_OUTER PrimaryExpression{v#KEY} alias=k}})]
             *  [symbols:
             *    v type=org.datanucleus.samples.annotations.one_many.map_join.MapJoinValue,
             *    v#VALUE type=org.datanucleus.samples.annotations.one_many.map_join.MapJoinValue,
             *    h type=org.datanucleus.samples.annotations.one_many.map_join.MapJoinHolder,
             *    k type=org.datanucleus.samples.annotations.one_many.map_join.MapJoinKey,
             *    v#KEY type=org.datanucleus.samples.annotations.one_many.map_join.MapJoinKey]
             *
             *SELECT K.DESCRIPTION,V.DESCRIPTION
             *FROM JPA_AN_MAPJOINHOLDER H
             *LEFT OUTER JOIN JPA_AN_MAPJOINHOLDER_MAP4 V_MAP ON H.ID = V_MAP.MAPJOINHOLDER_ID
             *LEFT OUTER JOIN JPA_AN_MAPJOINVALUE V ON V_MAP.MAP4_ID = V.ID
             *LEFT OUTER JOIN JPA_AN_MAPJOINKEY K ON V_MAP.MAP4_KEY = K.ID
             */
            Query q = em.createQuery("SELECT k.description, v.description FROM MapJoinHolder h LEFT JOIN VALUE(h.map4) v LEFT JOIN KEY(v) k");
            List<Object[]> results = q.getResultList();
            assertNotNull(results);
            assertEquals(3, results.size());
            /**
             *This will generate:
             *
             *QueryCompilation:
             *  [result:PrimaryExpression{k.description},PrimaryExpression{v.description}]
             *  [from:ClassExpression(alias=h join=
             *    JoinExpression{JOIN_LEFT_OUTER PrimaryExpression{h.map4} alias=v join=
             *    JoinExpression{JOIN_LEFT_OUTER PrimaryExpression{v#KEY} alias=k}})]
             *  [symbols:
             *    v type=org.datanucleus.samples.annotations.one_many.map_join.MapJoinValue,
             *    v#VALUE type=org.datanucleus.samples.annotations.one_many.map_join.MapJoinValue,
             *    h type=org.datanucleus.samples.annotations.one_many.map_join.MapJoinHolder,
             *    k type=org.datanucleus.samples.annotations.one_many.map_join.MapJoinKey,
             *    v#KEY type=org.datanucleus.samples.annotations.one_many.map_join.MapJoinKey]
             *
             *SELECT K.DESCRIPTION,V.DESCRIPTION
             *FROM JPA_AN_MAPJOINHOLDER H
             *LEFT OUTER JOIN JPA_AN_MAPJOINHOLDER_MAP4 V_MAP ON H.ID = V_MAP.MAPJOINHOLDER_ID
             *LEFT OUTER JOIN JPA_AN_MAPJOINVALUE V ON V_MAP.MAP4_ID = V.ID
             *LEFT OUTER JOIN JPA_AN_MAPJOINKEY K ON V_MAP.MAP4_KEY = K.ID
             */
            Query q2 = em.createQuery("SELECT k.description, v.description FROM MapJoinHolder h LEFT JOIN h.map4 v LEFT JOIN KEY(v) k");
            List<Object[]> results2 = q2.getResultList();
            assertNotNull(results2);
            assertEquals(3, results2.size());
            tx.rollback();
        } catch (PersistenceException e) {
            LOG.error("Exception in test", e);
            fail("Exception in test : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(MapJoinHolder.class);
        clean(MapJoinValue.class);
        clean(MapJoinKey.class);
    }
}
Also used : MapJoinHolder(org.datanucleus.samples.annotations.one_many.map_join.MapJoinHolder) EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) MapJoinKey(org.datanucleus.samples.annotations.one_many.map_join.MapJoinKey) MapJoinValue(org.datanucleus.samples.annotations.one_many.map_join.MapJoinValue) JPAQuery(org.datanucleus.api.jpa.JPAQuery) Query(javax.persistence.Query) TypedQuery(javax.persistence.TypedQuery) PersistenceException(javax.persistence.PersistenceException)

Aggregations

EntityManager (javax.persistence.EntityManager)1 EntityTransaction (javax.persistence.EntityTransaction)1 PersistenceException (javax.persistence.PersistenceException)1 Query (javax.persistence.Query)1 TypedQuery (javax.persistence.TypedQuery)1 JPAQuery (org.datanucleus.api.jpa.JPAQuery)1 MapJoinHolder (org.datanucleus.samples.annotations.one_many.map_join.MapJoinHolder)1 MapJoinKey (org.datanucleus.samples.annotations.one_many.map_join.MapJoinKey)1 MapJoinValue (org.datanucleus.samples.annotations.one_many.map_join.MapJoinValue)1