Skip to content

Commit

Permalink
#3133 - For IdClass match on property name
Browse files Browse the repository at this point in the history
  • Loading branch information
rbygrave committed Aug 10, 2023
1 parent 1ca73ca commit 2f233db
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static void visit(BeanDescriptor<?> descriptor, BeanPropertyVisitor visit

protected void visitProperties(BeanDescriptor<?> desc, BeanPropertyVisitor propertyVisitor) {
BeanProperty idProp = desc.idProperty();
if (idProp != null) {
if (idProp != null && !idProp.name().equals("_idClass")) {
visit(propertyVisitor, idProp);
}
BeanPropertyAssocOne<?> unidirectional = desc.unidirectional();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public void addToUpdate(PersistRequestBean<?> request, List<Bindable> list) {

@Override
public void dmlBind(BindableRequest request, EntityBean bean) throws SQLException {

EntityBean idValue = (EntityBean) embId.getValue(bean);
for (BeanProperty prop : props) {
Object value = prop.getValue(idValue);
Expand All @@ -75,14 +74,20 @@ public void dmlBind(BindableRequest request, EntityBean bean) throws SQLExceptio

@Override
public void dmlAppend(GenerateDmlRequest request) {
for (BeanProperty prop : props) {
request.appendColumn(prop.dbColumn());
if (matches != null) {
// prefer the match dbColumns over the embedded property ones
for (MatchedImportedProperty match : matches) {
request.appendColumn(match.dbColumn());
}
} else {
for (BeanProperty prop : props) {
request.appendColumn(prop.dbColumn());
}
}
}

@Override
public boolean deriveConcatenatedId(PersistRequestBean<?> persist) {

if (matches == null) {
String m = "No matches for " + embId.fullName() + " the concatenated key columns where not found?"
+ " I expect that the concatenated key was null, and this bean does"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ public void populate(EntityBean sourceBean, EntityBean embeddedId) {
localProp.setValue(embeddedId, value);
}

@Override
public String dbColumn() {
return localProp.dbColumn();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ private static MatchedImportedProperty findMatch(BeanProperty prop, BeanDescript
return new MatchedImportedScalar(prop, beanProperty);
}
}
// match on property name
for (BeanProperty beanProperty : desc.propertiesBaseScalar()) {
if (prop.name().equals(beanProperty.name())) {
return new MatchedImportedScalar(prop, beanProperty);
}
}
// there was no matching assoc one property.
// example UserRole bean missing assoc one to User?
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ interface MatchedImportedProperty {
* Populate the embeddedId bean from the source entity.
*/
void populate(EntityBean sourceBean, EntityBean embeddedId);

/**
* Return the DB column to use.
*/
String dbColumn();
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ public void populate(EntityBean sourceBean, EntityBean embeddedId) {
localProp.setValue(embeddedId, value);
}

@Override
public String dbColumn() {
return foreignProp.dbColumn();
}

}
39 changes: 39 additions & 0 deletions ebean-test/src/test/java/org/tests/cache/embeddedid/Concept2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.tests.cache.embeddedid;

import javax.persistence.*;

@Entity
@IdClass(ConceptId.class)
@SuppressWarnings("unused")
public class Concept2 {

@Id
@Column(name = "conn_id", nullable = false)
private String id;

@Id
@Column(name = "conn_network_id", nullable = false)
private String networkId;

private String label;


public Concept2(String networkId, String id, String label) {
this.networkId = networkId;
this.id = id;
this.label = label;
}

public String id() {
return id;
}

public String networkId() {
return networkId;
}

public String label() {
return label;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,10 @@ void createConnectionWithCompositeForeignKey() {
assertThat(sql.get(3)).contains("delete from concept where (network_id,id) in");
}
}

@Test
void insert() {
var c2 = new Concept2("A", "B", "foo");
DB.save(c2);
}
}

0 comments on commit 2f233db

Please sign in to comment.