2020-06-20

Hibernate JPA @ForeignKey Example

@ForeignKey

The @ForeignKey annotation is deprecated. Use the JPA 2.1 @ForeignKey annotation instead.

ForeignKey Define the foreign key name.

@ForeignKey

The @ForeignKey annotation is used to specify the associated foreign key of a @JoinColumn mapping. The @ForeignKey annotation is only used if the automated schema generation tool is enabled, in which case, it allows you to customize the underlying foreign key definition.


Example

@Entity(name = "Person")
public static class Person {

@Id
@GeneratedValue
private Long id;

//Getters and setters are omitted for brevity

}

@Entity(name = "Phone")
public static class Phone {

@Id
@GeneratedValue
private Long id;

@Column(name = "`number`")
private String number;

@ManyToOne
@JoinColumn(name = "person_id",
foreignKey = @ForeignKey(name = "PERSON_ID_FK")
)
private Person person;

//Getters and setters are omitted for brevity

}

No comments:

Post a Comment