java - 使用表键映射一对多
问题描述
我有主桌商户和副桌交易:
商户:
@Entity
@Table
public class Merchants {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id;
@Column(length = 255)
private String name;
交易:
@Entity
@Table
public class PaymentTransactions {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private int id;
@Column(length = 4)
private int reference_transaction_id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "merchant_id")
private Merchants merchants;
如何将商家 ID(表键)配置为表交易的参考键作为表列merchant_id
?
解决方案
我怀疑您是否应该在 @JoinColumn 注释中使用 referencedColumnName 属性,并将 PaymentTransactions 更改为 List:
@Entity
@Table
public class Merchants {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(length = 255)
private String name;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "merchant")
List<PaymentTransactions> transactions;
}
@Entity
@Table
public class PaymentTransactions {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", updatable = false, nullable = false)
private int id;
@Column(length = 4)
private int reference_transaction_id;
@ManyToOne
// here referencedColumnName is id column of merchant entity
@JoinColumn(name = "merchant_id", referencedColumnName = "id", insertable = false, updatable = false)
private Merchants merchant;
}
推荐阅读
- r - 从字符串中动态选择传单颜色字段
- c - 遍历链表获取值有什么意义,我可以直接得到它吗?
- mysql - 如何在 DataGrip 中创建或连接到 localhost 数据库?
- angular - 在角度应用程序中导入 SpreadJS Charting 库的正确方法
- c# - 底部 UI 的空白空间(C#、UWP)
- c# - 如何推断通用委托类型的方法签名以调用委托列表
- android - 如何使用 RecyclerView 在片段中“findViewById”?
- node.js - knex 迁移导致早期 mocha 测试失败
- java - 如何捕捉“致命信号 11 (SIGSEGV)”?
- java - 动作侦听器在 Java 中单击时没有响应