Hibernate版本 - 5.3.4.Final,mysql-connector版本 - 8.0.12
我在帖子和帖子内容之间有一个@OneToOne
关系:
帖子:
@Entity
@Table(name = "postsInfo")
public class PostsInfo {
private long postId;
private String title;
private java.util.Date createDate;
private Integer views;
private Collection<Tags> tagsPost;
private PostContent postContent;
private PostImage postImage;
private UserInfo userInfo;
private List<PostsComments> postsComments;
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "post_id", unique = true, nullable = false)
public long getPostId() {
return postId;
}
@Basic
@Column(name = "title", nullable = false)
public String getTitle() {
return title;
}
@Basic
@Column(name = "createDate", nullable = false)
public java.util.Date getCreateDate() {
return createDate;
}
@Basic
@Column(name = "views")
public Integer getViews() {
return views;
}
public PostsInfo(){}
public PostsInfo(String title, List<Tags> tagsPost) {
this.title = title;
this.tagsPost = tagsPost;
this.createDate = new Date();
}
@Fetch(FetchMode.SUBSELECT)
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "PostsTags",
joinColumns = @JoinColumn(name = "post_id"),
inverseJoinColumns = @JoinColumn(name = "tag_id"))
public Collection<Tags> getTagsPost() {
return tagsPost;
}
@OneToOne(cascade = CascadeType.ALL, mappedBy = "postsInfo", fetch = FetchType.LAZY, optional = false)
public PostContent getPostContent() {
return postContent;
}
@OneToOne(cascade = CascadeType.ALL, mappedBy = "postsInfo", orphanRemoval = true, fetch = FetchType.EAGER)
public PostImage getPostImage() {
return this.postImage;
}
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "user_id", nullable = false)
public UserInfo getUserInfo() {
return userInfo;
}
@Cascade(org.hibernate.annotations.CascadeType.ALL)
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "postsInfo")
public List<PostsComments> getPostsComments() {
return postsComments;
}
}
帖子内容:
@Entity
@Table(name = "postContent")
public class PostContent {
private long postId;
private String content;
private String subtitle;
private List<PostInsideImages> postInsideImages;
private PostsInfo postsInfo;
@GenericGenerator(name = "generator", strategy = "foreign",
parameters = @org.hibernate.annotations.Parameter(name = "property", value = "postsInfo"))
@Id @GeneratedValue(generator = "generator")
@Column(name = "post_id")
public long getPostId() {
return postId;
}
@Basic
@Column(name = "content")
public String getContent() {
return content;
}
@Basic
@Column(name = "subtitle")
public String getSubtitle() {
return subtitle;
}
public PostContent(){}
public PostContent(String subtitle ,String content) {
this.content = content;
this.subtitle = subtitle;
}
public PostContent(String subtitle, String content, List<PostInsideImages> postInsideImages) {
this.content = content;
this.subtitle = subtitle;
this.postInsideImages = postInsideImages;
}
@OneToMany( cascade = CascadeType.ALL, mappedBy = "postContent")
@LazyCollection(LazyCollectionOption.FALSE)
public List<PostInsideImages> getPostInsideImages() {
return postInsideImages;
}
@OneToOne(fetch = FetchType.LAZY, optional = false)
@PrimaryKeyJoinColumn
public PostsInfo getPostsInfo() {
return postsInfo;
}
}
我一直试图让PostContent加载懒惰,但没有任何作用。
我添加到@OneToOne
关系fetch = FetchType.LAZY,optional = false,
我在hibernate.cfg.xml中添加了这个属性:
<property name="hibernate.enhancer.enableLazyInitialization">true</property>
和这个maven插件:
<plugin>
<groupId>org.hibernate.orm.tooling</groupId>
<artifactId>hibernate-enhance-maven-plugin</artifactId>
<version>5.3.4.Final</version>
<executions>
<execution>
<configuration>
<failOnError>true</failOnError>
<enableLazyInitialization>true</enableLazyInitialization>
</configuration>
<goals>
<goal>enhance</goal>
</goals>
</execution>
</executions>
</plugin>
启用字节码增强,
添加@LazyToOne(LazyToOneOption.NO_PROXY)
,
我尝试更改hibernate版本(5.2,5.1.10,5.3.3,5.3.1),但仍然hibernate加载帖子内容渴望。
我获取帖子的代码:
@Override
public PostsInfo getCommonPost(long id){
return session.find(PostsInfo.class, id);
}
Hibernate sql:
Hibernate: select postsinfo0_.post_id as post_id1_9_0_, postsinfo0_.createDate as createDa2_9_0_, postsinfo0_.title as title3_9_0_, postsinfo0_.user_id as user_id5_9_0_, postsinfo0_.views as views4_9_0_, postimage1_.post_id as post_id1_6_1_, postimage1_.image_l as image_l2_6_1_, postimage1_.image_m as image_m3_6_1_, postimage1_.image_s as image_s4_6_1_ from postsInfo postsinfo0_ left outer join postImage postimage1_ on postsinfo0_.post_id=postimage1_.post_id where postsinfo0_.post_id=?
Hibernate: select postconten0_.post_id as post_id1_5_0_, postconten0_.content as content2_5_0_, postconten0_.subtitle as subtitle3_5_0_ from postContent postconten0_ where postconten0_.post_id=?
Hibernate: select postinside0_.post_id as post_id3_7_0_, postinside0_.image_id as image_id1_7_0_, postinside0_.image_id as image_id1_7_1_, postinside0_.image as image2_7_1_, postinside0_.post_id as post_id3_7_1_ from postInsideImages postinside0_ where postinside0_.post_id=?
Hibernate: select tagspost0_.post_id as post_id1_10_0_, tagspost0_.tag_id as tag_id2_10_0_, tags1_.tag_id as tag_id1_15_1_, tags1_.description as descript2_15_1_, tags1_.name as name3_15_1_ from PostsTags tagspost0_ inner join Tags tags1_ on tagspost0_.tag_id=tags1_.tag_id where tagspost0_.post_id=?
我能做些什么才能得到一个懒惰的负载?
@Danil Eltsov,这意味着你要对该相关集合进行另一次查询。无论如何,延迟加载都会产生另一个查询。
您的数据库中有一种设计气味。因为您没有与父表PK共享子表PK。一对一协会总是分享PK。在你的情况下,你的PostContent类的postId应该只由PostsInfo类的postId映射。但是在PostContent中,对于PostsInfo你有单独的PK和FK,对于一对一的关系应该是相同的,这也是hibernate所倡导的。因为如果你有单独的FK和PK,那么一个PostsInfo可能有多个PostContent。请仔细阅读以下链接,因为其中说明的场景与您的场景非常相似。
https://vladmihalcea.com/the-best-way-to-map-a-onetoone-relationship-with-jpa-and-hibernate/