#323 - DDL Generation support for foreign key cascade options

This commit is contained in:
Rob Bygrave committed 2018-02-19 22:24:37 +13:00
1 parent b8aa822a30
commit 40634412f5
31 files changed
+868 -133

No files matched your search

@@ -3,6 +3,7 @@ package io.ebeaninternal.server.deploy;
import io.ebean.BackgroundExecutor;
import io.ebean.Model;
import io.ebean.RawSqlBuilder;
import io.ebean.annotation.ConstraintMode;
import io.ebean.bean.BeanCollection;
import io.ebean.bean.EntityBean;
import io.ebean.config.EncryptKey;
@@ -1156,7 +1157,6 @@ public class BeanDescriptorManager implements BeanDescriptorMap {
// get the mappedBy property
DeployBeanProperty mappedProp = targetDesc.getBeanProperty(mappedBy);
if (mappedProp == null) {
String m = "Error on " + prop.getFullBeanName();
m += " Can not find mappedBy property [" + mappedBy + "] ";
m += "in [" + targetDesc + "]";
@@ -1179,6 +1179,19 @@ public class BeanDescriptorManager implements BeanDescriptorMap {
otherTableJoin.copyTo(tableJoin, true, tableJoin.getTable());
}
PropertyForeignKey foreignKey = mappedAssocOne.getForeignKey();
if (foreignKey != null) {
ConstraintMode onDelete = foreignKey.getOnDelete();
switch (onDelete) {
case SET_DEFAULT:
case SET_NULL:
case CASCADE: {
// turn off cascade delete when we are using the foreign
// key constraint to cascade the delete or set null
prop.getCascadeInfo().setDelete(false);
}
}
}
}
/**
@@ -41,6 +41,8 @@ public class BeanPropertyAssocOne<T> extends BeanPropertyAssoc<T> {
private final boolean primaryKeyExport;
private final PropertyForeignKey foreignKey;
private AssocOneHelp localHelp;
protected final BeanProperty[] embeddedProps;
@@ -69,6 +71,7 @@ public class BeanPropertyAssocOne<T> extends BeanPropertyAssoc<T> {
super(descriptor, deploy);
foreignKey = deploy.getForeignKey();
primaryKeyExport = deploy.isPrimaryKeyExport();
importedPrimaryKey = deploy.isImportedPrimaryKey();
oneToOne = deploy.isOneToOne();
@@ -309,6 +312,18 @@ public class BeanPropertyAssocOne<T> extends BeanPropertyAssoc<T> {
}
}
public PropertyForeignKey getForeignKey() {
return foreignKey;
}
public boolean hasForeignKey() {
return foreignKey == null || !foreignKey.isNoConstraint();
}
public boolean hasForeignKeyIndex() {
return foreignKey == null || !foreignKey.isNoIndex();
}
/**
* Return true if this a OneToOne property. Otherwise assumed ManyToOne.
*/
@@ -0,0 +1,35 @@
package io.ebeaninternal.server.deploy;
import io.ebean.annotation.ConstraintMode;
import io.ebean.annotation.DbForeignKey;
public class PropertyForeignKey {
private final boolean noIndex;
private final boolean noConstraint;
private final ConstraintMode onDelete;
private final ConstraintMode onUpdate;
public PropertyForeignKey(DbForeignKey dbForeignKey) {
this.noIndex = dbForeignKey.noIndex();
this.noConstraint = dbForeignKey.noConstraint();
this.onDelete = dbForeignKey.onDelete();
this.onUpdate = dbForeignKey.onUpdate();
}
public boolean isNoIndex() {
return noIndex;
}
public boolean isNoConstraint() {
return noConstraint;
}
public ConstraintMode getOnDelete() {
return onDelete;
}
public ConstraintMode getOnUpdate() {
return onUpdate;
}
}
@@ -1,5 +1,7 @@
package io.ebeaninternal.server.deploy.meta;
import io.ebeaninternal.server.deploy.PropertyForeignKey;
import javax.persistence.CascadeType;
/**
@@ -21,6 +23,8 @@ public class DeployBeanPropertyAssocOne<T> extends DeployBeanPropertyAssoc<T> {
private String columnPrefix;
private PropertyForeignKey foreignKey;
/**
* Create the property.
*/
@@ -148,4 +152,12 @@ public class DeployBeanPropertyAssocOne<T> extends DeployBeanPropertyAssoc<T> {
cascadeInfo.setType(CascadeType.ALL);
}
}
public void setForeignKey(PropertyForeignKey foreignKey) {
this.foreignKey = foreignKey;
}
public PropertyForeignKey getForeignKey() {
return foreignKey;
}
}
@@ -1,10 +1,12 @@
package io.ebeaninternal.server.deploy.parse;
import io.ebean.annotation.DbForeignKey;
import io.ebean.annotation.FetchPreference;
import io.ebean.annotation.Where;
import io.ebean.config.NamingConvention;
import io.ebeaninternal.server.deploy.BeanDescriptorManager;
import io.ebeaninternal.server.deploy.BeanTable;
import io.ebeaninternal.server.deploy.PropertyForeignKey;
import io.ebeaninternal.server.deploy.meta.DeployBeanProperty;
import io.ebeaninternal.server.deploy.meta.DeployBeanPropertyAssoc;
import io.ebeaninternal.server.deploy.meta.DeployBeanPropertyAssocOne;
@@ -89,6 +91,11 @@ public class AnnotationAssocOnes extends AnnotationParser {
prop.setNullable(false);
}
DbForeignKey dbForeignKey = get(prop, DbForeignKey.class);
if (dbForeignKey != null){
prop.setForeignKey(new PropertyForeignKey(dbForeignKey));
}
Where where = get(prop, Where.class);
if (where != null) {
// not expecting this to be used on assoc one properties