Search in sources :

Example 76 with DbAttribute

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

the class IdentityColumnsIT method testGeneratedJoinInFlattenedRelationship.

@Test
public void testGeneratedJoinInFlattenedRelationship() throws Exception {
    // before saving objects, let's manually access PKGenerator to get a
    // base PK value
    // for comparison
    DbEntity joinTableEntity = context.getEntityResolver().getDbEntity(joinTable.getTableName());
    DbAttribute pkAttribute = joinTableEntity.getAttribute("ID");
    Number pk = (Number) adapter.getPkGenerator().generatePk(node, pkAttribute);
    GeneratedF1 f1 = context.newObject(GeneratedF1.class);
    GeneratedF2 f2 = context.newObject(GeneratedF2.class);
    f1.addToF2(f2);
    context.commitChanges();
    int id = joinTable.getInt("ID");
    assertTrue(id > 0);
    // base value
    if (adapter.supportsGeneratedKeys()) {
        assertFalse("Looks like auto-increment wasn't used for the join table. ID: " + id, id == pk.intValue() + 1);
    } else {
        assertEquals(id, pk.intValue() + 1);
    }
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) GeneratedF1(org.apache.cayenne.testdo.generated.GeneratedF1) GeneratedF2(org.apache.cayenne.testdo.generated.GeneratedF2) Test(org.junit.Test)

Example 77 with DbAttribute

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

the class DbEntityHandlerTest method testLoad.

