Skip to content

Commit

Permalink
Merge pull request #604 from jghansell/vertical-inheritance-joint-pre…
Browse files Browse the repository at this point in the history
…fetch

CAY-2840 Vertical Inheritance: Missing subclass attributes with joint…
  • Loading branch information
stariy95 authored Feb 6, 2024
2 parents d43d291 + 2358f52 commit 0cb5627
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 2 deletions.
3 changes: 2 additions & 1 deletion RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,5 @@ CAY-2810 Can't use custom operator expression with aggregate functions
CAY-2811 Incorrect SQL building using ColumnQuery with ASTScalar
CAY-2813 Regression: Constants.CI_PROPERTY flag is no longer working for MySQL
CAY-2815 Incorrect translation of aliased expression
CAY-2827 Saved data-source XML data doesn't correspond to the XSD schema
CAY-2827 Saved data-source XML data doesn't correspond to the XSD schema
CAY-2840 Vertical Inheritance: Missing subclass attributes with joint prefetch
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ public boolean visitAttribute(AttributeProperty property) {
}
if (count > 1) {
// it was a flattened attribute, so need to keep full path info
String dataRowKey = result.getAttributePaths().get(i) + "." + dbAttribute.getName();
String attributePath = result.getAttributePaths().get(i);
if (attributePath.startsWith(PREFETCH_PREFIX)) {
attributePath = attributePath.substring(PREFETCH_PREFIX.length());
}
String dataRowKey = attributePath + "." + dbAttribute.getName();
resultNodeDescriptor.setDataRowKey(dataRowKey);
addEntityResultField(dataRowKey);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,36 @@ public void testInsertTwoObjectsWithMultipleAttributeAndMultipleRelationship() {
assertEquals(2, ObjectSelect.query(IvImpl.class).selectCount(context));
}

/**
* @link https://issues.apache.org/jira/browse/CAY-2840
*/
@Test
public void testJointPrefetchBelongsTo() throws SQLException {
TableHelper ivOtherTable = new TableHelper(dbHelper, "IV_OTHER");
ivOtherTable.setColumns("ID", "NAME", "BASE_ID").setColumnTypes(Types.INTEGER, Types.VARCHAR, Types.INTEGER);

TableHelper ivBaseTable = new TableHelper(dbHelper, "IV_BASE");
ivBaseTable.setColumns("ID", "NAME", "TYPE").setColumnTypes(Types.INTEGER, Types.VARCHAR, Types.CHAR);

TableHelper ivImplTable = new TableHelper(dbHelper, "IV_IMPL");
ivImplTable.setColumns("ID", "ATTR1").setColumnTypes(Types.INTEGER, Types.VARCHAR);

ivBaseTable.insert(1, "Impl 1", "I");
ivImplTable.insert(1, "attr1");
ivOtherTable.insert(1, "other1", 1);

IvOther other = ObjectSelect.query(IvOther.class).prefetch(IvOther.BASE.joint()).selectOne(context);
assertNotNull(other);
assertNotNull(other.getBase());
assertTrue(IvImpl.class.isAssignableFrom(other.getBase().getClass()));

IvImpl impl = (IvImpl)other.getBase();
// Ensure that base attributes were prefetched correctly
assertEquals("Impl 1", impl.getName());
// Ensure that subclass attributes were prefetched correctly
assertEquals("attr1", impl.getAttr1());
}

/**
* @link https://issues.apache.org/jira/browse/CAY-2282
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import java.util.List;
import org.apache.cayenne.BaseDataObject;
import org.apache.cayenne.exp.property.EntityProperty;
import org.apache.cayenne.exp.property.ListProperty;
import org.apache.cayenne.exp.property.PropertyFactory;
import org.apache.cayenne.exp.property.StringProperty;
import org.apache.cayenne.testdo.inheritance_vertical.IvBase;
import org.apache.cayenne.testdo.inheritance_vertical.IvOther;

/**
* Class _IvBase was generated by Cayenne.
Expand All @@ -26,10 +29,12 @@ public abstract class _IvBase extends BaseDataObject {

public static final StringProperty<String> NAME = PropertyFactory.createString("name", String.class);
public static final StringProperty<String> TYPE = PropertyFactory.createString("type", String.class);
public static final ListProperty<IvOther> OTHERS = PropertyFactory.createList("others", IvOther.class);

protected String name;
protected String type;

protected Object others;

public void setName(String name) {
beforePropertyWrite("name", this.name, name);
Expand All @@ -51,6 +56,19 @@ public String getType() {
return this.type;
}

public void addToOthers(IvOther obj) {
addToManyTarget("others", obj, true);
}

public void removeFromOthers(IvOther obj) {
removeToManyTarget("others", obj, true);
}

@SuppressWarnings("unchecked")
public List<IvOther> getOthers() {
return (List<IvOther>)readProperty("others");
}

@Override
public Object readPropertyDirectly(String propName) {
if(propName == null) {
Expand All @@ -62,6 +80,8 @@ public Object readPropertyDirectly(String propName) {
return this.name;
case "type":
return this.type;
case "others":
return this.others;
default:
return super.readPropertyDirectly(propName);
}
Expand All @@ -80,6 +100,9 @@ public void writePropertyDirectly(String propName, Object val) {
case "type":
this.type = (String)val;
break;
case "others":
this.others = val;
break;
default:
super.writePropertyDirectly(propName, val);
}
Expand All @@ -98,13 +121,15 @@ protected void writeState(ObjectOutputStream out) throws IOException {
super.writeState(out);
out.writeObject(this.name);
out.writeObject(this.type);
out.writeObject(this.others);
}

@Override
protected void readState(ObjectInputStream in) throws IOException, ClassNotFoundException {
super.readState(in);
this.name = (String)in.readObject();
this.type = (String)in.readObject();
this.others = in.readObject();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.apache.cayenne.exp.property.ListProperty;
import org.apache.cayenne.exp.property.PropertyFactory;
import org.apache.cayenne.exp.property.StringProperty;
import org.apache.cayenne.testdo.inheritance_vertical.IvBase;
import org.apache.cayenne.testdo.inheritance_vertical.IvImpl;
import org.apache.cayenne.testdo.inheritance_vertical.IvImplWithLock;
import org.apache.cayenne.testdo.inheritance_vertical.IvOther;
Expand All @@ -29,11 +30,13 @@ public abstract class _IvOther extends BaseDataObject {
public static final String ID_PK_COLUMN = "ID";

public static final StringProperty<String> NAME = PropertyFactory.createString("name", String.class);
public static final EntityProperty<IvBase> BASE = PropertyFactory.createEntity("base", IvBase.class);
public static final ListProperty<IvImpl> IMPLS = PropertyFactory.createList("impls", IvImpl.class);
public static final ListProperty<IvImplWithLock> IMPLS_WITH_LOCK = PropertyFactory.createList("implsWithLock", IvImplWithLock.class);

protected String name;

protected Object base;
protected Object impls;
protected Object implsWithLock;

Expand All @@ -47,6 +50,14 @@ public String getName() {
return this.name;
}

public void setBase(IvBase base) {
setToOneTarget("base", base, true);
}

public IvBase getBase() {
return (IvBase)readProperty("base");
}

public void addToImpls(IvImpl obj) {
addToManyTarget("impls", obj, true);
}
Expand Down Expand Up @@ -82,6 +93,8 @@ public Object readPropertyDirectly(String propName) {
switch(propName) {
case "name":
return this.name;
case "base":
return this.base;
case "impls":
return this.impls;
case "implsWithLock":
Expand All @@ -101,6 +114,9 @@ public void writePropertyDirectly(String propName, Object val) {
case "name":
this.name = (String)val;
break;
case "base":
this.base = val;
break;
case "impls":
this.impls = val;
break;
Expand All @@ -124,6 +140,7 @@ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundE
protected void writeState(ObjectOutputStream out) throws IOException {
super.writeState(out);
out.writeObject(this.name);
out.writeObject(this.base);
out.writeObject(this.impls);
out.writeObject(this.implsWithLock);
}
Expand All @@ -132,6 +149,7 @@ protected void writeState(ObjectOutputStream out) throws IOException {
protected void readState(ObjectInputStream in) throws IOException, ClassNotFoundException {
super.readState(in);
this.name = (String)in.readObject();
this.base = in.readObject();
this.impls = in.readObject();
this.implsWithLock = in.readObject();
}
Expand Down
9 changes: 9 additions & 0 deletions cayenne/src/test/resources/inheritance-vertical.map.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<db-attribute name="OTHER1_ID" type="INTEGER" isMandatory="true"/>
</db-entity>
<db-entity name="IV_OTHER">
<db-attribute name="BASE_ID" type="INTEGER"/>
<db-attribute name="ID" type="INTEGER" isPrimaryKey="true" isMandatory="true"/>
<db-attribute name="NAME" type="VARCHAR" length="100"/>
</db-entity>
Expand Down Expand Up @@ -202,6 +203,9 @@
<db-relationship name="impl" source="IV_BASE" target="IV_IMPL" toDependentPK="true">
<db-attribute-pair source="ID" target="ID"/>
</db-relationship>
<db-relationship name="others" source="IV_BASE" target="IV_OTHER" toMany="true">
<db-attribute-pair source="ID" target="BASE_ID"/>
</db-relationship>
<db-relationship name="impl" source="IV_BASE_WITH_LOCK" target="IV_IMPL_WITH_LOCK" toDependentPK="true">
<db-attribute-pair source="ID" target="ID"/>
</db-relationship>
Expand Down Expand Up @@ -229,6 +233,9 @@
<db-relationship name="other1" source="IV_IMPL_WITH_LOCK" target="IV_OTHER">
<db-attribute-pair source="OTHER1_ID" target="ID"/>
</db-relationship>
<db-relationship name="base" source="IV_OTHER" target="IV_BASE">
<db-attribute-pair source="BASE_ID" target="ID"/>
</db-relationship>
<db-relationship name="impls" source="IV_OTHER" target="IV_IMPL" toMany="true">
<db-attribute-pair source="ID" target="OTHER_ID"/>
</db-relationship>
Expand Down Expand Up @@ -263,11 +270,13 @@
<db-attribute-pair source="IV_ROOT_ID" target="ID"/>
</db-relationship>
<obj-relationship name="x" source="Iv2Sub1" target="Iv2X" deleteRule="Nullify" db-relationship-path="sub1.x"/>
<obj-relationship name="others" source="IvBase" target="IvOther" deleteRule="Deny" db-relationship-path="others"/>
<obj-relationship name="children" source="IvConcrete" target="IvConcrete" deleteRule="Deny" db-relationship-path="children"/>
<obj-relationship name="parent" source="IvConcrete" target="IvConcrete" deleteRule="Nullify" db-relationship-path="parent"/>
<obj-relationship name="other1" source="IvImpl" target="IvOther" deleteRule="Nullify" db-relationship-path="impl.other1"/>
<obj-relationship name="other2" source="IvImpl" target="IvOther" deleteRule="Nullify" db-relationship-path="impl.other2"/>
<obj-relationship name="other1" source="IvImplWithLock" target="IvOther" deleteRule="Nullify" db-relationship-path="impl.other1"/>
<obj-relationship name="base" source="IvOther" target="IvBase" deleteRule="Nullify" db-relationship-path="base"/>
<obj-relationship name="impls" source="IvOther" target="IvImpl" deleteRule="Deny" db-relationship-path="impls.base"/>
<obj-relationship name="implsWithLock" source="IvOther" target="IvImplWithLock" deleteRule="Deny" db-relationship-path="impls.base"/>
<obj-relationship name="ivRoot" source="IvSub3" target="IvRoot" deleteRule="Nullify" db-relationship-path="sub3.ivRoot1"/>
Expand Down

0 comments on commit 0cb5627

Please sign in to comment.