use of org.jpox.samples.one_many.map_fk.MapFKHolder in project tests by datanucleus.
the class JPQLQueryTest method testMapFKWithKEYandVALUE.
/**
* Test for KEY and VALUE use with Map field.
*/
public void testMapFKWithKEYandVALUE() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
MapFKHolder holder = new MapFKHolder(1);
holder.setName("First Holder");
for (int i = 0; i < 3; i++) {
MapFKValue val1 = new MapFKValue("Key" + i, "Map value " + i, "Some description " + i);
val1.setHolder(holder);
val1.setId(100 + i);
holder.getMap().put(val1.getKey(), val1);
}
em.persist(holder);
em.flush();
/**
*This will generate:
*
*QueryCompilation:
* [result:PrimaryExpression{h.name},PrimaryExpression{m#VALUE.name}]
* [from:ClassExpression(alias=h join=
* JoinExpression{JOIN_LEFT_OUTER PrimaryExpression{h.map} alias=m})]
* [filter:DyadicExpression{PrimaryExpression{m#KEY} = Literal{Key1}}]
* [symbols:
* m#VALUE type=org.jpox.samples.one_many.map_fk.MapFKValue,
* h type=org.jpox.samples.one_many.map_fk.MapFKHolder,
* m#KEY type=java.lang.String,
* m type=org.jpox.samples.one_many.map_fk.MapFKValue]
*
*SELECT H."NAME",M."NAME"
*FROM JPA_MAP_FK_HOLDER H
*LEFT OUTER JOIN JPA_MAP_FK_VALUE M ON H.ID = M.HOLDER_ID
*WHERE M."KEY" = 'Key1'
*/
List<Object[]> results = em.createQuery("SELECT h.name, VALUE(m).name FROM " + MapFKHolder.class.getName() + " h LEFT OUTER JOIN h.map m WHERE KEY(m) = 'Key1'").getResultList();
assertNotNull(results);
assertEquals(1, results.size());
Object[] resultRow = results.get(0);
assertEquals(2, resultRow.length);
assertEquals("First Holder", resultRow[0]);
assertEquals("Map value 1", resultRow[1]);
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(MapFKValue.class);
clean(MapFKHolder.class);
}
}
use of org.jpox.samples.one_many.map_fk.MapFKHolder in project tests by datanucleus.
the class RelationshipTest method test1toNBidirFKMapWithInheritedValue.
/**
* Test case for 1-N bidir FK, using a Map<String, PC>.
*/
public void test1toNBidirFKMapWithInheritedValue() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object holderId = null;
// Check the persistence of owner and elements
try {
tx.begin();
// Create some data
MapFKHolder holder = new MapFKHolder("First");
MapFKValue value1 = new MapFKValue("Value 1", "First value", "1");
value1.setHolder(holder);
MapFKValue value2 = new MapFKValue("Value 2", "Second value", "2");
value2.setHolder(holder);
holder.getMap().put(value1.getKey(), value1);
holder.getMap().put(value2.getKey(), value2);
pm.makePersistent(holder);
tx.commit();
holderId = pm.getObjectId(holder);
} catch (Exception e) {
LOG.error("Exception persisting data", e);
fail("Exception thrown while creating 1-N (Map) bidir FK relationships with inherited value : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
pmf.getDataStoreCache().evictAll();
// Check the retrieval of the owner and values
tx = pm.currentTransaction();
try {
tx.begin();
MapFKHolder holder = (MapFKHolder) pm.getObjectById(holderId);
assertNotNull("Unable to retrieve container object for 1-N bidir FK relationship (Map)", holder);
// Force the retrieval of the entrySet
Collection<Map.Entry<String, MapFKValue>> mapEntries = holder.getMap().entrySet();
for (Map.Entry<String, MapFKValue> entry : mapEntries) {
String key = entry.getKey();
MapFKValue val = entry.getValue();
LOG.debug("Map key=" + key + " value=" + val);
}
Collection values = holder.getMap().values();
Iterator iter = values.iterator();
while (iter.hasNext()) {
iter.next();
}
assertEquals("Number of values in 1-N bidir FK relationship (Map) is wrong", 2, values.size());
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while querying 1-N bidir FK relationships (Map) : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
pm.close();
} finally {
// Clean out our data
clean(MapFKHolder.class);
clean(MapFKValue.class);
}
}
use of org.jpox.samples.one_many.map_fk.MapFKHolder in project tests by datanucleus.
the class MapTest method test1toNBidirFKMapWithInheritedValue.
public void test1toNBidirFKMapWithInheritedValue() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object holderId = null;
// Check the persistence of owner and elements
try {
tx.begin();
MapFKHolder holder = new MapFKHolder("First");
MapFKValue value1 = new MapFKValue("Value 1", "First value", "1");
value1.setHolder(holder);
MapFKValue value2 = new MapFKValue("Value 2", "Second value", "2");
value2.setHolder(holder);
holder.getMap().put(value1.getKey(), value1);
holder.getMap().put(value2.getKey(), value2);
pm.makePersistent(holder);
tx.commit();
holderId = pm.getObjectId(holder);
} catch (Exception e) {
LOG.error("Exception persisting data", e);
fail("Exception thrown while creating 1-N (Map) bidir FK relationships with inherited value : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pmf.getDataStoreCache().evictAll();
// Check the retrieval of the owner and values
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
MapFKHolder holder = (MapFKHolder) pm.getObjectById(holderId);
assertNotNull("Unable to retrieve container object for 1-N bidir FK relationship (Map)", holder);
assertEquals("Number of map entries is not correct", 2, holder.getMap().size());
// Force the retrieval of the entrySet
Collection<Map.Entry<String, MapFKValue>> mapEntries = holder.getMap().entrySet();
for (Map.Entry<String, MapFKValue> entry : mapEntries) {
String key = entry.getKey();
MapFKValue val = entry.getValue();
LOG.debug("Map key=" + key + " value=" + val);
}
Collection values = holder.getMap().values();
Iterator iter = values.iterator();
while (iter.hasNext()) {
iter.next();
}
assertEquals("Number of values in 1-N bidir FK relationship (Map) is wrong", 2, values.size());
tx.commit();
} catch (Exception e) {
LOG.error("Exception running test", e);
fail("Exception thrown while querying 1-N bidir FK relationships (Map) : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
pm.close();
} finally {
// Clean out our data
clean(MapFKHolder.class);
clean(MapFKValue.class);
}
}
Aggregations