@Test
public void testLoad() throws Exception {
    final DataMap map = new DataMap();
    assertTrue(map.getDbEntities().isEmpty());
    parse("db-entity", new HandlerFactory() {

        @Override
        public NamespaceAwareNestedTagHandler createHandler(NamespaceAwareNestedTagHandler parent) {
            return new DbEntityHandler(parent, map);
        }
    });
    assertEquals(1, map.getDbEntities().size());
    DbEntity entity = map.getDbEntity("ARTGROUP");
    assertNotNull(entity);
    assertNull(entity.getPrimaryKeyGenerator());
    assertEquals(3, entity.getAttributes().size());
    assertEquals("catalog", entity.getCatalog());
    assertEquals("schema", entity.getSchema());
    assertEquals("name = \"test\"", entity.getQualifier().toString());
    DbAttribute attribute = entity.getAttribute("GROUP_ID");
    assertNotNull(attribute);
    assertTrue(attribute.isMandatory());
    assertTrue(attribute.isPrimaryKey());
    assertTrue(attribute.isGenerated());
    assertEquals(Types.INTEGER, attribute.getType());
    attribute = entity.getAttribute("NAME");
    assertNotNull(attribute);
    assertTrue(attribute.isMandatory());
    assertFalse(attribute.isPrimaryKey());
    assertFalse(attribute.isGenerated());
    assertEquals(100, attribute.getMaxLength());
    assertEquals(Types.VARCHAR, attribute.getType());
    attribute = entity.getAttribute("PARENT_GROUP_ID");
    assertNotNull(attribute);
    assertFalse(attribute.isMandatory());
    assertFalse(attribute.isPrimaryKey());
    assertFalse(attribute.isGenerated());
    assertEquals(10, attribute.getScale());
    assertEquals(Types.BIT, attribute.getType());
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 78 with DbAttribute

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

the class XMLDataMapLoaderTest method loadFullDataMap.

@Test
public void loadFullDataMap() {
    URL url = getClass().getResource("testConfigMap4.map.xml");
    DataMap map = loader.load(new URLResource(url));
    assertNotNull(map);
    assertEquals("testConfigMap4", map.getName());
    // check general state
    assertEquals(12, map.getDbEntities().size());
    assertEquals(17, map.getObjEntities().size());
    assertEquals(4, map.getProcedures().size());
    assertEquals(14, map.getQueryDescriptors().size());
    assertEquals(1, map.getEmbeddables().size());
    assertEquals("TEST_CATALOG", map.getDefaultCatalog());
    assertNull(map.getDefaultSchema());
    assertEquals("org.apache.cayenne.testdo.testmap", map.getDefaultPackage());
    assertTrue(map.isClientSupported());
    // check some loaded content
    assertEquals("org.apache.cayenne.testdo.testmap.Artist", map.getObjEntity("Artist").getClassName());
    assertEquals(5, map.getObjEntity("CompoundPainting").getAttributes().size());
    assertEquals(3, map.getObjEntity("Artist").getRelationships().size());
    assertEquals(7, map.getObjEntity("ArtistCallback").getCallbackMethods().size());
    assertEquals("name = \"test\"", map.getDbEntity("ARTGROUP").getQualifier().toString());
    assertEquals(4, map.getDbEntity("EXHIBIT").getAttributes().size());
    assertEquals(3, map.getDbEntity("PAINTING").getRelationships().size());
    assertEquals("gallery_seq", map.getDbEntity("GALLERY").getPrimaryKeyGenerator().getGeneratorName());
    DbAttribute pk1 = map.getDbEntity("EXHIBIT").getAttribute("EXHIBIT_ID");
    assertFalse(pk1.isGenerated());
    assertTrue(pk1.isPrimaryKey());
    DbAttribute pk2 = map.getDbEntity("GENERATED_COLUMN").getAttribute("GENERATED_COLUMN");
    assertTrue(pk2.isGenerated());
    assertTrue(pk2.isPrimaryKey());
    assertEquals(true, map.getProcedure("cayenne_tst_out_proc").isReturningValue());
    assertEquals(1, map.getProcedure("cayenne_tst_out_proc").getCallOutParameters().size());
    assertEquals(2, map.getProcedure("cayenne_tst_out_proc").getCallParameters().size());
    assertEquals("true", map.getQueryDescriptor("EjbqlQueryTest").getProperty("cayenne.GenericSelectQuery.fetchingDataRows"));
    SQLTemplateDescriptor descriptor = (SQLTemplateDescriptor) map.getQueryDescriptor("NonSelectingQuery");
    assertEquals("INSERT INTO PAINTING (PAINTING_ID, PAINTING_TITLE, ESTIMATED_PRICE) " + "VALUES (512, 'No Painting Like This', 12.5)", descriptor.getAdapterSql().get("org.apache.cayenne.dba.db2.DB2Adapter"));
    assertEquals("TEST", map.getEmbeddable("org.apache.cayenne.testdo.Embeddable").getAttribute("test").getDbAttributeName());
}
Also used : URLResource(org.apache.cayenne.resource.URLResource) SQLTemplateDescriptor(org.apache.cayenne.map.SQLTemplateDescriptor) DbAttribute(org.apache.cayenne.map.DbAttribute) URL(java.net.URL) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 79 with DbAttribute

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

the class HSQLDBAdapterIT method testCreateTableAddsCachedKeyword.

@Test
public void testCreateTableAddsCachedKeyword() {
    HSQLDBAdapter adapter = objectFactory.newInstance(HSQLDBAdapter.class, HSQLDBAdapter.class.getName());
    DbEntity e = new DbEntity("Test");
    DbAttribute dblPrec = new DbAttribute("dbl1");
    dblPrec.setType(Types.INTEGER);
    e.addAttribute(dblPrec);
    String sql = adapter.createTable(e);
    assertEquals(0, sql.indexOf("CREATE CACHED TABLE"));
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

Example 80 with DbAttribute

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

the class MySQLAdapterIT method testCreateTableAppendPKClause.

@Test
public void testCreateTableAppendPKClause() {
    MySQLAdapter adapter = objectFactory.newInstance(MySQLAdapter.class, MySQLAdapter.class.getName());
    DbEntity e = new DbEntity("Test");
    DbAttribute pk1 = new DbAttribute("PK1");
    pk1.setPrimaryKey(true);
    e.addAttribute(pk1);
    DbAttribute pk2 = new DbAttribute("PK2");
    pk2.setPrimaryKey(true);
    e.addAttribute(pk2);
    StringBuffer b1 = new StringBuffer();
    adapter.createTableAppendPKClause(b1, e);
    assertTrue(b1.indexOf("PK1") > 0);
    assertTrue(b1.indexOf("PK2") > 0);
    assertTrue(b1.indexOf("PK1") < b1.indexOf("PK2"));
    pk2.setGenerated(true);
    StringBuffer b2 = new StringBuffer();
    adapter.createTableAppendPKClause(b2, e);
    assertTrue(b2.indexOf("PK1") > 0);
    assertTrue(b2.indexOf("PK2") > 0);
    assertTrue(b2.indexOf("PK1") > b2.indexOf("PK2"));
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

Aggregations

DbAttribute (org.apache.cayenne.map.DbAttribute)194 DbEntity (org.apache.cayenne.map.DbEntity)109 Test (org.junit.Test)67 ObjEntity (org.apache.cayenne.map.ObjEntity)36 DbRelationship (org.apache.cayenne.map.DbRelationship)35 ObjAttribute (org.apache.cayenne.map.ObjAttribute)32 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)21 DbJoin (org.apache.cayenne.map.DbJoin)18 HashMap (java.util.HashMap)16 ObjRelationship (org.apache.cayenne.map.ObjRelationship)16 ArrayList (java.util.ArrayList)14 DbAttributeBinding (org.apache.cayenne.access.translator.DbAttributeBinding)12 DataMap (org.apache.cayenne.map.DataMap)11 JdbcAdapter (org.apache.cayenne.dba.JdbcAdapter)10 QuotingStrategy (org.apache.cayenne.dba.QuotingStrategy)10 MergerToken (org.apache.cayenne.dbsync.merge.token.MergerToken)10 DeleteBatchQuery (org.apache.cayenne.query.DeleteBatchQuery)10 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)10 ObjectId (org.apache.cayenne.ObjectId)9 Expression (org.apache.cayenne.exp.Expression)8