リレーションペア
@ManyToOneは、@OneToMany/@ManyToOneリレーションペアの"one"側と考えることができます。
したがって、外部キー列にマッピングされる側です。
@Entity
public class Contact ...
// maps to "customer_id" foreign key column
@ManyToOne
Customer customer;
...
外部キー列が、プロパティ名 + "_id"に基づくネーミング規則と一致しない場合、@JoinColumnを定義する必要があります。
@Entity
public class Contact ...
// explicit @JoinColumn of "cust_id" as the foreign key column
@ManyToOne @JoinColumn("cust_id")
Customer customer;
...
optional=false
基礎にある外部キーにNOT NULL制約を課す必要がある場合、optional=falseを指定します。
@Entity
public class Contact ...
// not null constraint as optional=false
@ManyToOne(optional=false)
Customer customer;
...
@NotNull
optional=falseの代わりにjavaxバリデーション@NotNullを使用して、基礎にある外部キー列にNOT NULL制約があることを定義できます。
@Entity
public class Contact ...
// @NotNull same as optional=false
@NotNull @ManyToOne
Customer customer;
...