HIbernate - 不要在Eager Association中加载Lazy Entities

时间:2015-10-05 18:35:58

标签: java performance hibernate hibernate-criteria

如果我的项目中有以下课程

    @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期望只从ApplicationImageVO类加载image1和image2,但事实并非如此

即使将它们标记为LAZY,也会加载ApplicationImageVO类中的所有其他对象。我希望只加载ApplicationHomeScreenVO对象

有没有办法阻止这些其他人加载?

谢谢 Damie

1 个答案:

答案 0 :(得分:1)

@OneToOne声明这种方式是由PK映射的,必须急切地提取......

你可以:

  • 将其设为@ManyToOne(fetch=FetchType.LAZY)

OR

  • 声明一个外键列:

@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;
}