为什么我们使用 @ForeignKey(name="FK_COUNTRY") 注解?
Why we use @ForeignKey(name="FK_COUNTRY") annotation?
我一直在休眠中处理一些相关的东西,在那里我得到了表之间关系的解决方案我试过了它工作正常但是当我删除 @ForeignKey(name="FK_COUNTRY") 没有任何变化那么我们为什么要使用这个注释是最佳实践?
@Entity
@Table(name = "state")
public class State {
@Id
@Column(name = "id")
private int id;
@Column(name = "name")
private String name;
@ManyToOne
@ForeignKey(name="FK_COUNTRY")
private Country country;
}
Hibernate 应该反映数据库结构,
您应该阅读 importance of foreign key
- Referential Integrity
- Easier Detective Work
- Better performance
如果您阅读 @ForeignKey
的 javadoc,您会发现:
Used to specify the handling of foreign key constraints when schema generation is in effect. If this annotation is not specified, the persistence provider's default foreign key strategy will be used.
如果您不从 class 定义(例如 CREATE TABLE
SQL 语句)生成数据库模式,则注释无效。
我一直在休眠中处理一些相关的东西,在那里我得到了表之间关系的解决方案我试过了它工作正常但是当我删除 @ForeignKey(name="FK_COUNTRY") 没有任何变化那么我们为什么要使用这个注释是最佳实践?
@Entity
@Table(name = "state")
public class State {
@Id
@Column(name = "id")
private int id;
@Column(name = "name")
private String name;
@ManyToOne
@ForeignKey(name="FK_COUNTRY")
private Country country;
}
Hibernate 应该反映数据库结构, 您应该阅读 importance of foreign key
- Referential Integrity
- Easier Detective Work
- Better performance
如果您阅读 @ForeignKey
的 javadoc,您会发现:
Used to specify the handling of foreign key constraints when schema generation is in effect. If this annotation is not specified, the persistence provider's default foreign key strategy will be used.
如果您不从 class 定义(例如 CREATE TABLE
SQL 语句)生成数据库模式,则注释无效。