Search in sources :

Example 1 with MapJoinValue

use of org.datanucleus.samples.annotations.one_many.map_join.MapJoinValue 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)

Example 2 with MapJoinValue

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

the class JPQLQueryTest method testMapJoinWithKEYandVALUE.

/**
 * Test for KEY and VALUE use with Map field.
 */
public void testMapJoinWithKEYandVALUE() {
    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++) {
                MapJoinValue val = new MapJoinValue(i, "Map value " + i, "Some description " + i);
                holder.getMap().put("Key" + i, val);
            }
            em.persist(holder);
            em.flush();
            /**
             *This will generate:
             *
             *QueryCompilation:
             *  [result:PrimaryExpression{m#VALUE}]
             *  [from:ClassExpression(alias=h join=JoinExpression{JOIN_LEFT_OUTER PrimaryExpression{h.map} alias=m on=DyadicExpression{PrimaryExpression{m#KEY}  =  ParameterExpression{key}}})]
             *  [symbols:
             *    m#VALUE type=org.datanucleus.samples.annotations.one_many.map_join.MapJoinValue,
             *    h type=org.datanucleus.samples.annotations.one_many.map_join.MapJoinHolder,
             *    m#KEY type=java.lang.String,
             *    m type=org.datanucleus.samples.annotations.one_many.map_join.MapJoinValue,
             *    key type=java.lang.String]
             *
             *SELECT M.DESCRIPTION,M.ID,M."NAME"
             *FROM JPA_AN_MAPJOINHOLDER H
             *LEFT OUTER JOIN JPA_AN_MAPJOINHOLDER_MAP M_MAP ON H.ID = M_MAP.MAPJOINHOLDER_ID
             *LEFT OUTER JOIN JPA_AN_MAPJOINVALUE M ON M_MAP.MAP_ID = M.ID AND M_MAP.MAP_KEY = ?
             */
            TypedQuery<MapJoinValue> q = em.createQuery("SELECT VALUE(m) AS x FROM MapJoinHolder h LEFT JOIN h.map m ON KEY(m) = :key", MapJoinValue.class);
            q.setParameter("key", "Key2");
            List<MapJoinValue> results = q.getResultList();
            assertNotNull(results);
            // TODO This should be 1, but we need to apply the ON clause on the join to the join table, not to the value table [rdbms-177]
            assertEquals(3, results.size());
            /*                MapJoinValue resultVal = results.get(0);
                assertEquals("Map value 2", resultVal.getName());
                assertEquals("Some description 2", resultVal.getDescription());*/
            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);
    }
}
Also used : MapJoinHolder(org.datanucleus.samples.annotations.one_many.map_join.MapJoinHolder) EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) MapJoinValue(org.datanucleus.samples.annotations.one_many.map_join.MapJoinValue) PersistenceException(javax.persistence.PersistenceException)

Aggregations

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