Search in sources :

Example 31 with DbEntity

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

the class AttributeLoaderIT method assertLobDbEntities.

private void assertLobDbEntities() {
    DbEntity blobEnt = getDbEntity("BLOB_TEST");
    assertNotNull(blobEnt);
    DbAttribute blobAttr = getDbAttribute(blobEnt, "BLOB_COL");
    assertNotNull(blobAttr);
    assertTrue(msgForTypeMismatch(Types.BLOB, blobAttr), Types.BLOB == blobAttr.getType() || Types.VARBINARY == blobAttr.getType() || Types.LONGVARBINARY == blobAttr.getType());
    DbEntity clobEnt = getDbEntity("CLOB_TEST");
    assertNotNull(clobEnt);
    DbAttribute clobAttr = getDbAttribute(clobEnt, "CLOB_COL");
    assertNotNull(clobAttr);
    assertTrue(msgForTypeMismatch(Types.CLOB, clobAttr), Types.CLOB == clobAttr.getType() || Types.VARCHAR == clobAttr.getType() || Types.LONGVARCHAR == clobAttr.getType());
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbAttribute(org.apache.cayenne.map.DbAttribute)

Example 32 with DbEntity

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

the class ExportedKeyLoaderIT method testExportedKeyLoad.

@Test
public void testExportedKeyLoad() throws Exception {
    boolean supportsFK = accessStackAdapter.supportsFKConstraints();
    if (!supportsFK) {
        return;
    }
    createEntity(nameForDb("ARTIST"));
    createEntity(nameForDb("GALLERY"));
    createEntity(nameForDb("PAINTING"));
    DbEntity artist = getDbEntity("ARTIST");
    DbAttribute artistId = new DbAttribute("ARTIST_ID");
    artist.addAttribute(artistId);
    DbEntity gallery = getDbEntity("GALLERY");
    DbAttribute galleryId = new DbAttribute("GALLERY_ID");
    gallery.addAttribute(galleryId);
    DbEntity painting = getDbEntity("PAINTING");
    DbAttribute paintingId = new DbAttribute("PAINTING_ID");
    DbAttribute paintingArtistId = new DbAttribute("ARTIST_ID");
    DbAttribute paintingGalleryId = new DbAttribute("GALLERY_ID");
    painting.addAttribute(paintingId);
    painting.addAttribute(paintingArtistId);
    painting.addAttribute(paintingGalleryId);
    ExportedKeyLoader loader = new ExportedKeyLoader(EMPTY_CONFIG, new DefaultDbLoaderDelegate());
    loader.load(connection.getMetaData(), store);
    assertEquals(2, store.getExportedKeysEntrySet().size());
    ExportedKey artistIdFk = findArtistExportedKey();
    assertNotNull(artistIdFk);
    assertEquals("ARTIST", artistIdFk.getPk().getTable().toUpperCase());
    assertEquals("ARTIST_ID", artistIdFk.getPk().getColumn().toUpperCase());
    assertEquals("PAINTING", artistIdFk.getFk().getTable().toUpperCase());
    assertEquals("ARTIST_ID", artistIdFk.getFk().getColumn().toUpperCase());
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

Example 33 with DbEntity

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

the class PrimaryKeyLoaderIT method testPrimaryKeyLoad.

@Test
public void testPrimaryKeyLoad() throws Exception {
    createDbEntities();
    DbEntity artist = getDbEntity(nameForDb("ARTIST"));
    DbAttribute artistId = new DbAttribute(nameForDb("ARTIST_ID"));
    DbAttribute artistName = new DbAttribute(nameForDb("ARTIST_NAME"));
    DbAttribute artistId1 = new DbAttribute(nameForDb("ARTIST_ID1"));
    artist.addAttribute(artistId);
    artist.addAttribute(artistName);
    artist.addAttribute(artistId1);
    assertFalse(artistId.isPrimaryKey());
    assertFalse(artistName.isPrimaryKey());
    assertFalse(artistId1.isPrimaryKey());
    PrimaryKeyLoader loader = new PrimaryKeyLoader(EMPTY_CONFIG, new DefaultDbLoaderDelegate());
    loader.load(connection.getMetaData(), store);
    assertTrue(artistId.isPrimaryKey());
    assertFalse(artistId1.isPrimaryKey());
    assertFalse(artistName.isPrimaryKey());
    Collection<DbAttribute> pk = artist.getPrimaryKeys();
    assertEquals(1, pk.size());
    assertEquals(artistId, pk.iterator().next());
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

Example 34 with DbEntity

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

the class DefaultValueTransformerFactory method getJavaType.

// TODO: calculating Java type of ObjAttribute may become unneeded per
// CAY-1752, as DbAttribute will have it.
protected String getJavaType(DbAttribute a) {
    DbEntity dbEntity = a.getEntity();
    DataMap dataMap = dbEntity.getDataMap();
    Collection<String> javaTypes = new HashSet<>();
    for (ObjEntity objEntity : dataMap.getMappedEntities(dbEntity)) {
        for (ObjAttribute oa : objEntity.getAttributes()) {
            // TODO: this won't pick up flattened attributes
            if (a.getName().equals(oa.getDbAttributePath())) {
                javaTypes.add(oa.getType());
            }
        }
    }
    if (javaTypes.size() != 1) {
        String javaType = TypesMapping.getJavaBySqlType(a.getType());
        String attributeName = dbEntity.getName() + "." + a.getName();
        String msg = javaTypes.size() > 1 ? "ObjAttributes with different java types" : "No ObjAttributes";
        // Warn user about this problem as there is nothing else we can do
        logger.warn(msg + " bound to DbAttribute '" + attributeName + "', " + javaType + " type will be used.");
        return javaType;
    }
    return javaTypes.iterator().next();
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) ObjAttribute(org.apache.cayenne.map.ObjAttribute) DataMap(org.apache.cayenne.map.DataMap) HashSet(java.util.HashSet)

Example 35 with DbEntity

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

the class DefaultValueTransformerFactoryIT method testGetJavaType.

@Test
public void testGetJavaType() {
    DbAttribute t1_ct = t1.getAttribute("CRYPTO_STRING");
    assertEquals("java.lang.String", f.getJavaType(t1_ct));
    DbAttribute t2_cb = t2.getAttribute("CRYPTO_BYTES");
    assertEquals("byte[]", f.getJavaType(t2_cb));
    DbEntity fakeEntity = mock(DbEntity.class);
    when(fakeEntity.getDataMap()).thenReturn(t1.getDataMap());
    DbAttribute fakeA1 = mock(DbAttribute.class);
    when(fakeA1.getName()).thenReturn("fake1");
    when(fakeA1.getEntity()).thenReturn(fakeEntity);
    when(fakeA1.getType()).thenReturn(Types.VARBINARY);
    assertEquals("byte[]", f.getJavaType(fakeA1));
    DbAttribute fakeA2 = mock(DbAttribute.class);
    when(fakeA2.getName()).thenReturn("fake2");
    when(fakeA2.getEntity()).thenReturn(fakeEntity);
    when(fakeA2.getType()).thenReturn(Types.VARCHAR);
    assertEquals("java.lang.String", f.getJavaType(fakeA2));
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

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