Skip to content

CAY-2739 Mapping Proposal: replace "toDepPk" with "fk" #625

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions cayenne-ant/src/test/resources/cgenTest.map.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<data-map xmlns="http://cayenne.apache.org/schema/11/modelMap"
<data-map xmlns="http://cayenne.apache.org/schema/12/modelMap"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://cayenne.apache.org/schema/11/modelMap http://cayenne.apache.org/schema/11/modelMap.xsd"
project-version="11">
xsi:schemaLocation="http://cayenne.apache.org/schema/12/modelMap http://cayenne.apache.org/schema/12/modelMap.xsd"
project-version="12">
<embeddable className="Embeddable"/>
<obj-entity name="ObjEntity" className="ObjEntity"/>
<obj-entity name="ObjEntity1" className="ObjEntity1"/>
<cgen xmlns="http://cayenne.apache.org/schema/11/cgen">
<cgen xmlns="http://cayenne.apache.org/schema/12/cgen">
<destDir>cgenConfigTest</destDir>
<excludeEntities>ObjEntity</excludeEntities>
<excludeEmbeddables>Embeddable</excludeEmbeddables>
Expand Down
6 changes: 3 additions & 3 deletions cayenne-ant/src/test/resources/embeddable.map.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<data-map xmlns="http://cayenne.apache.org/schema/11/modelMap"
<data-map xmlns="http://cayenne.apache.org/schema/12/modelMap"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://cayenne.apache.org/schema/11/modelMap http://cayenne.apache.org/schema/11/modelMap.xsd"
project-version="11">
xsi:schemaLocation="http://cayenne.apache.org/schema/12/modelMap http://cayenne.apache.org/schema/12/modelMap.xsd"
project-version="12">
<property name="defaultPackage" value="org.apache.cayenne.testdo.embeddable"/>
<embeddable className="org.apache.cayenne.testdo.embeddable.Embeddable1">
<embeddable-attribute name="embedded10" type="java.lang.String" db-attribute-name="EMBEDDED10"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
specific language governing permissions and limitations
under the License.
-->
<data-map xmlns="http://cayenne.apache.org/schema/11/modelMap"
<data-map xmlns="http://cayenne.apache.org/schema/12/modelMap"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://cayenne.apache.org/schema/11/modelMap http://cayenne.apache.org/schema/11/modelMap.xsd"
project-version="11">
xsi:schemaLocation="http://cayenne.apache.org/schema/12/modelMap http://cayenne.apache.org/schema/12/modelMap.xsd"
project-version="12">
<db-entity name="PARENT" schema="APP">
<db-attribute name="COL2" type="CHAR" length="25"/>
<db-attribute name="COL3" type="DECIMAL" length="10" scale="2"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<data-map xmlns="http://cayenne.apache.org/schema/11/modelMap"
<data-map xmlns="http://cayenne.apache.org/schema/12/modelMap"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://cayenne.apache.org/schema/11/modelMap http://cayenne.apache.org/schema/11/modelMap.xsd"
project-version="11">
xsi:schemaLocation="http://cayenne.apache.org/schema/12/modelMap http://cayenne.apache.org/schema/12/modelMap.xsd"
project-version="12">
<property name="defaultPackage" value="org.apache.cayenne.testdo.cgen2"/>
<db-entity name="MYARTGROUP">
<db-attribute name="MY_GROUP_ID" type="INTEGER" isPrimaryKey="true" isMandatory="true"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<data-map xmlns="http://cayenne.apache.org/schema/11/modelMap"
<data-map xmlns="http://cayenne.apache.org/schema/12/modelMap"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://cayenne.apache.org/schema/11/modelMap http://cayenne.apache.org/schema/11/modelMap.xsd"
project-version="11">
xsi:schemaLocation="http://cayenne.apache.org/schema/12/modelMap http://cayenne.apache.org/schema/12/modelMap.xsd"
project-version="12">

