We can specify if a constraint should not be defined at all or control the onDelete and onUpdate modes used.
Example: On delete cascade
@DbForeignKey(onDelete = ConstraintMode.CASCADE)
@ManyToOne
RelatedBean parent;
Example: No foreign key
// No FK Constraint
@DbForeignKey(noConstraint=true)
@ManyToOne
RelatedBean parent;
Example: On delete set null
@DbForeignKey(onDelete = ConstraintMode.SET_NULL)
@ManyToOne
RelatedBean parent;
Example: Allow inconsistency:
// disable foreign keys and force ebean to use left joins
@DbForeignKey(noConstraint=true, forceLeftJoin=true)
// the reference column in the database will be NOT NULL
@ManyToOne(optional=false)
RelatedBean parent;
To create such a bean, 'parent' must not be null (because of 'optional=false').
There is no FK constraint, that enforces that 'parent' exists, so you can even use
a non existent (or not yet existent) reference bean with a particular ID and create.
that bean in a later step.
Ebean will also be enforced to use LEFT JOINs instead of INNER JOINS,
so that the query result wouldn't skip entries if a non-existent relation is involved.
Note: In this case, the 'parent' may not be null, but you'll
get an EntityNotFoundException when accessing properties of 'parent'
-
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionbooleanIf this is set to true, LEFT JOIN is always used in queries, because data may be inconsistent and we cannot rely to have matching join partners.booleanSet to true when we do not wish any foreign key constraint to be created.booleanSet to true when we do not wish an index to be created on the foreign key column(s).Specify the onDelete mode.Do NOT change this - seriously, don't do it.
-
Element Details
-
onDelete
ConstraintMode onDeleteSpecify the onDelete mode. Typically will default to RESTRICT (aka No Action).- Default:
RESTRICT
-
onUpdate
ConstraintMode onUpdateDo NOT change this - seriously, don't do it.Your primary keys should never change by design. We should orientate the design to support primary keys that change (and instead find better non mutating primary keys). Oracle go to the point of actively not supporting "on update" for this reason.
So yes, we can specify the onUpdate mode but I don't expect anyone to change this.
- Default:
RESTRICT
-
noConstraint
boolean noConstraintSet to true when we do not wish any foreign key constraint to be created. (either for performance reasons, or data may be inconsistent) When this is set to true the onDelete and onUpdate have no effect.Note: This setting controls only ddl-generation. By default, we still assume, that the data is consistent and we may use INNER JOINs.
- Default:
false
-
forceLeftJoin
boolean forceLeftJoinIf this is set to true, LEFT JOIN is always used in queries, because data may be inconsistent and we cannot rely to have matching join partners.Note: This setting controls only sql-generation (left join vs. join). You may need
noConstraint = truein most cases.- Default:
false
-
noIndex
boolean noIndexSet to true when we do not wish an index to be created on the foreign key column(s).- Default:
false
-