Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to name the foreign key constraint of ManyToOne references since JPA 2.1?

@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?

like image 325
membersound Avatar asked Apr 11 '14 08:04

membersound


People also ask

How foreign key is defined in JPA entity?

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.

How hibernate define foreign key?

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 ?

What is @manytoone?

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.

What is <UNK>Manytoone JPA?

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.


2 Answers

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")) 
like image 102
JB Nizet Avatar answered Sep 28 '22 03:09

JB Nizet


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")) 
like image 26
Blekit Avatar answered Sep 28 '22 04:09

Blekit