Java @JoinTable注释中的foreignKey和inverseForeignKey是什么?
我正在做一个项目,我发现了一个类似这样的模式Java @JoinTable注释中的foreignKey和inverseForeignKey是什么?,java,spring,hibernate,jpa,Java,Spring,Hibernate,Jpa,我正在做一个项目,我发现了一个类似这样的模式 @NoArgsConstructor @AllArgsConstructor @Builder @Data @EqualsAndHashCode @ToString(callSuper = true) @IdClass(QuarantinePolicyIdentity.class) @Entity(name = PolicyServiceConstants.QUARANTINE_POLICY_TABLE) @Table(uniqueConstrai
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Data
@EqualsAndHashCode
@ToString(callSuper = true)
@IdClass(QuarantinePolicyIdentity.class)
@Entity(name = PolicyServiceConstants.QUARANTINE_POLICY_TABLE)
@Table(uniqueConstraints = @UniqueConstraint(name = PolicyServiceConstants.QUARANTINE_POLICY_UNIQUE_NAME_CONSTRAINT,
columnNames = {"customerUuid", "name"}),
indexes = {@Index(name = PolicyServiceConstants.QUARANTINE_POLICY_UNIQUE_INDEX,
columnList = "orderNo",
unique = false)
})
public class QuarantinePolicyEntity {
@Id
private String quarantinePolicyId;
@Id
@JsonIgnore
private String customerUuid;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
@JoinTable(name = PolicyServiceConstants.QUARANTINE_POLICY_OUTBOUND_RULE_TABLE,
foreignKey = @ForeignKey(name = PolicyServiceConstants.FOREIGN_KEY_QUARANTINE_POLICY_QPOR),
inverseForeignKey = @ForeignKey(name = PolicyServiceConstants.FOREIGN_KEY_QUARANTINE_POLICY_ORQP))
private Set<RuleEntity> outboundRules;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
@JoinTable(name = PolicyServiceConstants.QUARANTINE_POLICY_INBOUND_RULE_TABLE,
foreignKey = @ForeignKey(name = PolicyServiceConstants.FOREIGN_KEY_QUARANTINE_POLICY_QPIR),
inverseForeignKey = @ForeignKey(name = PolicyServiceConstants.FOREIGN_KEY_QUARANTINE_POLICY_IRQP))
private Set<RuleEntity> inboundRules;
private String createdBy;
private Date createdDate;
private String lastModifiedBy;
private Date lastModifiedDate;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof QuarantinePolicyEntity)) return false;
QuarantinePolicyEntity that = (QuarantinePolicyEntity) o;
return com.google.common.base.Objects.equal(getQuarantinePolicyId(), that.getQuarantinePolicyId()) &&
com.google.common.base.Objects.equal(getCustomerUuid(), that.getCustomerUuid());
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), quarantinePolicyId, customerUuid);
}
}
@noargsconstuctor
@AllArgsConstructor
@建筑商
@资料
@EqualsAndHashCode
@ToString(callSuper=true)
@IdClass(检疫政策类)
@实体(名称=PolicyServiceConstants.quantial\u POLICY\u表)
@表(uniqueConstraints=@UniqueConstraint(name=PolicyServiceConstants.quantial\u POLICY\u UNIQUE\u name\u CONSTRAINT),
columnNames={“CustomerUID”,“name”}),
索引={@Index(name=PolicyServiceConstants.quantial\u POLICY\u UNIQUE\u Index,
columnList=“orderNo”,
唯一=错误)
})
公共类检疫政策{
@身份证
私有字符串隔离策略;
@身份证
@杰索尼奥雷
私有字符串customeruid;
@OneToMany(cascade=CascadeType.ALL,orphanRemoving=true,fetch=FetchType.LAZY)
@JoinTable(name=PolicyServiceConstants.quantial\u POLICY\u OUTBOUND\u RULE\u TABLE,
foreignKey=@foreignKey(name=PolicyServiceConstants.FOREIGN\u KEY\u Quarantage\u POLICY\u QPOR),
inverseForeignKey=@ForeignKey(name=PolicyServiceConstants.FOREIGN\u KEY\u POLICY\u ORQP))
私人制定的规则;
@OneToMany(cascade=CascadeType.ALL,orphanRemoving=true,fetch=FetchType.LAZY)
@JoinTable(name=PolicyServiceConstants.quantial\u POLICY\u INBOUND\u RULE\u TABLE,
foreignKey=@foreignKey(name=PolicyServiceConstants.FOREIGN\u KEY\u Quarantage\u POLICY\u QPIR),
inverseForeignKey=@ForeignKey(name=PolicyServiceConstants.FOREIGN\u KEY\u POLICY\u IRQP))
私人设置的边界规则;
创建的私有字符串;
私人日期创建日期;
私有字符串lastModifiedBy;
私人日期最后修改日期;
@凌驾
公共布尔等于(对象o){
如果(this==o)返回true;
如果(!(o检疫政策实例))返回false;
检疫政策性,即=(检疫政策性)o;
返回com.google.common.base.Objects.equal(getQuarantinePolicyId(),that.getQuarantinePolicyId())&&
equal(getCustomerUID(),that.getCustomerUID());
}
@凌驾
公共int hashCode(){
返回Objects.hash(super.hashCode(),quarantinePolicyId,CustomerUID);
}
}
- 我无法理解
中的@JoinTable
和外键
是什么逆键
- 其次,我们是否必须提到FK,RuleEntity表,因为它如何知道使用哪个FK
JoinTable
注释文档,您将发现
foreignKey
(可选)用于指定或控制对应于
当表生成生效时,joinColumns
元素
反向外键
(可选)用于指定或控制对应列外键约束的生成
当表生成生效时,返回到inverseJoinColumns
元素
你真的需要这些吗?
因此,您可以看到这两个属性在模式生成期间定义了外键约束的创建。不是外键本身。外键是用joinColumns
和inverseJoinColumns
属性定义的
现在,如果您不放置这些外键
和反向外键
,则默认值为ConstraintMode。PROVIDER\u default
,这意味着您正在使用的数据库(此处为Oracle)将决定外键约束的名称
因此,除非您愿意,否则您实际上不需要显式地提供foreignKey
和inverseForeignKey
。这些是可选属性
更多改善关联的方法
- 我不知道这是不是一个工作代码。但实际上,
关联在这里没有正确定义。没有OneToMany
和joinColumns
关联(因此没有任何东西可以告诉我们映射哪个列)。应该这样定义关联inverseJoinColumns
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY) @JoinTable(name = "join_table_name", joinColumns = @JoinColumn(name = "column_a"), inverseJoinColumns = @JoinColumn(name = "column_b"), foreignKey = @ForeignKey(name = "name_of_the_constraint_for_column_a"), inverseForeignKey = @ForeignKey(name = "name_of_the_constraint_for_column_b")) private Set<RuleEntity> outboundRules;
@OneToMany(cascade=CascadeType.ALL,orphaneremovation=true,fetch=FetchType.LAZY) @JoinTable(name=“join\u table\u name”, joinColumns=@JoinColumn(name=“column_a”), inverseJoinColumns=@JoinColumn(name=“column_b”), foreignKey=@foreignKey(name=“列a的约束的名称”), inverseForeignKey=@ForeignKey(name=“列的约束的名称”)) 私人制定的规则;
- 此外,你甚至不需要一个可连接的来建立一个OneToMany关联。只需一个
就可以完成这项工作。想想这个关联,如果它是单向的或双向的,或者哪一方是关系的拥有方。您将找到一些选项来优化您的关联JoinColumn