Search in sources :

Example 6 with CompoundPkTestEntity

use of org.apache.cayenne.testdo.compound.CompoundPkTestEntity in project cayenne by apache.

the class DataContextEJBQLQueryCompoundIT method testSelectFromWhereNoMatchOnMultiColumnObject.

@Test
public void testSelectFromWhereNoMatchOnMultiColumnObject() throws Exception {
    createTwoCompoundPKTwoFK();
    Map<String, String> key1 = new HashMap<>();
    key1.put(CompoundPkTestEntity.KEY1_PK_COLUMN, "b1");
    key1.put(CompoundPkTestEntity.KEY2_PK_COLUMN, "b2");
    CompoundPkTestEntity a = Cayenne.objectForPK(context, CompoundPkTestEntity.class, key1);
    String ejbql = "select e from CompoundFkTestEntity e WHERE e.toCompoundPk <> :param";
    EJBQLQuery query = new EJBQLQuery(ejbql);
    query.setParameter("param", a);
    List<?> ps = context.performQuery(query);
    assertEquals(1, ps.size());
    CompoundFkTestEntity o1 = (CompoundFkTestEntity) ps.get(0);
    assertEquals(33001, Cayenne.intPKForObject(o1));
}
Also used : CompoundPkTestEntity(org.apache.cayenne.testdo.compound.CompoundPkTestEntity) HashMap(java.util.HashMap) EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) CompoundFkTestEntity(org.apache.cayenne.testdo.compound.CompoundFkTestEntity) Test(org.junit.Test)

Example 7 with CompoundPkTestEntity

use of org.apache.cayenne.testdo.compound.CompoundPkTestEntity in project cayenne by apache.

the class DataContextEJBQLUpdateCompoundIT method testUpdateNoQualifierToOneCompoundPK.

@Test
public void testUpdateNoQualifierToOneCompoundPK() throws Exception {
    createTwoCompoundPKTwoFK();
    Map<String, String> key1 = new HashMap<>();
    key1.put(CompoundPkTestEntity.KEY1_PK_COLUMN, "b1");
    key1.put(CompoundPkTestEntity.KEY2_PK_COLUMN, "b2");
    CompoundPkTestEntity object = Cayenne.objectForPK(context, CompoundPkTestEntity.class, key1);
    EJBQLQuery check = new EJBQLQuery("select count(e) from CompoundFkTestEntity e WHERE e.toCompoundPk <> :param");
    check.setParameter("param", object);
    Object notUpdated = Cayenne.objectForQuery(context, check);
    assertEquals(new Long(1l), notUpdated);
    String ejbql = "UPDATE CompoundFkTestEntity e SET e.toCompoundPk = :param";
    EJBQLQuery query = new EJBQLQuery(ejbql);
    query.setParameter("param", object);
    QueryResponse result = context.performGenericQuery(query);
    int[] count = result.firstUpdateCount();
    assertNotNull(count);
    assertEquals(1, count.length);
    assertEquals(2, count[0]);
    notUpdated = Cayenne.objectForQuery(context, check);
    assertEquals(new Long(0l), notUpdated);
}
Also used : CompoundPkTestEntity(org.apache.cayenne.testdo.compound.CompoundPkTestEntity) HashMap(java.util.HashMap) EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) QueryResponse(org.apache.cayenne.QueryResponse) Test(org.junit.Test)

Example 8 with CompoundPkTestEntity

use of org.apache.cayenne.testdo.compound.CompoundPkTestEntity in project cayenne by apache.

the class DataContextSQLTemplateCompoundIT method testBindObjectNotEqualCompound.

@Test
public void testBindObjectNotEqualCompound() throws Exception {
    createTwoCompoundPKsAndCompoundFKsDataSet();
    Map<String, String> pk = new HashMap<>();
    pk.put(CompoundPkTestEntity.KEY1_PK_COLUMN, "a1");
    pk.put(CompoundPkTestEntity.KEY2_PK_COLUMN, "a2");
    CompoundPkTestEntity a = Cayenne.objectForPK(context, CompoundPkTestEntity.class, pk);
    String template = "SELECT * FROM COMPOUND_FK_TEST t0" + " WHERE #bindObjectNotEqual($a [ 't0.F_KEY1', 't0.F_KEY2' ] [ 'KEY1', 'KEY2' ] ) ORDER BY PKEY";
    SQLTemplate query = new SQLTemplate(CompoundFkTestEntity.class, template);
    query.setColumnNamesCapitalization(CapsStrategy.UPPER);
    query.setParams(Collections.singletonMap("a", a));
    List<CompoundFkTestEntity> objects = context.performQuery(query);
    assertEquals(1, objects.size());
    CompoundFkTestEntity p = objects.get(0);
    assertEquals(7, Cayenne.intPKForObject(p));
}
Also used : CompoundPkTestEntity(org.apache.cayenne.testdo.compound.CompoundPkTestEntity) SQLTemplate(org.apache.cayenne.query.SQLTemplate) HashMap(java.util.HashMap) CompoundFkTestEntity(org.apache.cayenne.testdo.compound.CompoundFkTestEntity) Test(org.junit.Test)

Example 9 with CompoundPkTestEntity

use of org.apache.cayenne.testdo.compound.CompoundPkTestEntity in project cayenne by apache.

the class CompoundPkChangeIT method testCompoundPkChangeSingleElement.

