Skip to content

Commit 9252154

Browse files
committed
CAY-2976 Exception creating a relationship for an Incomplete ObjEntity
1 parent e0cf8ce commit 9252154

4 files changed

Lines changed: 29 additions & 16 deletions

File tree

RELEASE-NOTES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ CAY-2966 "comment" field is lost when upgrading from v10 to v12
3939
CAY-2967 SQLTemplate/SQLSelect broken pagination
4040
CAY-2968 Vertical Inheritance: INSERT instead of UPDATE after updating flattened attribute
4141
CAY-2973 Exception trying to copy/paste a callback
42+
CAY-2976 Exception creating a relationship for an Incomplete ObjEntity
4243

4344
----------------------------------
4445
Release: 5.0-M2

modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/action/CreateRelationshipAction.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,11 @@ public void performAction(ActionEvent e) {
8686
ObjEntity objEnt = getProjectSession().getSelectedObjEntity();
8787
if (objEnt != null) {
8888

89-
new ObjRelationshipInfoDialog(getProjectSession(), app.getFrame())
90-
.createRelationship(objEnt)
91-
.open();
89+
if (ObjRelationshipInfoDialog.canMap(objEnt, app.getFrame())) {
90+
new ObjRelationshipInfoDialog(getProjectSession(), app.getFrame())
91+
.createRelationship(objEnt)
92+
.open();
93+
}
9294

9395
} else {
9496
DbEntity dbEnt = getProjectSession().getSelectedDbEntity();

modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/project/editor/objentity/properties/ObjRelationshipPanel.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,12 @@ private void edit(ActionEvent e) {
377377
}
378378

379379
ObjRelationshipTableModel model = (ObjRelationshipTableModel) table.getModel();
380+
ObjRelationship relationship = model.getRelationship(row);
381+
if (!ObjRelationshipInfoDialog.canMap(relationship.getSourceEntity(), app.getFrame())) {
382+
return;
383+
}
380384
new ObjRelationshipInfoDialog(session, app.getFrame())
381-
.modifyRelationship(model.getRelationship(row))
385+
.modifyRelationship(relationship)
382386
.open();
383387

384388
// This is required for a table to be updated properly

modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/project/editor/objentity/relinfo/ObjRelationshipInfoDialog.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import com.jgoodies.forms.builder.PanelBuilder;
2222
import com.jgoodies.forms.layout.CellConstraints;
2323
import com.jgoodies.forms.layout.FormLayout;
24-
import org.apache.cayenne.CayenneRuntimeException;
2524
import org.apache.cayenne.configuration.DataChannelDescriptor;
2625
import org.apache.cayenne.dbsync.naming.NameBuilder;
2726
import org.apache.cayenne.map.DbEntity;
@@ -151,10 +150,6 @@ public ObjRelationshipInfoDialog modifyRelationship(ObjRelationship rel) {
151150
this.relationship = rel;
152151
this.undo = new ObjRelationshipUndoableEdit(session, rel);
153152

154-
// current limitation is that an ObjRelationship must have source
155-
// and target entities present, with DbEntities chosen.
156-
validateCanMap();
157-
158153
initFromModel();
159154
initBindings();
160155
return this;
@@ -587,17 +582,28 @@ private void connectEnds() {
587582
}
588583

589584
/**
590-
* Checks if the entity can be edited with this inspector.
585+
* Checks whether the given source entity can be mapped with this inspector, showing an
586+
* informative message and returning false if not. Callers must use this to decide whether to
587+
* open the inspector at all — the dialog assumes a mappable source entity.
591588
* NOTE: As of CAY-1077, the inspector can be opened even with no target entity set.
592589
*/
593-
private void validateCanMap() {
594-
if (relationship.getSourceEntity() == null) {
595-
throw new CayenneRuntimeException("Can't map relationship without source entity.");
590+
public static boolean canMap(ObjEntity source, Window owner) {
591+
if (source == null) {
592+
showCannotMapMessage(owner, "This relationship has no source ObjEntity, so it can't be mapped.");
593+
return false;
596594
}
597-
if (getStartEntity() == null) {
598-
JOptionPane.showMessageDialog(this, "Can't map relationship without source DbEntity. Set source DbEntity.");
599-
throw new CayenneRuntimeException("Can't map relationship without source DbEntity.");
595+
if (source.getDbEntity() == null) {
596+
String message = """
597+
"ObjEntity '%s' is not mapped to a DbEntity. Set either DbEntity or super ObjEntity on the \
598+
"ObjEntity" tab before mapping relationships.""".formatted(source.getName());
599+
showCannotMapMessage(owner, message);
600+
return false;
600601
}
602+
return true;
603+
}
604+
605+
private static void showCannotMapMessage(Window owner, String message) {
606+
JOptionPane.showMessageDialog(owner, message, "Can't Map Relationship", JOptionPane.WARNING_MESSAGE);
601607
}
602608

603609
private DbEntity getStartEntity() {

0 commit comments

Comments
 (0)