java HIbernate不加载渴望关联中的惰性实体
如果我的项目中有以下课程
@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类中的所有其他对象也会被加载,即使它们被标记为懒惰。我希望只加载ApplicationHomeScreenVO对象
有没有办法阻止这些其他实体被加载
多谢各位 达米
# 1 楼答案
@OneToOne声明这种方式是由PK映射的,必须急切地获取
你可以:
@ManyToOne(fetch=FetchType.LAZY)
或
@OneToOne(fetch = FetchType.LAZY) @JoinColumn(name="fk") public T getT()
编辑