Search in sources :

Example 1 with MapJoinEmbeddedValue

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

ArrayList (java.util.ArrayList)1 List (java.util.List)1 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 MapJoinEmbeddedValue (org.datanucleus.samples.annotations.one_many.map_join.MapJoinEmbeddedValue)1 MapJoinHolder (org.datanucleus.samples.annotations.one_many.map_join.MapJoinHolder)1