#225 - Mapping - Add / review support for @IdClass

This commit is contained in:
Rob Bygrave committed 2018-03-15 18:19:17 +13:00
1 parent 602a35d0b6
commit 4799c56e25
34 files changed
+765 -309

No files matched your search

@@ -69,7 +69,6 @@ public class InsertHandler extends DmlHandler {
// expecting a concatenated key that can
// be built from supplied AssocOne beans
withId = meta.deriveConcatenatedId(persistRequest);
} else if (meta.supportsGetGeneratedKeys()) {
// Identity with getGeneratedKeys
useGeneratedKeys = true;
@@ -2,7 +2,6 @@ package io.ebeaninternal.server.persist.dmlbind;
import io.ebean.bean.EntityBean;
import io.ebeaninternal.server.core.PersistRequestBean;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import io.ebeaninternal.server.deploy.BeanProperty;
import io.ebeaninternal.server.deploy.BeanPropertyAssocOne;
import io.ebeaninternal.server.persist.dml.GenerateDmlRequest;
@@ -15,7 +14,7 @@ import java.util.List;
/**
* Bindable for a EmbeddedId.
*/
public final class BindableIdEmbedded implements BindableId {
final class BindableIdEmbedded implements BindableId {
private final BeanPropertyAssocOne<?> embId;
@@ -23,10 +22,10 @@ public final class BindableIdEmbedded implements BindableId {
private final MatchedImportedProperty[] matches;
public BindableIdEmbedded(BeanPropertyAssocOne<?> embId, BeanDescriptor<?> desc) {
BindableIdEmbedded(BeanPropertyAssocOne<?> embId, MatchedImportedProperty[] matches) {
this.embId = embId;
this.props = embId.getProperties();
matches = MatchedImportedProperty.build(props, desc);
this.matches = matches;
}
@Override
@@ -67,13 +66,10 @@ public final class BindableIdEmbedded implements BindableId {
public void dmlBind(BindableRequest request, EntityBean bean) throws SQLException {
EntityBean idValue = (EntityBean) embId.getValue(bean);
for (BeanProperty prop : props) {
Object value = prop.getValue(idValue);
request.bind(value, prop);
}
request.setIdValue(idValue);
}
@@ -88,7 +84,7 @@ public final class BindableIdEmbedded implements BindableId {
public boolean deriveConcatenatedId(PersistRequestBean<?> persist) {
if (matches == null) {
String m = "Matches for the concatenated key columns where not found?"
String m = "No matches for " + embId.getFullBeanName() + " the concatenated key columns where not found?"
+ " I expect that the concatenated key was null, and this bean does"
+ " not have ManyToOne assoc beans matching the primary key columns?";
throw new PersistenceException(m);
@@ -100,8 +96,8 @@ public final class BindableIdEmbedded implements BindableId {
EntityBean newId = (EntityBean) embId.createEmbeddedId();
// populate it from the assoc one id values...
for (MatchedImportedProperty matche : matches) {
matche.populate(bean, newId);
for (MatchedImportedProperty match : matches) {
match.populate(bean, newId);
}
// support PropertyChangeSupport
@@ -27,9 +27,11 @@ public class FactoryBaseProperties {
public void create(List<Bindable> list, BeanDescriptor<?> desc, DmlMode mode, boolean withLobs) {
for (BeanProperty prop : desc.propertiesBaseScalar()) {
Bindable item = factoryProperty.create(prop, mode, withLobs);
if (item != null) {
list.add(item);
if (!prop.isImportedPrimaryKey()) {
Bindable item = factoryProperty.create(prop, mode, withLobs);
if (item != null) {
list.add(item);
}
}
}
}
@@ -27,18 +27,14 @@ public class FactoryEmbedded {
BeanPropertyAssocOne<?>[] embedded = desc.propertiesEmbedded();
for (BeanPropertyAssocOne<?> anEmbedded : embedded) {
BeanProperty[] props = anEmbedded.getProperties();
List<Bindable> bindList = new ArrayList<>(props.length);
for (BeanProperty prop : props) {
Bindable item = factoryProperty.create(prop, mode, withLobs);
if (item != null) {
bindList.add(item);
}
}
list.add(new BindableEmbedded(anEmbedded, bindList));
}
}
@@ -20,14 +20,14 @@ public class FactoryId {
BeanProperty id = desc.getIdProperty();
if (id == null) {
return new BindableIdEmpty();
}
if (!id.isEmbedded()) {
return new BindableIdScalar(id);
} else {
BeanPropertyAssocOne<?> embId = (BeanPropertyAssocOne<?>) id;
return new BindableIdEmbedded(embId, desc);
MatchedImportedProperty[] matches = MatchedImportedFactory.build(embId.getProperties(), desc);
return new BindableIdEmbedded(embId, matches);
}
}
}
@@ -0,0 +1,35 @@
package io.ebeaninternal.server.persist.dmlbind;
import io.ebean.bean.EntityBean;
import io.ebeaninternal.server.deploy.BeanProperty;
import io.ebeaninternal.server.deploy.BeanPropertyAssocOne;
/**
* Matches local embedded id properties to 'matching' properties from a
* ManyToOne associated bean that is a 'imported primary key'.
*/
class MatchedImportedEmbedded implements MatchedImportedProperty {
private final BeanProperty localProp;
private final BeanPropertyAssocOne<?> assocOne;
private final BeanProperty foreignProp;
MatchedImportedEmbedded(BeanProperty localProp, BeanPropertyAssocOne<?> assocOne, BeanProperty foreignProp) {
this.localProp = localProp;
this.assocOne = assocOne;
this.foreignProp = foreignProp;
}
@Override
public void populate(EntityBean sourceBean, EntityBean embeddedId) {
Object assocBean = assocOne.getValue(sourceBean);
if (assocBean == null) {
throw new NullPointerException("The assoc bean for " + assocOne + " is null?");
}
Object value = foreignProp.getValue((EntityBean) assocBean);
localProp.setValue(embeddedId, value);
}
}
@@ -0,0 +1,54 @@
package io.ebeaninternal.server.persist.dmlbind;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import io.ebeaninternal.server.deploy.BeanProperty;
import io.ebeaninternal.server.deploy.BeanPropertyAssocOne;
class MatchedImportedFactory {
/**
* Create the array of matchedImportedProperty based on the properties and descriptor.
*/
protected static MatchedImportedProperty[] build(BeanProperty[] props, BeanDescriptor<?> desc) {
MatchedImportedProperty[] matches = new MatchedImportedProperty[props.length];
for (int i = 0; i < props.length; i++) {
// find matching assoc one property for dbColumn
matches[i] = findMatch(props[i], desc);
if (matches[i] == null) {
// ok, the assoc ones are not on the bean?
return null;
}
}
return matches;
}
private static MatchedImportedProperty findMatch(BeanProperty prop, BeanDescriptor<?> desc) {
// find matching against the local database column
String dbColumn = prop.getDbColumn();
BeanPropertyAssocOne<?>[] assocOnes = desc.propertiesOne();
for (BeanPropertyAssocOne<?> assocOne1 : assocOnes) {
if (assocOne1.isImportedPrimaryKey()) {
// search using the ImportedId from the assoc one
BeanProperty foreignMatch = assocOne1.getImportedId().findMatchImport(dbColumn);
if (foreignMatch != null) {
return new MatchedImportedEmbedded(prop, assocOne1, foreignMatch);
}
}
}
BeanProperty[] scalar = desc.propertiesBaseScalar();
for (BeanProperty beanProperty : scalar) {
if (dbColumn.equals(beanProperty.getDbColumn())) {
return new MatchedImportedScalar(prop, beanProperty);
}
}
// there was no matching assoc one property.
// example UserRole bean missing assoc one to User?
return null;
}
}
@@ -1,86 +1,11 @@
package io.ebeaninternal.server.persist.dmlbind;
import io.ebean.bean.EntityBean;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import io.ebeaninternal.server.deploy.BeanProperty;
import io.ebeaninternal.server.deploy.BeanPropertyAssocOne;
/**
* Matches local embedded id properties to 'matching' properties from a
* ManyToOne associated bean that is a 'imported primary key'.
* <p>
* This object is designed to help BindableIdEmbedded and BindableIdMap to
* create a concatenated id from the id values from ManyToOne associated beans.
* This can be done when those ManyToOne associated beans make up the primary
* key. This typically means the BindableIdEmbedded is for a intersection table
* of a Many to Many relationship.
* </p>
*/
class MatchedImportedProperty {
private final BeanPropertyAssocOne<?> assocOne;
private final BeanProperty foreignProp;
private final BeanProperty localProp;
protected MatchedImportedProperty(BeanPropertyAssocOne<?> assocOne, BeanProperty foreignProp,
BeanProperty localProp) {
this.assocOne = assocOne;
this.foreignProp = foreignProp;
this.localProp = localProp;
}
protected void populate(EntityBean sourceBean, EntityBean destBean) {
Object assocBean = assocOne.getValue(sourceBean);
if (assocBean == null) {
String msg = "The assoc bean for " + assocOne + " is null?";
throw new NullPointerException(msg);
}
Object value = foreignProp.getValue((EntityBean) assocBean);
localProp.setValue(destBean, value);
}
public interface MatchedImportedProperty {
/**
* Create the array of matchedImportedProperty based on the properties and descriptor.
* Populate the embeddedId bean from the source entity.
*/
protected static MatchedImportedProperty[] build(BeanProperty[] props, BeanDescriptor<?> desc) {
MatchedImportedProperty[] matches = new MatchedImportedProperty[props.length];
for (int i = 0; i < props.length; i++) {
// find matching assoc one property for dbColumn
matches[i] = MatchedImportedProperty.findMatch(props[i], desc);
if (matches[i] == null) {
// ok, the assoc ones are not on the bean?
return null;
}
}
return matches;
}
private static MatchedImportedProperty findMatch(BeanProperty prop, BeanDescriptor<?> desc) {
// find matching against the local database column
String dbColumn = prop.getDbColumn();
BeanPropertyAssocOne<?>[] assocOnes = desc.propertiesOne();
for (BeanPropertyAssocOne<?> assocOne1 : assocOnes) {
if (assocOne1.isImportedPrimaryKey()) {
// search using the ImportedId from the assoc one
BeanProperty foreignMatch = assocOne1.getImportedId().findMatchImport(dbColumn);
if (foreignMatch != null) {
return new MatchedImportedProperty(assocOne1, foreignMatch, prop);
}
}
}
// there was no matching assoc one property.
// example UserRole bean missing assoc one to User?
return null;
}
void populate(EntityBean sourceBean, EntityBean embeddedId);
}
@@ -0,0 +1,27 @@
package io.ebeaninternal.server.persist.dmlbind;
import io.ebean.bean.EntityBean;
import io.ebeaninternal.server.deploy.BeanProperty;
/**
* Matches local embedded id properties to 'matching' imported primary key scalar properties.
*/
class MatchedImportedScalar implements MatchedImportedProperty {
private final BeanProperty localProp;
private final BeanProperty foreignProp;
MatchedImportedScalar(BeanProperty localProp, BeanProperty foreignProp) {
this.localProp = localProp;
this.foreignProp = foreignProp;
}
@Override
public void populate(EntityBean sourceBean, EntityBean embeddedId) {
Object value = foreignProp.getValue(sourceBean);
localProp.setValue(embeddedId, value);
}
}