我正在Java 8
使用Spring 4.3.1.RELEASE
,JPA 2
和Hibernate 5.2.1.Final
以及MySQL
。
+-----+ +-------------+ +----------+ +------------------------+
| Job | | Person_Job | | Person | | Person_job_favourites |
+-----+ +-------------+ +----------+ +------------------------+
| ID | | PER_ID | | ID | | PER_ID |
| | | JOB_ID | | | | JOB_ID |
+-----+ +-------------+ +----------+ +------------------------+
我有一个Job
个对象。 Job
包含Person
个对象。 Person
对象的列表为Favourite Jobs
。
Job.java
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinTable(name = "person_job", joinColumns = {
@JoinColumn(name = "JOB_ID", referencedColumnName = "ID", unique = true) }, inverseJoinColumns = {
@JoinColumn(name = "PER_ID", referencedColumnName = "ID") })
private Person person;
Person.java
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "person_job_favourites", joinColumns = {
@JoinColumn(name = "PER_ID", referencedColumnName = "ID") }, inverseJoinColumns = {
@JoinColumn(name = "JOB_ID", referencedColumnName = "ID", unique = true) })
private Set<Job> favouriteJobs;
如您所见,存在递归,结果集会爆炸。
问题
我希望不必完全重新设计我的架构,因为我已经围绕这个做了很多开发。有没有办法在特定点停止递归?即当我查询Job
个对象时,返回Person
个对象及其Job
个收藏夹,但那些Job
个收藏夹不包含Person
个对象。