@org.hibernate.annotations.ForeignKey
has been deprecated, but I cannot find any examples how the JPA 2.1 equivalent would have to look like?
@ManyToOne @ForeignKey(name = "FK_USER") //@deprecated Prefer the JPA 2.1 introduced {@link javax.persistence.ForeignKey} instead. private User user;
How is this to be implemented without the deprecated annotation?
Implementing With a Foreign Key in JPA. Note that we place the @OneToOne annotation on the related entity field, Address. Also, we need to place the @JoinColumn annotation to configure the name of the column in the users table that maps to the primary key in the address table.
Foreign key refers to single column or group of columns in table that link data present in another table through its primary key. A Foreign key can't exist without its parent key but viceversa is not true. Want to be a Hibernate Master ?
Many-to-One relationship in DBMS is a relationship between more than one instances of an entity with one instance of another entity. The relation can be stated as − A project can have more than one student working on it.
The Many-To-One mapping represents a single-valued association where a collection of entities can be associated with the similar entity. Hence, in relational database any more than one row of an entity can refer to the similar rows of another entity.
As the documentation indicates, this annotation can't be applied to anything:
@Target(value={})
It can thus only be used as part of another annotation (listed in the See Also section):
@JoinColumn(foreignKey = @ForeignKey(name = "FK_USER"))
You're right, I misread the documentation. It can be defined as a part of @JoinColumn
annotation.
It should look like that:
@JoinColumn(foreignKey = @ForeignKey(name = "FK_USER"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With