<db-entity name="ARTGROUP">
<db-attribute name="GROUP_ID" type="INTEGER" isPrimaryKey="true" isMandatory="true"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
specific language governing permissions and limitations
under the License.
-->
<data-map xmlns="http://cayenne.apache.org/schema/11/modelMap"
<data-map xmlns="http://cayenne.apache.org/schema/12/modelMap"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://cayenne.apache.org/schema/11/modelMap http://cayenne.apache.org/schema/11/modelMap.xsd"
project-version="11">
xsi:schemaLocation="http://cayenne.apache.org/schema/12/modelMap http://cayenne.apache.org/schema/12/modelMap.xsd"
project-version="12">
<property name="defaultSchema" value="SCHEMA_01"/>

<db-entity name="TEST1" schema="SCHEMA_01">
Expand Down
6 changes: 3 additions & 3 deletions cayenne-ant/src/test/resources/testmap.map.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<data-map xmlns="http://cayenne.apache.org/schema/11/modelMap"
<data-map xmlns="http://cayenne.apache.org/schema/12/modelMap"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://cayenne.apache.org/schema/11/modelMap http://cayenne.apache.org/schema/11/modelMap.xsd"
project-version="11">
xsi:schemaLocation="http://cayenne.apache.org/schema/12/modelMap http://cayenne.apache.org/schema/12/modelMap.xsd"
project-version="12">
<property name="defaultPackage" value="org.apache.cayenne.testdo.testmap"/>
<property name="defaultSuperclass" value="org.apache.cayenne.GenericPersistentObject"/>
<procedure name="cayenne_tst_out_proc">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<domain xmlns="http://cayenne.apache.org/schema/11/domain"
project-version="11">
<domain xmlns="http://cayenne.apache.org/schema/12/domain"
project-version="12">
<map name="lifecycle-map"/>

