Search in sources :

Example 1 with DbEntity

use of org.apache.cayenne.map.DbEntity in project cayenne by apache.

the class EntityIdCoderTest method testSingleLongPk.

@Test
public void testSingleLongPk() {
    DbEntity dbEntity = new DbEntity("X");
    DbAttribute pk = new DbAttribute("ID");
    pk.setType(Types.BIGINT);
    pk.setPrimaryKey(true);
    dbEntity.addAttribute(pk);
    ObjEntity entity = mock(ObjEntity.class);
    when(entity.getName()).thenReturn("x");
    when(entity.getDbEntityName()).thenReturn(dbEntity.getName());
    when(entity.getDbEntity()).thenReturn(dbEntity);
    ObjectId id = new ObjectId("x", "ID", 3L);
    EntityIdCoder coder = new EntityIdCoder(entity);
    assertEquals("x:3", coder.toStringId(id));
    ObjectId parsedId = coder.toObjectId("x:3");
    assertEquals(id, parsedId);
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) ObjectId(org.apache.cayenne.ObjectId) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

Example 2 with DbEntity

use of org.apache.cayenne.map.DbEntity in project cayenne by apache.

the class EntityIdCoderTest method testSingleIntPk.

@Test
public void testSingleIntPk() {
    DbEntity dbEntity = new DbEntity("X");
    DbAttribute pk = new DbAttribute("ID");
    pk.setType(Types.INTEGER);
    pk.setPrimaryKey(true);
    dbEntity.addAttribute(pk);
    ObjEntity entity = mock(ObjEntity.class);
    when(entity.getName()).thenReturn("x");
    when(entity.getDbEntityName()).thenReturn(dbEntity.getName());
    when(entity.getDbEntity()).thenReturn(dbEntity);
    ObjectId id = new ObjectId("x", "ID", 3);
    EntityIdCoder coder = new EntityIdCoder(entity);
    assertEquals("x:3", coder.toStringId(id));
    ObjectId parsedId = coder.toObjectId("x:3");
    assertEquals(id, parsedId);
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) ObjectId(org.apache.cayenne.ObjectId) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

Example 3 with DbEntity

use of org.apache.cayenne.map.DbEntity in project cayenne by apache.

the class EntityIdCoderTest method testSingleStringPk.

@Test
public void testSingleStringPk() {
    DbEntity dbEntity = new DbEntity("X");
    DbAttribute pk = new DbAttribute("ID");
    pk.setType(Types.VARCHAR);
    pk.setPrimaryKey(true);
    dbEntity.addAttribute(pk);
    ObjEntity entity = mock(ObjEntity.class);
    when(entity.getName()).thenReturn("x");
    when(entity.getDbEntityName()).thenReturn(dbEntity.getName());
    when(entity.getDbEntity()).thenReturn(dbEntity);
    EntityIdCoder coder = new EntityIdCoder(entity);
    ObjectId id = new ObjectId("x", "ID", "AbC");
    assertEquals("x:AbC", coder.toStringId(id));
    ObjectId parsedId = coder.toObjectId("x:AbC");
    assertEquals(id, parsedId);
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) ObjectId(org.apache.cayenne.ObjectId) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

Example 4 with DbEntity

use of org.apache.cayenne.map.DbEntity in project cayenne by apache.

the class EntityIdCoderTest method testIdEncoding.

@Test
public void testIdEncoding() {
    DbEntity dbEntity = new DbEntity("X");
    DbAttribute pk = new DbAttribute("ID");
    pk.setType(Types.VARCHAR);
    pk.setPrimaryKey(true);
    dbEntity.addAttribute(pk);
    ObjEntity entity = mock(ObjEntity.class);
    when(entity.getName()).thenReturn("x");
    when(entity.getDbEntityName()).thenReturn(dbEntity.getName());
    when(entity.getDbEntity()).thenReturn(dbEntity);
    EntityIdCoder coder = new EntityIdCoder(entity);
    ObjectId id = new ObjectId("x", "ID", "Ab:C");
    assertEquals("x:Ab%3AC", coder.toStringId(id));
    ObjectId parsedId = coder.toObjectId("x:Ab%3AC");
    assertEquals(id, parsedId);
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) ObjectId(org.apache.cayenne.ObjectId) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

Example 5 with DbEntity

use of org.apache.cayenne.map.DbEntity in project cayenne by apache.

the class DataDomainFlushAction method runQueries.

private void runQueries() {
    DataDomainFlushObserver observer = new DataDomainFlushObserver(domain.getJdbcEventLogger());
    try {
        DataNode lastNode = null;
        DbEntity lastEntity = null;
        int rangeStart = 0;
        int len = queries.size();
        for (int i = 0; i < len; i++) {
            BatchQuery query = (BatchQuery) queries.get(i);
            if (query.getDbEntity() != lastEntity) {
                lastEntity = query.getDbEntity();
                DataNode node = domain.lookupDataNode(lastEntity.getDataMap());
                if (node != lastNode) {
                    if (i - rangeStart > 0) {
                        lastNode.performQueries(queries.subList(rangeStart, i), observer);
                    }
                    rangeStart = i;
                    lastNode = node;
                }
            }
        }
        // process last segment of the query list...
        lastNode.performQueries(queries.subList(rangeStart, len), observer);
    } catch (Throwable th) {
        BaseTransaction.getThreadTransaction().setRollbackOnly();
        throw new CayenneRuntimeException("Transaction was rolledback.", th);
    }
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) BatchQuery(org.apache.cayenne.query.BatchQuery)

Aggregations

DbEntity (org.apache.cayenne.map.DbEntity)273 DbAttribute (org.apache.cayenne.map.DbAttribute)106 Test (org.junit.Test)106 ObjEntity (org.apache.cayenne.map.ObjEntity)64 DbRelationship (org.apache.cayenne.map.DbRelationship)55 DataMap (org.apache.cayenne.map.DataMap)47 ObjAttribute (org.apache.cayenne.map.ObjAttribute)26 ArrayList (java.util.ArrayList)25 DbJoin (org.apache.cayenne.map.DbJoin)24 MergerToken (org.apache.cayenne.dbsync.merge.token.MergerToken)20 ObjRelationship (org.apache.cayenne.map.ObjRelationship)19 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)16 JdbcAdapter (org.apache.cayenne.dba.JdbcAdapter)16 Entity (org.apache.cayenne.map.Entity)16 List (java.util.List)15 DbAdapter (org.apache.cayenne.dba.DbAdapter)15 EntityEvent (org.apache.cayenne.map.event.EntityEvent)14 HashMap (java.util.HashMap)12 SelectQuery (org.apache.cayenne.query.SelectQuery)12 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)11