Search in sources :

Example 1 with MapJoinHolder

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

the class JPQLQueryTest method testMapJoinNonPCWithKEYandVALUE.

/**
 * Test for KEY and VALUE use with Map<Integer, String> field.
 */
public void testMapJoinNonPCWithKEYandVALUE() {
    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(2);
            holder.setName("First Holder");
            for (int i = 0; i < 3; i++) {
                holder.getMap2().put(i, "Val" + i);
            }
            em.persist(holder);
            em.flush();
            /**
             *This will generate
             *
             *QueryCompilation:
             *  [result:PrimaryExpression{m2#VALUE}]
             *  [from:ClassExpression(alias=h join=JoinExpression{JOIN_LEFT_OUTER PrimaryExpression{h.map2} alias=m2 on=DyadicExpression{PrimaryExpression{m2#KEY}  =  ParameterExpression{key}}})]
             *  [symbols:
             *    m2#KEY type=java.lang.Integer,
             *    m2 type=org.datanucleus.samples.annotations.one_many.map_join.MapJoinHolder,
             *    h type=org.datanucleus.samples.annotations.one_many.map_join.MapJoinHolder,
             *    m2#VALUE type=java.lang.String, key type=java.lang.Integer]
             *
             *SELECT M2.MAP2_VALUE
             *FROM JPA_AN_MAPJOINHOLDER H
             *LEFT OUTER JOIN JPA_AN_MAPJOINHOLDER_MAP2 M2 ON H.ID = M2.MAPJOINHOLDER_ID AND M2.MAP2_KEY = <1>
             */
            Query q = em.createQuery("SELECT VALUE(m2) FROM MapJoinHolder h LEFT JOIN h.map2 m2 ON KEY(m2) = :key");
            q.setParameter("key", new Integer(1));
            List results = q.getResultList();
            assertNotNull(results);
            assertEquals(1, results.size());
            String resultVal = (String) results.get(0);
            assertEquals("Val1", resultVal);
            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);
    }
}
Also used : MapJoinHolder(org.datanucleus.samples.annotations.one_many.map_join.MapJoinHolder) EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) JPAQuery(org.datanucleus.api.jpa.JPAQuery) Query(javax.persistence.Query) TypedQuery(javax.persistence.TypedQuery) PersistenceException(javax.persistence.PersistenceException) List(java.util.List) ArrayList(java.util.ArrayList)

Example 2 with MapJoinHolder

use of org.datanucleus.samples.annotations.one_many.map_join.MapJoinHolder 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 3 with MapJoinHolder

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

Example 4 with MapJoinHolder

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

the class JPQLQueryTest method testMapJoinEmbeddableWithKEYandVALUE.

/**
 * Test for KEY and VALUE use with Map<String, {embedded}> field.
 */
public void testMapJoinEmbeddableWithKEYandVALUE() {
    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(3);
            holder.setName("First Holder");
            for (int i = 0; i < 3; i++) {
                MapJoinEmbeddedValue val = new MapJoinEmbeddedValue("Name" + i, "Description" + i);
                holder.getMap3().put("Key" + i, val);
            }
            em.persist(holder);
            em.flush();
            /**
             *This will generate:
             *
             *QueryCompilation:
             *  [result:PrimaryExpression{m3#VALUE}]
             *  [from:ClassExpression(alias=h
             *    join=JoinExpression{JOIN_LEFT_OUTER PrimaryExpression{h.map3} alias=m3
             *    on=DyadicExpression{PrimaryExpression{m3#KEY}  =  ParameterExpression{key}}})]
             *  [symbols:
             *    m3 type=org.datanucleus.samples.annotations.one_many.map_join.MapJoinEmbeddedValue,
             *    m3#VALUE type=org.datanucleus.samples.annotations.one_many.map_join.MapJoinEmbeddedValue,
             *    h type=org.datanucleus.samples.annotations.one_many.map_join.MapJoinHolder,
             *    m3#KEY type=java.lang.String,
             *    key type=unknown]
             *
             *SELECT M3.DESCRIPTION,M3."NAME"
             *FROM JPA_AN_MAPJOINHOLDER H
             *LEFT OUTER JOIN JPA_AN_MAPJOINHOLDER_MAP3 M3 ON H.ID = M3.MAPJOINHOLDER_ID AND M3.MAP3_KEY = ?
             */
            TypedQuery<MapJoinEmbeddedValue> q = em.createQuery("SELECT VALUE(m3) FROM MapJoinHolder h LEFT JOIN h.map3 m3 ON KEY(m3) = :key", MapJoinEmbeddedValue.class);
            q.setParameter("key", "Key1");
            List<MapJoinEmbeddedValue> results = q.getResultList();
            assertNotNull(results);
            assertEquals(1, results.size());
            MapJoinEmbeddedValue resultVal = results.get(0);
            assertEquals("Name1", resultVal.getName());
            assertEquals("Description1", resultVal.getDescription());
            // Try access to a field of the embedded value
            Query q2 = em.createQuery("SELECT VALUE(m3).name FROM MapJoinHolder h LEFT JOIN h.map3 m3 ON KEY(m3) = :key");
            q2.setParameter("key", "Key1");
            List results2 = q2.getResultList();
            assertNotNull(results2);
            assertEquals(1, results2.size());
            assertEquals("Name1", (String) results2.get(0));
            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);
    }
}
Also used : MapJoinHolder(org.datanucleus.samples.annotations.one_many.map_join.MapJoinHolder) EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) MapJoinEmbeddedValue(org.datanucleus.samples.annotations.one_many.map_join.MapJoinEmbeddedValue) JPAQuery(org.datanucleus.api.jpa.JPAQuery) Query(javax.persistence.Query) TypedQuery(javax.persistence.TypedQuery) PersistenceException(javax.persistence.PersistenceException) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

EntityManager (javax.persistence.EntityManager)4 EntityTransaction (javax.persistence.EntityTransaction)4 PersistenceException (javax.persistence.PersistenceException)4 MapJoinHolder (org.datanucleus.samples.annotations.one_many.map_join.MapJoinHolder)4 Query (javax.persistence.Query)3 TypedQuery (javax.persistence.TypedQuery)3 JPAQuery (org.datanucleus.api.jpa.JPAQuery)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 MapJoinValue (org.datanucleus.samples.annotations.one_many.map_join.MapJoinValue)2 MapJoinEmbeddedValue (org.datanucleus.samples.annotations.one_many.map_join.MapJoinEmbeddedValue)1 MapJoinKey (org.datanucleus.samples.annotations.one_many.map_join.MapJoinKey)1