2015-10-05 58 views
0

如果我在我的項目冬眠 - 不負載的渴望協會懶實體

@Entity 
@Table(name = "application_home_screen") 
public class ApplicationHomeScreenVO implements Serializable { 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "id", unique = true, nullable = false) 
    private Integer id; 

    @OneToOne(fetch = FetchType.EAGER) 
    @Cascade({ CascadeType.ALL }) 
    @JoinColumn(name="image1_id") 
    private ApplicationImageVO image1; 

    @OneToOne(fetch = FetchType.EAGER) 
    @Cascade({ CascadeType.ALL }) 
    @JoinColumn(name="image2_id") 
    private ApplicationImageVO image2; 
} 



@Entity 
@Table(name = "application_image") 
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) 
public class ApplicationImageVO implements Serializable { 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "id", unique = true, nullable = false) 
    private Integer id; 


    @OneToOne(fetch = FetchType.LAZY, mappedBy = "image1") 
    @Cascade({ CascadeType.ALL }) 
    private ApplicationHomeScreenVO homeScreenImage1; 

    @OneToOne(fetch = FetchType.LAZY, mappedBy = "image2") 
    @Cascade({ CascadeType.ALL }) 
    private ApplicationHomeScreenVO homeScreenImage2; 

    @OneToOne(fetch = FetchType.LAZY, mappedBy = "otherEntity1") 
    @Cascade({ CascadeType.ALL }) 
    private OtherEntity1 otherEntity1; 

    @OneToOne(fetch = FetchType.LAZY, mappedBy = "otherEntity2") 
    @Cascade({ CascadeType.ALL }) 
    private OtherEntity2 otherEntity2; 

    @OneToOne(fetch = FetchType.LAZY, mappedBy = "otherEntity3") 
    @Cascade({ CascadeType.ALL }) 
    private OtherEntity3 otherEntity3; 

    @OneToOne(fetch = FetchType.LAZY, mappedBy = "otherEntity4") 
    @Cascade({ CascadeType.ALL }) 
    private OtherEntity4 otherEntity3; 
} 

以下類當我加載ApplicationHomeVO - ID只期望圖像1和圖像2被從ApplicationImageVO類但加載情況並非如此

ApplicationImageVO類中的所有其他對象也會加載,即使它們標記爲LAZY。我預計只有ApplicationHomeScreenVO對象被加載

有什麼辦法阻止這些其他entites被加載?

謝謝 Damie

+0

「所有的其他圖像對象也加載即使它們被標記爲懶人」有沒有您發佈的代碼中的其他圖像對象。 – Tunaki

+0

道歉 - 更新了帖子 –

回答

1

@OneToOne宣佈這樣被PK映射,並且它必須是預先抓取...

,您可以:

  • 使其成爲@ManyToOne(fetch=FetchType.LAZY)

  • 聲明一個外鍵列:

@OneToOne(fetch = FetchType.LAZY) @JoinColumn(name="fk") public T getT()

編輯

@Entity 
@Table(name = "application_home_screen") 
public class ApplicationHomeScreenVO implements Serializable { 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "id", unique = true, nullable = false) 
    private Integer id; 

    @OneToOne(fetch = FetchType.EAGER) 
    @Cascade({ CascadeType.ALL }) 
    @JoinColumn(name="image1_id") 
    private ApplicationImageVO image1; 

    @OneToOne(fetch = FetchType.EAGER) 
    @Cascade({ CascadeType.ALL }) 
    @JoinColumn(name="image2_id") 
    private ApplicationImageVO image2; 
} 



@Entity 
@Table(name = "application_image") 
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) 
public class ApplicationImageVO implements Serializable { 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "id", unique = true, nullable = false) 
    private Integer id; 


    @OneToOne(fetch = FetchType.LAZY, mappedBy = "image1") 
    @Cascade({ CascadeType.ALL }) 
    private ApplicationHomeScreenVO homeScreenImage1; 

    @OneToOne(fetch = FetchType.LAZY, mappedBy = "image2") 
    @Cascade({ CascadeType.ALL }) 
    private ApplicationHomeScreenVO homeScreenImage2; 

    @ManyToOne(fetch = FetchType.LAZY, mappedBy = "otherEntity1") 
    @Cascade({ CascadeType.ALL }) 
    private OtherEntity1 otherEntity1; 

    @ManyToOne(fetch = FetchType.LAZY, mappedBy = "otherEntity2") 
    @Cascade({ CascadeType.ALL }) 
    private OtherEntity2 otherEntity2; 

    @ManyToOne(fetch = FetchType.LAZY, mappedBy = "otherEntity3") 
    @Cascade({ CascadeType.ALL }) 
    private OtherEntity3 otherEntity3; 

    @ManyToOne(fetch = FetchType.LAZY, mappedBy = "otherEntity4") 
    @Cascade({ CascadeType.ALL }) 
    private OtherEntity4 otherEntity3; 
} 
+0

在ApplicationImageVO類上使@ManyToOne成爲它嗎?和ApplicationHomeScreenVO類中提到的外鍵? –

+0

no no ...不是所有......你有選擇......只需將你的映射改爲「ManyToOne」......它應該可以工作(嘿,那就是映射到對象......好吧它是一對一的DB,但是在JPA中就是這樣......)...或者,添加一個外鍵並且不要鏈接該ID的... – Pras

+0

您會推薦哪種方法用於現有的數據庫結構? –