@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.
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.
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>".
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.
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.
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; }
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"))
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