@Test
public void testCompoundPkChangeSingleElement() throws Exception {
    CompoundPkTestEntity object = context.newObject(CompoundPkTestEntity.class);
    CompoundPkTestEntity refreshedObject = null;
    object.setKey1(key1v1);
    object.setKey2(key2v1);
    object.setName("testing testing");
    context.commitChanges();
    assertEquals(key1v1, object.getObjectId().getIdSnapshot().get(CompoundPkTestEntity.KEY1_PK_COLUMN));
    assertEquals(key2v1, object.getObjectId().getIdSnapshot().get(CompoundPkTestEntity.KEY2_PK_COLUMN));
    ObjectIdQuery refetch = new ObjectIdQuery(object.getObjectId(), false, ObjectIdQuery.CACHE_REFRESH);
    refreshedObject = (CompoundPkTestEntity) Cayenne.objectForQuery(context, refetch);
    assertEquals(object.getObjectId(), refreshedObject.getObjectId());
    object.setKey2(key2v2);
    context.commitChanges();
    assertEquals(key1v1, object.getObjectId().getIdSnapshot().get(CompoundPkTestEntity.KEY1_PK_COLUMN));
    assertEquals(key2v2, object.getObjectId().getIdSnapshot().get(CompoundPkTestEntity.KEY2_PK_COLUMN));
    ObjectIdQuery refetch1 = new ObjectIdQuery(object.getObjectId(), false, ObjectIdQuery.CACHE_REFRESH);
    refreshedObject = (CompoundPkTestEntity) Cayenne.objectForQuery(context, refetch1);
    assertEquals(object.getObjectId(), refreshedObject.getObjectId());
    object.setKey2(key2v3);
    context.commitChanges();
    assertEquals(key1v1, object.getObjectId().getIdSnapshot().get(CompoundPkTestEntity.KEY1_PK_COLUMN));
    assertEquals(key2v3, object.getObjectId().getIdSnapshot().get(CompoundPkTestEntity.KEY2_PK_COLUMN));
    ObjectIdQuery refetch2 = new ObjectIdQuery(object.getObjectId(), false, ObjectIdQuery.CACHE_REFRESH);
    refreshedObject = (CompoundPkTestEntity) Cayenne.objectForQuery(context, refetch2);
    assertEquals(object.getObjectId(), refreshedObject.getObjectId());
}
Also used : CompoundPkTestEntity(org.apache.cayenne.testdo.compound.CompoundPkTestEntity) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Test(org.junit.Test)

Example 10 with CompoundPkTestEntity

use of org.apache.cayenne.testdo.compound.CompoundPkTestEntity in project cayenne by apache.

the class CompoundPkChangeIT method testCompoundPkChangeAllElements.

@Test
public void testCompoundPkChangeAllElements() throws Exception {
    CompoundPkTestEntity object = context.newObject(CompoundPkTestEntity.class);
    CompoundPkTestEntity refreshedObject = null;
    object.setKey1(key1v1);
    object.setKey2(key2v1);
    object.setName("testing testing");
    context.commitChanges();
    assertEquals(key1v1, object.getObjectId().getIdSnapshot().get(CompoundPkTestEntity.KEY1_PK_COLUMN));
    assertEquals(key2v1, object.getObjectId().getIdSnapshot().get(CompoundPkTestEntity.KEY2_PK_COLUMN));
    ObjectIdQuery refetch = new ObjectIdQuery(object.getObjectId(), false, ObjectIdQuery.CACHE_REFRESH);
    refreshedObject = (CompoundPkTestEntity) Cayenne.objectForQuery(context, refetch);
    assertEquals(object.getObjectId(), refreshedObject.getObjectId());
    object.setKey1(key1v2);
    object.setKey2(key2v2);
    context.commitChanges();
    assertEquals(key1v2, object.getObjectId().getIdSnapshot().get(CompoundPkTestEntity.KEY1_PK_COLUMN));
    assertEquals(key2v2, object.getObjectId().getIdSnapshot().get(CompoundPkTestEntity.KEY2_PK_COLUMN));
    ObjectIdQuery refetch1 = new ObjectIdQuery(object.getObjectId(), false, ObjectIdQuery.CACHE_REFRESH);
    refreshedObject = (CompoundPkTestEntity) Cayenne.objectForQuery(context, refetch1);
    assertEquals(object.getObjectId(), refreshedObject.getObjectId());
    object.setKey1(key1v3);
    object.setKey2(key2v3);
    context.commitChanges();
    assertEquals(key1v3, object.getObjectId().getIdSnapshot().get(CompoundPkTestEntity.KEY1_PK_COLUMN));
    assertEquals(key2v3, object.getObjectId().getIdSnapshot().get(CompoundPkTestEntity.KEY2_PK_COLUMN));
    ObjectIdQuery refetch2 = new ObjectIdQuery(object.getObjectId(), false, ObjectIdQuery.CACHE_REFRESH);
    refreshedObject = (CompoundPkTestEntity) Cayenne.objectForQuery(context, refetch2);
    assertEquals(object.getObjectId(), refreshedObject.getObjectId());
}
Also used : CompoundPkTestEntity(org.apache.cayenne.testdo.compound.CompoundPkTestEntity) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Test(org.junit.Test)

Aggregations

CompoundPkTestEntity (org.apache.cayenne.testdo.compound.CompoundPkTestEntity)11 Test (org.junit.Test)11 CompoundFkTestEntity (org.apache.cayenne.testdo.compound.CompoundFkTestEntity)8 HashMap (java.util.HashMap)6 EJBQLQuery (org.apache.cayenne.query.EJBQLQuery)4 SelectQuery (org.apache.cayenne.query.SelectQuery)3 Expression (org.apache.cayenne.exp.Expression)2 ObjectIdQuery (org.apache.cayenne.query.ObjectIdQuery)2 SQLTemplate (org.apache.cayenne.query.SQLTemplate)2 QueryResponse (org.apache.cayenne.QueryResponse)1