本文介绍了在Hibernate中更改生成的外键名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
@OneToOne()
@JoinColumn(name =vehicle_id,referencedColumnName =vehicleId)
public Vehicle getVehicle(){
return车辆;
$ b我的UserDetails类与Entitity类Vehicle 。 Hibernate
创建2个表并分配一个通用外键,该外键将vehicle_id列(UserDetails表)映射到主键vehicleId(Vehicle table)。 KEY FKB7C889CEAF42C7A1(vehicle_id),
CONSTRAINT FKB7C889CEAF42C7A1 FOREIGN KEY(vehicle_id)REFERENCES vehicle(vehicleId)
$ c $我的问题是:我们如何将这个生成的外键更改为有意义的内容,例如Fk_userdetails_vehicle。
从JPA 2.1开始,您可以使用注释: @OneToOne()
@JoinColumn(name =vehicle_id,referencedColumnName =vehicleId,foreignKey = @ ForeignKey(name =Fk_userdetails_vehicle))
public Vehicle getVehicle(){
return车辆;
}
在JPA 2.1之前,可以使用Hibernate的注释,但现在已被弃用:
@OneToOne()
@JoinColumn(name =vehicle_id,referencedColumnName = vehicleId)
@ForeignKey(name =Fk_userdetails_vehicle)
public Vehicle getVehicle(){
return vehicle;
}
@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.
解决方案 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;
}
这篇关于在Hibernate中更改生成的外键名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!