#107 - Mapping - Add support for @PrimaryKeyJoinColumn/@PrimaryKeyJoinColumns

Adds Bi-directional support
This commit is contained in:
Rob Bygrave
2018-02-15 00:49:51 +13:00
parent 6a067c0cf7
commit 31cac60393
8 changed files with 278 additions and 18 deletions
@@ -797,8 +797,12 @@ public class BeanDescriptorManager implements BeanDescriptorMap {
// We only perform 'circular' checks etc after we have
// all the DeployBeanDescriptors created and in the map.
List<DeployBeanPropertyAssocOne<?>> primaryKeyJoinCheck = new ArrayList<>();
for (DeployBeanInfo<?> info : deployInfoMap.values()) {
checkMappedBy(info);
checkMappedBy(info, primaryKeyJoinCheck);
}
for (DeployBeanPropertyAssocOne<?> prop : primaryKeyJoinCheck) {
checkUniDirectionalPrimaryKeyJoin(prop);
}
for (DeployBeanInfo<?> info : deployInfoMap.values()) {
@@ -868,12 +872,14 @@ public class BeanDescriptorManager implements BeanDescriptorMap {
* relationships.
* </p>
*/
private void checkMappedBy(DeployBeanInfo<?> info) {
private void checkMappedBy(DeployBeanInfo<?> info, List<DeployBeanPropertyAssocOne<?>> primaryKeyJoinCheck) {
for (DeployBeanPropertyAssocOne<?> oneProp : info.getDescriptor().propertiesAssocOne()) {
if (!oneProp.isTransient()) {
if (oneProp.getMappedBy() != null) {
checkMappedByOneToOne(oneProp);
} else if (oneProp.isPrimaryKeyJoin()) {
primaryKeyJoinCheck.add(oneProp);
}
}
}
@@ -1071,6 +1077,21 @@ public class BeanDescriptorManager implements BeanDescriptorMap {
DeployTableJoin otherTableJoin = mappedAssocOne.getTableJoin();
otherTableJoin.copyWithoutType(tableJoin, true, tableJoin.getTable());
}
if (mappedAssocOne.isPrimaryKeyJoin()) {
// bi-directional PrimaryKeyJoin ...
mappedAssocOne.setPrimaryKeyJoin(false);
prop.setPrimaryKeyExport();
addPrimaryKeyJoin(prop);
}
}
private void checkUniDirectionalPrimaryKeyJoin(DeployBeanPropertyAssocOne<?> prop) {
if (prop.isPrimaryKeyJoin()) {
// uni-directional PrimaryKeyJoin ...
prop.setPrimaryKeyExport();
addPrimaryKeyJoin(prop);
}
}
/**
@@ -1546,7 +1567,10 @@ public class BeanDescriptorManager implements BeanDescriptorMap {
return changeLogListener;
}
public void addPrimaryKeyJoin(DeployBeanPropertyAssocOne<?> prop, DeployTableJoin inverse) {
public void addPrimaryKeyJoin(DeployBeanPropertyAssocOne<?> prop) {
String baseTable = prop.getDesc().getBaseTable();
DeployTableJoin inverse = prop.getTableJoin().createInverse(baseTable);
TableJoin inverseJoin = new TableJoin(inverse);
@@ -17,8 +17,8 @@ import io.ebean.config.dbplatform.DbEncryptFunction;
import io.ebean.util.AnnotationUtil;
import io.ebeaninternal.server.core.InternString;
import io.ebeaninternal.server.deploy.BeanProperty;
import io.ebeaninternal.server.deploy.DeployDocPropertyOptions;
import io.ebeaninternal.server.deploy.DbMigrationInfo;
import io.ebeaninternal.server.deploy.DeployDocPropertyOptions;
import io.ebeaninternal.server.deploy.generatedproperty.GeneratedProperty;
import io.ebeaninternal.server.el.ElPropertyValue;
import io.ebeaninternal.server.properties.BeanPropertyGetter;
@@ -284,6 +284,10 @@ public class DeployBeanProperty {
return desc.getFullName() + "." + name;
}
public DeployBeanDescriptor<?> getDesc() {
return desc;
}
/**
* Return the DB column length for character columns.
* <p>
@@ -16,7 +16,7 @@ public abstract class DeployBeanPropertyAssoc<T> extends DeployBeanProperty {
/**
* Persist settings.
*/
private final BeanCascadeInfo cascadeInfo = new BeanCascadeInfo();
protected final BeanCascadeInfo cascadeInfo = new BeanCascadeInfo();
/**
* The join table information.
@@ -1,5 +1,7 @@
package io.ebeaninternal.server.deploy.meta;
import javax.persistence.CascadeType;
/**
* Property mapped to a joined bean.
*/
@@ -9,6 +11,8 @@ public class DeployBeanPropertyAssocOne<T> extends DeployBeanPropertyAssoc<T> {
private boolean oneToOneExported;
private boolean primaryKeyJoin;
private boolean primaryKeyExport;
private boolean importedPrimaryKey;
@@ -118,11 +122,30 @@ public class DeployBeanPropertyAssocOne<T> extends DeployBeanPropertyAssoc<T> {
return columnPrefix;
}
public void setPrimaryKeyExport() {
this.primaryKeyExport = true;
/**
* Mark as PrimaryKeyJoin (we don't know which side is the export side initially).
*/
public void setPrimaryKeyJoin(boolean primaryKeyJoin) {
this.primaryKeyJoin = primaryKeyJoin;
}
public boolean isPrimaryKeyJoin() {
return primaryKeyJoin;
}
public boolean isPrimaryKeyExport() {
return primaryKeyExport;
}
/**
* Set as export side of OneToOne with PrimaryKeyJoin.
*/
public void setPrimaryKeyExport() {
this.primaryKeyExport = true;
this.oneToOneExported = true;
if (!cascadeInfo.isSave()) {
// we pretty much need to cascade save so turning that on automatically ...
cascadeInfo.setType(CascadeType.ALL);
}
}
}
@@ -8,13 +8,11 @@ import io.ebeaninternal.server.deploy.BeanTable;
import io.ebeaninternal.server.deploy.meta.DeployBeanProperty;
import io.ebeaninternal.server.deploy.meta.DeployBeanPropertyAssoc;
import io.ebeaninternal.server.deploy.meta.DeployBeanPropertyAssocOne;
import io.ebeaninternal.server.deploy.meta.DeployTableJoin;
import io.ebeaninternal.server.deploy.meta.DeployTableJoinColumn;
import io.ebeaninternal.server.query.SqlJoinType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.EmbeddedId;
@@ -212,12 +210,7 @@ public class AnnotationAssocOnes extends AnnotationParser {
if (!prop.isOneToOne()) {
throw new IllegalStateException("Expecting property " + prop.getFullBeanName() + " with PrimaryKeyJoinColumn to be a OneToOne?");
}
prop.setPrimaryKeyExport();
prop.setOneToOneExported();
if (!prop.getCascadeInfo().isSave()) {
// we pretty much need to cascade save so turning that on automatically ...
prop.getCascadeInfo().setType(CascadeType.ALL);
}
prop.setPrimaryKeyJoin(true);
if (!primaryKeyJoin.name().isEmpty()) {
log.warn("Automatically determining join columns and ignoring PrimaryKeyJoinColumn.name {} on {}", primaryKeyJoin.name(), prop.getFullBeanName());
@@ -232,9 +225,6 @@ public class AnnotationAssocOnes extends AnnotationParser {
String foreignColumn = beanTable(prop).getIdColumn();
prop.getTableJoin().addJoinColumn(new DeployTableJoinColumn(localPrimaryKey, foreignColumn, false, false));
DeployTableJoin inverse = prop.getTableJoin().createInverse(baseBeanTable.getBaseTable());
factory.addPrimaryKeyJoin(prop, inverse);
}
private void readEmbedded(DeployBeanPropertyAssocOne<?> prop, Embedded embedded) {