gpt4 book ai didi

java - HIbernate - 不要在急切关联中加载惰性实体

转载 作者:行者123 更新时间:2023-12-01 11:05:54 24 4
gpt4 key购买 nike

如果我的项目中有以下类(class)

    @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,但事实并非如此

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

有什么办法可以阻止这些其他实体被加载吗?

谢谢达米

最佳答案

@OneToOne 以这种方式声明是通过 PK 映射的,并且必须急切地获取它......

你可以:

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

或者

  • 声明外键列:

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(名称=“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;
}

关于java - HIbernate - 不要在急切关联中加载惰性实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32955536/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com