Java 在 Hibernate 中更改生成的外键名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16564789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing the generated name of a foreign key in Hibernate
提问by yovan786
@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).
我的 UserDetails 类与实体类 Vehicle 具有一对一的映射关系。Hibernate
创建 2 个表并分配一个通用外键,它将 Vehicle_id 列(UserDetails 表)映射到主键 VehicleId(Vehicle 表)。
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.
我的问题是:我们如何将这个生成的外键更改为有意义的内容,例如 Fk_userdetails_vehicle。
采纳答案by Etienne Miret
Since JPA 2.1, you can use the @javax.persistence.ForeignKeyannotation:
从 JPA 2.1 开始,您可以使用@javax.persistence.ForeignKey注释:
@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.ForeignKeyannotation, but this is now deprecated:
在 JPA 2.1 之前,您可以使用 Hibernate 的@org.hibernate.annotations.ForeignKey注释,但现在已弃用:
@OneToOne()
@JoinColumn(name="vehicle_id", referencedColumnName="vehicleId")
@ForeignKey(name="Fk_userdetails_vehicle")
public Vehicle getVehicle() {
return vehicle;
}
回答by Wayan Wiprayoga
May be you should try this, adding @ForeignKey
annotation :
可能你应该试试这个,添加@ForeignKey
注释:
@ManyToOne
@ForeignKey(name="FK_some_model")
@JoinColumn(name="some_model_id")
private SomeModel someModel
回答by Mohsen Kashi
Also you can use @ForeignKey
embedded in @JoinColumn
like this:
你也可以像这样使用@ForeignKey
嵌入 @JoinColumn
:
@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:
对于@ManyToMany
您可以像这样使用foreignKey
和inverseForeignKey
嵌入的关系@JoinTable
:
@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"))
回答by maaartinus
You can do it also by implementing ImplicitNamingStrategy.determineForeignKeyName
and using
您也可以通过实施ImplicitNamingStrategy.determineForeignKeyName
和使用来做到这一点
configuration.setImplicitNamingStrategy(
new MyImplicitNamingStrategy())
which is nice as you don't have to do it manually again and again. However, it may be hard to put the relevant information there. I tried to concat everything I got (using three underscore to separate the parts) and ended up with
这很好,因为您不必一次又一次地手动操作。但是,可能很难将相关信息放在那里。我试图连接我得到的所有东西(使用三个下划线来分隔部分)并最终得到
FK_ACCESS_TEMPLATE____TEMPLATE____TEMPLATE_ID____TEMPLATE_ID__INDEX_B
which isn't really better than
这并不比
FKG2JM5OO91HT64EWUACF7TJCFN_INDEX_B
I guess, using just the referenced table and column names together with a number for uniqueness would be just fine.
我想,只使用引用的表名和列名以及唯一性的数字就可以了。
Note also that this seems to be legacy Hibernate stuff, unsupported by JPA.
另请注意,这似乎是 JPA 不支持的遗留 Hibernate 内容。
OTOH it works with Hibernate 5.0.1 (just one week old).
OTOH 它适用于 Hibernate 5.0.1(仅一周大)。