Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the generated name of a foreign key in Hibernate

@OneToOne() @JoinColumn(name="vehicle_id", referencedColumnName="vehicleId") public Vehicle getVehicle() {     return vehicle; } 

My UserDetails class has a one-to-one mapping with the Entitity class Vehicle. Hibernate creates the 2 tables and assigns a generic Foreign Key, which maps the vehicle_id column (UserDetails table.) to the primary key vehicleId (Vehicle table).

KEY FKB7C889CEAF42C7A1 (vehicle_id), CONSTRAINT FKB7C889CEAF42C7A1 FOREIGN KEY (vehicle_id) REFERENCES vehicle (vehicleId) 

My question is : how do we change this generated foreign key, into something meaningful, like Fk_userdetails_vehicle for example.

like image 808
yovan786 Avatar asked May 15 '13 12:05

yovan786


People also ask

Can foreign key name be different?

A foreign key can also have different column names than the primary key. The foreign key and primary key can also have different default values. However, since values in the referenced table must be unique, default values are not much used and are rarely used for columns that are part of a primary key.

How do you name a foreign key?

Foreign key is a field in the database table that is a primary key in other tables. The naming conventions for a foreign key constraint should have an "FK_" prefix, followed by the target table name, followed by the source table name. The syntax should be "FK_<TargetTable>_<SourceTable>".

How do you make a foreign key a primary key in hibernate?

Use @PrimaryKeyJoinColumn and @PrimaryKeyJoinColumns annotations. From Hibernate manual: The @PrimaryKeyJoinColumn annotation does say that the primary key of the entity is used as the foreign key value to the associated entity.

What is foreign key in hibernate?

Introduction. In this post, we feature a comprehensive Example on Hibernate 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.


2 Answers

Since JPA 2.1, you can use the @javax.persistence.ForeignKey annotation:

@OneToOne() @JoinColumn(name="vehicle_id", referencedColumnName="vehicleId", foreignKey=@ForeignKey(name = "Fk_userdetails_vehicle")) public Vehicle getVehicle() {     return vehicle; } 

Prior to JPA 2.1, you could use Hibernate’s @org.hibernate.annotations.ForeignKey annotation, but this is now deprecated:

@OneToOne() @JoinColumn(name="vehicle_id", referencedColumnName="vehicleId") @ForeignKey(name="Fk_userdetails_vehicle") public Vehicle getVehicle() {    return vehicle; } 
like image 109
Étienne Miret Avatar answered Sep 21 '22 12:09

Étienne Miret


Also you can use @ForeignKey embedded in @JoinColumn like this:

@JoinColumn(name = "BAR_ID", foreignKey = @ForeignKey(name = FK_BAR_OF_FOO)) 

for @ManyToMany relations you can use foreignKey and inverseForeignKey embedded in @JoinTable like this:

@JoinTable(name = "ARC_EMPLOYEE_OF_BAR"         , joinColumns = {@JoinColumn(name = "BAR_ID")}         , inverseJoinColumns = {@JoinColumn(name = "EMPLOYEE_ID")}         , uniqueConstraints = {@UniqueConstraint(name = "ARC_UK_EMPLOYEE_OF_BAR", columnNames = {"EMPLOYEE_ID", "BAR_ID"})}         , foreignKey = @ForeignKey(name = "ARC_FK_BAR_OF_EMPLOYEE")         , inverseForeignKey = @ForeignKey(name = "ARC_FK_EMPLOYEE_OF_BAR")) 
like image 39
Mohsen Kashi Avatar answered Sep 25 '22 12:09

Mohsen Kashi