#3133 - For IdClass match on property name

This commit is contained in:
Rob Bygrave
2023-08-10 23:22:57 +12:00
parent 1ca73ca6aa
commit 2f233db465
8 changed files with 75 additions and 5 deletions
@@ -22,7 +22,7 @@ public class VisitProperties {
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();
@@ -64,7 +64,6 @@ final class BindableIdEmbedded implements BindableId {
@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);
@@ -75,14 +74,20 @@ final class BindableIdEmbedded implements BindableId {
@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"
@@ -30,4 +30,8 @@ final class MatchedImportedEmbedded implements MatchedImportedProperty {
localProp.setValue(embeddedId, value);
}
@Override
public String dbColumn() {
return localProp.dbColumn();
}
}
@@ -39,6 +39,12 @@ final class MatchedImportedFactory {
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;
@@ -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();
}
@@ -22,4 +22,9 @@ final class MatchedImportedScalar implements MatchedImportedProperty {
localProp.setValue(embeddedId, value);
}
@Override
public String dbColumn() {
return foreignProp.dbColumn();
}
}
@@ -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;
}
}
@@ -47,4 +47,10 @@ class TestCompositeForeignKey extends BaseTestCase {
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);
}
}