Hibernate递归对象

时间:2017-04-11 13:25:03

标签: java mysql spring hibernate jpa

我正在Java 8使用Spring 4.3.1.RELEASEJPA 2Hibernate 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个对象。

0 个答案:

没有答案