HIbernate - 不要在 Eager Association 中加载惰性实体
HIbernate - dont load Lazy Entities in an Eager Association
如果我的项目中有以下classes
@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 class 但事实并非如此
ApplicationImageVO class 中的所有其他对象也会被加载,即使它们被标记为 LAZY。我希望只加载 ApplicationHomeScreenVO 对象
有什么方法可以阻止加载这些其他实体吗?
谢谢
大米
@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;
}
如果我的项目中有以下classes
@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 class 但事实并非如此
ApplicationImageVO class 中的所有其他对象也会被加载,即使它们被标记为 LAZY。我希望只加载 ApplicationHomeScreenVO 对象
有什么方法可以阻止加载这些其他实体吗?
谢谢 大米
@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;
}