Re: Cross data map relationships
Ricardo Parada <[email protected]> Tue, 14 Oct 2025 17:23:40 -0400
| Newsgroups | gmane.comp.java.cayenne.user |
|---|---|
| Message-ID | <[email protected]> |
Here is a very minimal test of what I am seeing.
Two data maps: Main and Errors.
I have an APP_USER DbTable in the Main data map and an APP_ERROR DbTable in t=
he Errors data map.
Then I have a to-many relationship and inverse to one between these two.
APP_USER <->> APP_ERROR
I then simply create the ObjEntity, ObjAttribute and ObjRelationship objects=
programmatically.
I then save the first data map and it throws a NullPointerException. =20
Here is the isolated test:
(I avoided creating data nodes and the project in this code to keep things s=
imple and illustrate the NullPointerException)
package play.cay.utils.conversion;
import java.io.PrintWriter;
import java.sql.Types;
import org.apache.cayenne.configuration.EmptyConfigurationNodeVisitor;
import org.apache.cayenne.map.DataMap;
import org.apache.cayenne.map.DbAttribute;
import org.apache.cayenne.map.DbEntity;
import org.apache.cayenne.map.DbJoin;
import org.apache.cayenne.map.DbRelationship;
import org.apache.cayenne.map.ObjAttribute;
import org.apache.cayenne.map.ObjEntity;
import org.apache.cayenne.map.ObjRelationship;
import org.apache.cayenne.util.XMLEncoder;
public class DataMapTest {
public static void main(String[] args) {
// Create DataMaps
DataMap mainMap =3D new DataMap("Main");
DataMap errorsMap =3D new DataMap("Errors");
// -------------------- DB Entities --------------------
// APP_USER DbEntity
DbEntity appUserDb =3D new DbEntity("APP_USER");
DbAttribute appUserIdDbAttr =3D new DbAttribute("ID", Types.INTEGER,=
appUserDb);
appUserIdDbAttr.setPrimaryKey(true);
appUserDb.addAttribute(appUserIdDbAttr);
appUserDb.addAttribute(new DbAttribute("NAME", Types.VARCHAR, appUse=
rDb));
appUserDb.addAttribute(new DbAttribute("EMAIL", Types.VARCHAR, appUs=
erDb));
appUserDb.getPrimaryKeyGenerator();
// APP_ERROR DbEntity
DbEntity appErrorDb =3D new DbEntity("APP_ERROR");
DbAttribute appErrorIdDbAttr =3D new DbAttribute("ID", Types.INTEGER=
, appErrorDb);
appErrorDb.addAttribute(appErrorIdDbAttr);
appErrorDb.addAttribute(new DbAttribute("APP_USER_ID", Types.INTEGER=
, appErrorDb));
appErrorDb.addAttribute(new DbAttribute("ERROR_MESSAGE", Types.VARCH=
AR, appErrorDb));
// Add DbEntities to respective maps
mainMap.addDbEntity(appUserDb);
errorsMap.addDbEntity(appErrorDb);
// -------------------- DB Relationships --------------------
// APP_ERROR -> APP_USER (to-one)
DbRelationship toUserRel =3D new DbRelationship("appUser");
toUserRel.setSourceEntity(appErrorDb);
toUserRel.setTargetEntityName(appUserDb);
toUserRel.setToMany(false);
toUserRel.addJoin(new DbJoin(toUserRel, "APP_USER_ID", "ID"));
appErrorDb.addRelationship(toUserRel);
// APP_USER ->> APP_ERROR (to-many)
DbRelationship errorsRel =3D new DbRelationship("errors");
errorsRel.setSourceEntity(appUserDb);
errorsRel.setTargetEntityName(appErrorDb);
errorsRel.setToMany(true);
errorsRel.addJoin(new DbJoin(errorsRel, "ID", "APP_USER_ID"));
appUserDb.addRelationship(errorsRel);
// -------------------- ObjEntities --------------------
// APP_USER ObjEntity
ObjEntity appUserObj =3D new ObjEntity("AppUser");
appUserObj.setDbEntity(appUserDb);
appUserObj.addAttribute(new ObjAttribute("id", "java.lang.Integer", a=
ppUserObj));
appUserObj.addAttribute(new ObjAttribute("name", "java.lang.String",=
appUserObj));
appUserObj.addAttribute(new ObjAttribute("email", "java.lang.String"=
, appUserObj));
// APP_ERROR ObjEntity
ObjEntity appErrorObj =3D new ObjEntity("AppError");
appErrorObj.setDbEntity(appErrorDb);
appErrorObj.addAttribute(new ObjAttribute("id", "java.lang.Integer",=
appErrorObj));
appErrorObj.addAttribute(new ObjAttribute("errorMessage", "java.lang=
.String", appErrorObj));
// Add ObjEntities to respective maps
mainMap.addObjEntity(appUserObj);
errorsMap.addObjEntity(appErrorObj);
// -------------------- ObjRelationships --------------------
// AppError =C3=A2=C2=86=C2=92 AppUser (to-one)
ObjRelationship appUserRel =3D new ObjRelationship("appUser");
appUserRel.setSourceEntity(appErrorObj);
appUserRel.setTargetEntityName(appUserObj); // sets target name
appUserRel.setDbRelationshipPath("appUser");
appErrorObj.addRelationship(appUserRel);
// AppUser =C3=A2=C2=86=C2=92 AppError (to-many)
ObjRelationship errorsRelObj =3D new ObjRelationship("errors");
errorsRelObj.setSourceEntity(appUserObj);
errorsRelObj.setTargetEntityName(appErrorObj); // sets target name
errorsRelObj.setDbRelationshipPath("errors");
appUserObj.addRelationship(errorsRelObj);
=20
PrintWriter stdout =3D new PrintWriter(System.out, true);
XMLEncoder encoder =3D new XMLEncoder(stdout, "\t", "11");
EmptyConfigurationNodeVisitor visitor =3D new EmptyConfigurationNode=
Visitor();
=20
stdout.println(mainMap.getName());
stdout.println();
encoder.nested(mainMap, visitor);
=20
stdout.println();
stdout.println();
=20
stdout.println(errorsMap.getName());
stdout.println();
encoder.nested(errorsMap, visitor);
}
}
=20
>=20
> On Oct 14, 2025, at 3:29=E2=80=AFPM, Ricardo Parada <[email protected]> wrot=
e:
>=20
> =EF=BB=BFHello,
>=20
> I wrote a converter to convert our eomodels from EOF to Cayenne-project.xm=
l and data maps .map.xml
>=20
> So far so good. However, I seem to be running into a problem for cross dat=
a map relationships.
>=20
> I=E2=80=99m using 5.0-M1.
>=20
> When I=E2=80=99m creating the ObjRelationship I call:
>=20
> objRel.setTargetEntityName(targetObjEntity);
>=20
> If I then call objRel.getTargetEntityName() it returns the correct target e=
ntity name. =20
>=20
> However, if I call objRel.getTargetEntity() it returns null.
>=20
> I debugged the code and I can see that getTargetEntity() is looking for th=
e target entity name in the data map corresponding to the source ObjEntity. A=
nd it does not find it and returns null for that reason.
>=20
> As a result of that, when I call encodeAsXML() on objRel I see that the XM=
L is missing the target attribute in the obj-relationship xml tag.
>=20
> Is this a bug in 5.0-M1 or am I missing something when creating my ObjRela=
tionship programmatically.
>=20
> I could put all eomodels that have cross eomodel relationships into the sa=
me data map to avoid this problem.
>=20
> But when I create it in Cayenne Modeler 4.3.3 it seems to work. I see the o=
bj-relationship in the XML have target correctly set.
>=20
> Thank you all in advance.
> Ricardo Parada