Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA: Give a name to a Foreign Key on DB?

I have a simple questions. How can I give a name to the Foreign Key relations that arise from the @ ManyToOne annotation ?

like image 828
giulius Avatar asked Jul 07 '11 10:07

giulius


People also ask

How to name foreign key in JPA?

If you are interested in naming the column used in the foreign key, one may specify the name of the column used to create the foreign key, using the @JoinColumn annotation along with the @ManyToOne annotation.

How does JPA represent foreign key?

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.

What is the use of@ JoinColumn annotation?

Annotation Type JoinColumn. Specifies a column for joining an entity association or element collection. If the JoinColumn annotation itself is defaulted, a single join column is assumed and the default values apply. (Optional) The SQL fragment that is used when generating the DDL for the column.

What is JoinColumn in JPA?

JPA JAVA EE. @JoinColumn is used to specify a column for joining an entity association or element collection. This annotation indicates that the enclosing entity is the owner of the relationship and the corresponding table has a foreign key column which references to the table of the non-owning side.


3 Answers

With JPA 2.1 you can just do this with the foreignKey annotation:

import javax.persistence.ForeignKey;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@ManyToOne
@JoinColumn(name = "company_id", nullable = false, foreignKey = @ForeignKey(name="FK_COMPANY__ROUTE"))
private Company company;

Do not confuse with the deprecated hibernate equivalent

like image 172
borjab Avatar answered Oct 06 '22 14:10

borjab


As of JPA 2.1 it is possible to define foreign keys via @ForeignKey annotation.

Unfortunately, it is not very useful if you only need to change the name. If you specify custom name of the FK, you also have to specify SQL definition of the FK. That is at least the way it works in EclipseLink 2.5.0.

like image 25
Mareen Avatar answered Oct 06 '22 14:10

Mareen


If you are interested in naming the column used in the foreign key, one may specify the name of the column used to create the foreign key, using the @JoinColumn annotation along with the @ManyToOne annotation. The value of the name attribute of the @JoinColumn annotation is used by the JPA provider to map the column name in the table to the entity's attribute.

However, the name of the foreign key constraint created itself cannot be configured. At the time of writing this, it is not possible to specify the name of the foreign key constraint using a JPA annotation or configuration parameter in the OR mapping files. If you need to change the name of the foreign key constraint, then you must create the DDL statement yourself, instead of relying on the JPA provider to do this.

like image 21
Vineet Reynolds Avatar answered Oct 06 '22 13:10

Vineet Reynolds