我有一个我发现的项目,一个类似于这样的模式
@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);
}
}foreignKey和inverseForeignKey在@JoinTable中的含义。谢谢。
发布于 2020-08-18 13:52:08
JPA文档是怎么说的?
如果您访问JoinTable注释文档,您将发现
foreignKey(可选),用于指定或控制表生成生效时对应于joinColumns元素的列的外键约束的生成。inverseForeignKey(可选),用于指定或控制表生成生效时对应于inverseJoinColumns元素的列的外键约束的生成。
你真的需要这些吗?
因此,您可以看到,这两个属性在模式生成期间定义了外键对比创建。不是外键本身。外键使用joinColumns和inverseJoinColumns属性定义。
现在,如果不放置这些foreignKey和inverseForeignKey,那么默认值是ConstraintMode.PROVIDER_DEFAULT,这意味着您正在使用的数据库(这里的Oracle)将决定外键约束的名称。
因此,除非您需要,否则不需要显式地提供foreignKey和inverseForeignKey。这些是可选属性。
更多改善协会的方法
OneToMany关联。没有joinColumns和inverseJoinColumns关联(所以没有什么能说明要映射哪一列)。应该用这种方式来定义关联。
@OneToMany( CascadeType.ALL = orphanRemoval,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”)私人集合outboundRules;JoinColumn来完成这项工作。考虑一下关联,如果它是单向的还是双向的,或者哪一方是关系的所有者。您将找到一些优化关联的选项。更多内容如下:
https://stackoverflow.com/questions/63469521
复制相似问题