java - 有没有办法使用父实体字段(不是父对象)将子实体映射到父实体?
问题描述
我正在尝试对一对多的单向关联进行建模。我有两个实体,第一个(父)是:
@Entity
@Table(name = "customer")
public class Customer{
@Id
@Column(name = "ID")
private Long id;
第二个实体与客户具有多对一关系:
@Entity
@Table(name = "address")
public class Address{
@Id
@Column(name = "ID")
private Long id;
@ManyToOne(optional = false)
@JoinColumn(name = "CUSTOMER_ID", referencedColumnName = "ID")
private Customer customer;
我需要用 customer.id 替换 Address 类中的属性 Customer,这可能吗?
解决方案
您可以使用 Adress 实体中的 customerId 字段并使用 JPQL 手动加入它,但不建议这样做,也不会创建外键。
@Entity
@Table(name = "address")
public class Address{
@Id
@Column(name = "ID")
private Long id;
@Column(name = "CUSTOMER_ID")
private Long customerId;
推荐阅读
- javascript - 将登录放在弹出窗口中
- javascript - 获取在 animateMotion 中设置的路径上移动的移动 SVG 圆的坐标
- python - 创建一个二维列表,在重复分配索引时将值作为列附加
- python - 已经启动 BlockingScheduler 实例后,是否可以向 APScheduler 添加作业?
- scala - Akka - 如何阻止整个循环而不是收到的每条消息
- python - 从 Flask 重定向获取请求数据
- filesystems - Python 的 glob.glob 或 Path.glob 返回与系统不同的文件名
- go - GO - WaitGroups 引用中的指针或变量
- java - Surefire 在多模块 spring-boot 项目上
- excel - 在 SUMPRODUCT 中嵌套 INDEX 函数以聚合正确列中的值