Skip to content
Closed
8 changes: 4 additions & 4 deletions cayenne-commitlog/src/test/resources/lifecycle-map.map.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,16 @@
<db-relationship name="parent" source="AUDITABLE_CHILD3" target="AUDITABLE2">
<db-attribute-pair source="AUDITABLE2_ID" target="ID"/>
</db-relationship>
<db-relationship name="e34s" source="E3" target="E34" toDependentPK="true" toMany="true">
<db-relationship name="e34s" source="E3" target="E34" toMany="true">
<db-attribute-pair source="ID" target="E3_ID"/>
</db-relationship>
<db-relationship name="e3" source="E34" target="E3">
<db-relationship name="e3" source="E34" target="E3" fk="true">
<db-attribute-pair source="E3_ID" target="ID"/>
</db-relationship>
<db-relationship name="e4" source="E34" target="E4">
<db-relationship name="e4" source="E34" target="E4" fk="true">
<db-attribute-pair source="E4_ID" target="ID"/>
</db-relationship>
<db-relationship name="e34s" source="E4" target="E34" toDependentPK="true" toMany="true">
<db-relationship name="e34s" source="E4" target="E34" toMany="true">
<db-attribute-pair source="ID" target="E4_ID"/>
</db-relationship>
<obj-relationship name="children1" source="Auditable1" target="AuditableChild1" deleteRule="Deny" db-relationship-path="children1"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public MergerToken createAddRelationshipToDb(DbEntity entity, final DbRelationsh
return new AddRelationshipToDb(entity, rel) {
@Override
public List<String> createSql(DbAdapter adapter) {
if (!rel.isToMany() && rel.isToPK() && !rel.isToDependentPK()) {
if (rel.isToMasterPK()) {

DbEntity source = (DbEntity) rel.getSourceEntity();
QuotingStrategy context = adapter.getQuotingStrategy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ private boolean isManyToMany() {
boolean isNotHaveAttributes = joinEntity.getAttributes().size() == 0;

return isNotHaveAttributes
&& reverseRelationship1 != null && reverseRelationship1.isToDependentPK()
&& reverseRelationship2 != null && reverseRelationship2.isToDependentPK()
&& reverseRelationship1 != null && !reverseRelationship1.isFK()
&& reverseRelationship2 != null && !reverseRelationship2.isFK()
&& entity1 != null && entity2 != null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public void load(DatabaseMetaData metaData, DbLoadDataStore map) throws SQLExcep
DbRelationship forwardRelationship = new DbRelationship();
forwardRelationship.setSourceEntity(pkEntity);
forwardRelationship.setTargetEntityName(fkEntity);
forwardRelationship.setFK(false);

// TODO: dirty and non-transparent... using DbRelationshipDetected for the benefit of the merge package.
// This info is available from joins....
Expand All @@ -84,14 +85,10 @@ public void load(DatabaseMetaData metaData, DbLoadDataStore map) throws SQLExcep
reverseRelationship.setSourceEntity(fkEntity);
reverseRelationship.setTargetEntityName(pkEntity);
reverseRelationship.setToMany(false);

reverseRelationship.setFK(true);
createAndAppendJoins(exportedKeys, pkEntity, fkEntity, forwardRelationship, reverseRelationship);

boolean toDependentPK = isToDependentPK(forwardRelationship);
boolean toMany = isToMany(toDependentPK, fkEntity, forwardRelationship);

forwardRelationship.setToDependentPK(toDependentPK);
forwardRelationship.setToMany(toMany);
forwardRelationship.setToMany(isToMany(fkEntity,forwardRelationship));

// set relationship names only after their joins are ready ...
// generator logic is based on relationship state...
Expand Down Expand Up @@ -146,18 +143,13 @@ private void checkAndAddRelationship(DbEntity entity, DbRelationship relationshi
}
}

private boolean isToMany(boolean toDependentPK, DbEntity fkEntity, DbRelationship forwardRelationship) {
return !toDependentPK || fkEntity.getPrimaryKeys().size() != forwardRelationship.getJoins().size();
}

private boolean isToDependentPK(DbRelationship forwardRelationship) {
for (DbJoin dbJoin : forwardRelationship.getJoins()) {
if (!dbJoin.getTarget().isPrimaryKey()) {
return false;
private boolean isToMany(DbEntity fkEntity, DbRelationship forwardRelationship) {
for (DbJoin join : forwardRelationship.getJoins()) {
if (!join.getSource().isPrimaryKey() || !join.getTarget().isPrimaryKey()) {
return true;
}
}

return true;
return fkEntity.getPrimaryKeys().size() != forwardRelationship.getJoins().size();
}

private void createAndAppendJoins(Set<ExportedKey> exportedKeys, DbEntity pkEntity, DbEntity fkEntity,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public void testAddRelationship() throws Exception {
dbEntity("table2").attributes(
dbAttr("attr01").typeInt().primaryKey(),
dbAttr("attr02").typeInt())
).join("rel", "table1.attr01", "table2.attr01")
).join("rel", "table1.attr01", "table2.attr01", true)
.build();

DataMap db = dataMap().with(
Expand Down Expand Up @@ -287,8 +287,8 @@ public void testAddRelationship1() throws Exception {
dbAttr("attr01").typeInt().primaryKey(),
dbAttr("attr02").typeInt().primaryKey(),
dbAttr("attr03").typeInt().primaryKey())
).join("rel", "table1.attr01", "table2.attr01")
.join("rel1", "table1.attr01", "table2.attr03")
).join("rel", "table1.attr01", "table2.attr01",true)
.join("rel1", "table1.attr01", "table2.attr03",false)
.build();

DataMap db = dataMap().with(
Expand All @@ -300,8 +300,8 @@ public void testAddRelationship1() throws Exception {
dbAttr("attr01").typeInt().primaryKey(),
dbAttr("attr02").typeInt().primaryKey(),
dbAttr("attr03").typeInt().primaryKey())
).join("rel", "table1.attr01", "table2.attr02")
.join("rel1", "table1.attr01", "table2.attr03")
).join("rel", "table1.attr01", "table2.attr02",true)
.join("rel1", "table1.attr01", "table2.attr03",false)
.build();


Expand Down Expand Up @@ -329,7 +329,7 @@ public void testTableNameUppercaseRelationship() throws Exception {
dbAttr("attr01").typeInt().primaryKey(),
dbAttr("attr02").typeInt().primaryKey(),
dbAttr("attr03").typeInt().primaryKey())
).join("rel", "TABLE1.attr01", "table2.attr01").build();
).join("rel", "TABLE1.attr01", "table2.attr01",true).build();

DataMap db = dataMap().with(
dbEntity("table1").attributes(
Expand All @@ -340,7 +340,7 @@ public void testTableNameUppercaseRelationship() throws Exception {
dbAttr("attr01").typeInt().primaryKey(),
dbAttr("attr02").typeInt().primaryKey(),
dbAttr("attr03").typeInt().primaryKey())
).join("rel", "table1.attr01", "table2.attr01").build();
).join("rel", "table1.attr01", "table2.attr01",false).build();


List<MergerToken> tokens = dbMerger().createMergeTokens(existing, db);
Expand All @@ -358,7 +358,7 @@ public void testAttributeNameUppercaseRelationship() throws Exception {
dbAttr("attr01").typeInt().primaryKey(),
dbAttr("attr02").typeInt().primaryKey(),
dbAttr("attr03").typeInt().primaryKey())
).join("rel", "table1.ATTR01", "table2.attr01").build();
).join("rel", "table1.ATTR01", "table2.attr01",true).build();

DataMap db = dataMap().with(
dbEntity("table1").attributes(
Expand All @@ -369,7 +369,7 @@ public void testAttributeNameUppercaseRelationship() throws Exception {
dbAttr("attr01").typeInt().primaryKey(),
dbAttr("attr02").typeInt().primaryKey(),
dbAttr("attr03").typeInt().primaryKey())
).join("rel", "table1.attr01", "table2.attr01").build();
).join("rel", "table1.attr01", "table2.attr01",false).build();


List<MergerToken> tokens = dbMerger().createMergeTokens(existing, db);
Expand Down Expand Up @@ -397,7 +397,7 @@ public void testRemoveRelationship() throws Exception {
dbEntity("table2").attributes(
dbAttr("attr01").typeInt().primaryKey(),
dbAttr("attr02").typeInt())
).join("rel", "table1.attr01", "table2.attr01")
).join("rel", "table1.attr01", "table2.attr01",true)
.build();


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ public void testAddForeignKeyWithTable() throws Exception {
r1.setSourceEntity(dbEntity);
r1.setTargetEntityName(artistDbEntity);
r1.setToMany(false);
r1.setFK(true);
r1.addJoin(new DbJoin(r1, "ARTIST_ID", "ARTIST_ID"));
dbEntity.addRelationship(r1);

Expand Down Expand Up @@ -262,6 +263,7 @@ public void testAddForeignKeyAfterTable() throws Exception {
r1.setSourceEntity(dbEntity);
r1.setTargetEntityName(artistDbEntity);
r1.setToMany(false);
r1.setFK(true);
r1.addJoin(new DbJoin(r1, "ARTIST_ID", "ARTIST_ID"));
dbEntity.addRelationship(r1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ public DataMapBuilder withObjEntities(int count) {
return this;
}

public DataMapBuilder join(String from, String to) {
return join(null, from, to);
public DataMapBuilder join(String from, String to,boolean isFK) {
return join(null, from, to,isFK);
}

public DataMapBuilder join(String name, String from, String to) {
public DataMapBuilder join(String name, String from, String to, boolean isFK) {
String[] fromSplit = from.split("\\.");
DbEntity fromEntity = obj.getDbEntity(fromSplit[0]);
if (fromEntity == null) {
Expand All @@ -103,7 +103,7 @@ public DataMapBuilder join(String name, String from, String to) {
fromEntity.addRelationship(new DbRelationshipBuilder(name)
.from(fromEntity, fromSplit[1])
.to(toSplit[0], toSplit[1])

.fK(isFK)
.build());

return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,19 @@ public DbRelationshipBuilder name(String name) {
return this;
}

public DbRelationshipBuilder from(DbEntity entity, String ... columns) {
public DbRelationshipBuilder from(DbEntity entity, String... columns) {
obj.setSourceEntity(entity);
this.from = columns;

return this;
}

public DbRelationshipBuilder to(String entityName, String ... columns) {
public DbRelationshipBuilder fK(boolean fk) {
obj.setFK(fk);
return this;
}

public DbRelationshipBuilder to(String entityName, String... columns) {
obj.setTargetEntityName(entityName);
this.to = columns;

Expand All @@ -80,6 +85,13 @@ public DbRelationship build() {
obj.addJoin(new DbJoin(obj, from[i], to[i]));
}

DbJoin join = new DbJoin(obj);
if (!obj.isFK() && join.getTarget() != null && join.getSource() != null) {
if (join.getTarget().isPrimaryKey() && !join.getSource().isPrimaryKey()) {
obj.setFK(true);
}
}

return obj;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public void testRemoveFKColumnWithoutRelationshipInDb() throws Exception {
rel2To1.setSourceEntity(dbEntity2);
rel2To1.setTargetEntityName(dbEntity1);
rel2To1.setToMany(false);
rel2To1.setFK(true);
rel2To1.addJoin(new DbJoin(rel2To1, e2col2.getName(), e1col1.getName()));
dbEntity2.addRelationship(rel2To1);
assertSame(rel1To2, rel2To1.getReverseRelationship());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public void testForeignKey() throws Exception {
rel2To1.setSourceEntity(dbEntity2);
rel2To1.setTargetEntityName(dbEntity1);
rel2To1.setToMany(false);
rel2To1.setFK(true);
rel2To1.addJoin(new DbJoin(rel2To1, e2col2.getName(), e1col1.getName()));
dbEntity2.addRelationship(rel2To1);
assertSame(rel1To2, rel2To1.getReverseRelationship());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void testRelationshipLoad() throws Exception {

assertNotNull("No relationship to PAINTING_INFO", oneToOne);
assertFalse("Relationship to PAINTING_INFO must be to-one", oneToOne.isToMany());
assertTrue("Relationship to PAINTING_INFO must be to-one", oneToOne.isToDependentPK());
assertTrue("Relationship to PAINTING_INFO must be to-one", !oneToOne.isFK());
}

// private void assertUniqueConstraintsInRelationships(DataMap map) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ protected List<UpgradeUnit> upgradeDOM(Resource resource, List<UpgradeHandler> h
for(UpgradeUnit dataMapUnit : dataMapUnits) {
handler.processDataMapDom(dataMapUnit);
}
handler.processAllDataMapDomes(dataMapUnits);
}

return allUnits;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import java.util.List;

/**
* Interface that upgrade handlers should implement.
* Implementation also should be injected into DI stack in right order.
Expand Down Expand Up @@ -108,4 +110,8 @@ default void updateExtensionSchema(UpgradeUnit upgradeUnit, String extension) {
element.setAttribute("xmlns", "http://cayenne.apache.org/schema/"+getVersion()+"/"+extension);
}
}

default void processAllDataMapDomes(List<UpgradeUnit> dataMapUnits) {
//noop
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.apache.cayenne.project.upgrade.handlers;

import org.apache.cayenne.project.upgrade.UpgradeUnit;
import org.apache.cayenne.project.upgrade.handlers.v11.ToDepPkToFkUpdater;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Element;
Expand Down Expand Up @@ -87,6 +88,12 @@ public void processDataMapDom(UpgradeUnit upgradeUnit) {
updateDbImportConfig(upgradeUnit);
}

@Override
public void processAllDataMapDomes(List<UpgradeUnit> dataMapUnits) {
ToDepPkToFkUpdater fkUpdater = new ToDepPkToFkUpdater();
fkUpdater.update(dataMapUnits);
}

private void upgradeComments(UpgradeUnit upgradeUnit) {
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList infoNodes;
Expand Down
Loading