001package io.ebean.annotation; 002 003import java.lang.annotation.ElementType; 004import java.lang.annotation.Retention; 005import java.lang.annotation.RetentionPolicy; 006import java.lang.annotation.Target; 007 008/** 009 * Specify how a foreign key constraint should be defined. 010 * <p> 011 * We can specify if a constraint should not be defined at all or control the 012 * onDelete and onUpdate modes used. 013 * <p> 014 * <h3>Example: On delete cascade</h3> 015 * <pre>{@code 016 * 017 * @DbForeignKey(onDelete = ConstraintMode.CASCADE) 018 * @ManyToOne 019 * RelatedBean parent; 020 * 021 * }</pre> 022 * <p> 023 * <h3>Example: No foreign key</h3> 024 * <pre>{@code 025 * 026 * // No FK Constraint 027 * @DbForeignKey(noConstraint=true) 028 * @ManyToOne 029 * RelatedBean parent; 030 * 031 * }</pre> 032 * <p> 033 * <p> 034 * <h3>Example: On delete set null</h3> 035 * <pre>{@code 036 * 037 * @DbForeignKey(onDelete = ConstraintMode.SET_NULL) 038 * @ManyToOne 039 * RelatedBean parent; 040 * 041 * }</pre> 042 */ 043@Retention(RetentionPolicy.RUNTIME) 044@Target(ElementType.FIELD) 045public @interface DbForeignKey { 046 047 /** 048 * Specify the onDelete mode. Typically will default to RESTRICT (aka No Action). 049 */ 050 ConstraintMode onDelete() default ConstraintMode.RESTRICT; 051 052 /** 053 * Do NOT change this - seriously, don't do it. 054 * <p> 055 * Your primary keys should never change by design. We should orientate the design to 056 * support primary keys that change (and instead find better non mutating primary keys). 057 * Oracle go to the point of actively not supporting "on update" for this reason. 058 * </p> 059 * <p> 060 * So yes, we can specify the onUpdate mode but I don't expect anyone to change this. 061 * </p> 062 */ 063 ConstraintMode onUpdate() default ConstraintMode.RESTRICT; 064 065 /** 066 * Set to true when we do not wish any foreign key constraint to be created. 067 * When this is set to true the onDelete and onUpdate have no effect. 068 */ 069 boolean noConstraint() default false; 070 071 /** 072 * Set to true when we do not wish an index to be created on the foreign key column(s). 073 */ 074 boolean noIndex() default false; 075}