<node name="lifecycle-db"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<data-map xmlns="http://cayenne.apache.org/schema/11/modelMap"
<data-map xmlns="http://cayenne.apache.org/schema/12/modelMap"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://cayenne.apache.org/schema/11/modelMap http://cayenne.apache.org/schema/11/modelMap.xsd"
project-version="11">
xsi:schemaLocation="http://cayenne.apache.org/schema/12/modelMap http://cayenne.apache.org/schema/12/modelMap.xsd"
project-version="12">
<property name="defaultPackage" value="org.apache.cayenne.lifecycle.db"/>
<db-entity name="E1">
<db-attribute name="ID" type="BIGINT" isPrimaryKey="true" isMandatory="true"/>
Expand Down
6 changes: 3 additions & 3 deletions cayenne-commitlog/src/test/resources/cayenne-lifecycle.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<domain xmlns="http://cayenne.apache.org/schema/11/domain"
<domain xmlns="http://cayenne.apache.org/schema/12/domain"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://cayenne.apache.org/schema/11/domain https://cayenne.apache.org/schema/11/domain.xsd"
project-version="11">
xsi:schemaLocation="http://cayenne.apache.org/schema/12/domain https://cayenne.apache.org/schema/12/domain.xsd"
project-version="12">
<map name="lifecycle-map"/>
<node name="lifecycle-db"
factory="org.apache.cayenne.configuration.runtime.XMLPoolingDataSourceFactory"
Expand Down
34 changes: 11 additions & 23 deletions cayenne-commitlog/src/test/resources/lifecycle-map.map.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<data-map xmlns="http://cayenne.apache.org/schema/11/modelMap"
<data-map xmlns="http://cayenne.apache.org/schema/12/modelMap"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://cayenne.apache.org/schema/11/modelMap https://cayenne.apache.org/schema/11/modelMap.xsd"
project-version="11">
xsi:schemaLocation="http://cayenne.apache.org/schema/12/modelMap https://cayenne.apache.org/schema/12/modelMap.xsd"
project-version="12">
<property name="defaultPackage" value="org.apache.cayenne.commitlog.db"/>
<db-entity name="AUDITABLE1">
<db-attribute name="CHAR_PROPERTY1" type="VARCHAR" length="200"/>
Expand Down Expand Up @@ -104,28 +104,28 @@
<db-relationship name="auditable4s" source="AUDITABLE3" target="AUDITABLE4" toMany="true">
<db-attribute-pair source="ID" target="AUDITABLE3_ID"/>
</db-relationship>
<db-relationship name="auditable3" source="AUDITABLE4" target="AUDITABLE3">
<db-relationship name="auditable3" source="AUDITABLE4" target="AUDITABLE3" fk="true">
<db-attribute-pair source="AUDITABLE3_ID" target="ID"/>
</db-relationship>
<db-relationship name="parent" source="AUDITABLE_CHILD1" target="AUDITABLE1">
<db-relationship name="parent" source="AUDITABLE_CHILD1" target="AUDITABLE1" fk="true">
<db-attribute-pair source="AUDITABLE1_ID" target="ID"/>
</db-relationship>
<db-relationship name="parent" source="AUDITABLE_CHILD1X" target="AUDITABLE1">
<db-relationship name="parent" source="AUDITABLE_CHILD1X" target="AUDITABLE1" fk="true">
<db-attribute-pair source="AUDITABLE1_ID" target="ID"/>
</db-relationship>
<db-relationship name="parent" source="AUDITABLE_CHILD3" target="AUDITABLE2">
<db-relationship name="parent" source="AUDITABLE_CHILD3" target="AUDITABLE2" fk="true">
<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 All @@ -137,16 +137,4 @@
<obj-relationship name="parent" source="AuditableChild3" target="Auditable2" deleteRule="Nullify" db-relationship-path="parent"/>
<obj-relationship name="e4s" source="E3" target="E4" deleteRule="Deny" db-relationship-path="e34s.e4"/>
<obj-relationship name="e3s" source="E4" target="E3" deleteRule="Deny" db-relationship-path="e34s.e3"/>
<cgen xmlns="http://cayenne.apache.org/schema/11/cgen">
<destDir>../java</destDir>
<mode>entity</mode>
<template>templates/v4_1/subclass.vm</template>
<superTemplate>templates/v4_1/superclass.vm</superTemplate>
<outputPattern>*.java</outputPattern>
<makePairs>true</makePairs>
<usePkgPath>true</usePkgPath>
<overwrite>false</overwrite>
<createPropertyNames>false</createPropertyNames>
<createPKProperties>false</createPKProperties>
</cgen>
</data-map>
6 changes: 3 additions & 3 deletions cayenne-crypto/src/test/resources/cayenne-crypto.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<domain xmlns="http://cayenne.apache.org/schema/11/domain"
<domain xmlns="http://cayenne.apache.org/schema/12/domain"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://cayenne.apache.org/schema/11/domain https://cayenne.apache.org/schema/11/domain.xsd"
project-version="11">
xsi:schemaLocation="http://cayenne.apache.org/schema/12/domain https://cayenne.apache.org/schema/12/domain.xsd"
project-version="12">
<map name="datamap"/>
<node name="datanode"
factory="org.apache.cayenne.configuration.runtime.XMLPoolingDataSourceFactory"
Expand Down
8 changes: 4 additions & 4 deletions cayenne-crypto/src/test/resources/datamap.map.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<data-map xmlns="http://cayenne.apache.org/schema/11/modelMap"
<data-map xmlns="http://cayenne.apache.org/schema/12/modelMap"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://cayenne.apache.org/schema/11/modelMap https://cayenne.apache.org/schema/11/modelMap.xsd"
project-version="11">
xsi:schemaLocation="http://cayenne.apache.org/schema/12/modelMap https://cayenne.apache.org/schema/12/modelMap.xsd"
project-version="12">
<property name="defaultPackage" value="org.apache.cayenne.crypto.db"/>
<db-entity name="TABLE1">
<db-attribute name="CRYPTO_INT" type="BLOB"/>
Expand Down Expand Up @@ -78,7 +78,7 @@
<db-relationship name="table7s" source="TABLE1" target="TABLE7" toMany="true">
<db-attribute-pair source="ID" target="TABLE1_ID"/>
</db-relationship>
<db-relationship name="toTable1" source="TABLE7" target="TABLE1">
<db-relationship name="toTable1" source="TABLE7" target="TABLE1" fk="true">
<db-attribute-pair source="TABLE1_ID" target="ID"/>
</db-relationship>
<obj-relationship name="table7s" source="Table1" target="Table7" deleteRule="Deny" db-relationship-path="table7s"/>
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 @@ -84,11 +84,11 @@ public static ManyToManyCandidateEntity build(ObjEntity joinEntity) {
}

private boolean isManyToMany() {
boolean isNotHaveAttributes = joinEntity.getAttributes().size() == 0;
boolean isNotHaveAttributes = joinEntity.getAttributes().isEmpty();

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
Loading