【发布时间】:2019-05-24 09:48:16
【问题描述】:
我有一个包含两个模块的多模块应用程序:
- 部门管理
- 通信管理
现在在我的department-management 中有一个实体Department,在communication-management 模块中我有MailingGroup 实体。
通信管理也依赖于部门管理模块。
现在我想在Department和MailingGroup之间建立双向ManyToOne关系
@Entity
public class Department {
@OneToMany(mappedBy = "department")
List<MailingGroup> mailingGroups;
}
@Entity
public class MailingGroup{
@ManyToOne
@JoinColumn(name = "DEPARTMENT_ID")
Department department;
}
当然,这不能像上面那样存档,但是我可以使用接口存档这种双向关系吗?我最初的想法是这样解决它:
public interface MailingGroupProvider {
Department getDepartment()
}
@Entity
public class Department {
@OneToMany(mappedBy = "department")
List<MailingGroupProvider> mailingGroups;
}
@Entity
public class MailingGroup implements MailingGroupProvider {
@ManyToOne
@JoinColumn(name = "DEPARTMENT_ID")
Department department;
}
但它提出了问题:
- 在这种情况下这是首选解决方案吗?
- 我的接口应该提供哪些方法才能被 JPA 视为实体?
- 这甚至可能是我想要做的吗?
【问题讨论】:
标签: java spring hibernate jpa multi-module