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;
}
}
PostContent:
@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,可选= 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)
,
我尝试更改休眠版本(5.2、5.1.10、5.3.3、5.3.1),但仍然希望加载休眠内容。
我获得帖子的代码:
@Override
public PostsInfo getCommonPost(long id){
return session.find(PostsInfo.class, id);
}
休眠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=?
我可以做些什么来懒惰负载?
答案 0 :(得分:0)
@Danil Eltsov,这意味着您将对该相关集合进行另一个查询。无论如何,延迟加载会生成另一个查询。
答案 1 :(得分:0)
您的数据库中有设计气味。因为您没有与父表PK共享子表PK。一对一关联始终共享PK。在您的情况下,应仅通过PostsInfo类的postId映射PostContent类的postId。但是在PostContent中,PostsInfo具有单独的PK和FK,对于一对一关系而言,它们应该相同,冬眠提倡这一点。因为如果您有单独的FK和PK,则一个PostsInfo可能有多个PostContent。 请通过以下链接,因为其中说明的场景与您的场景非常相似。
https://vladmihalcea.com/the-best-way-to-map-a-onetoone-relationship-with-jpa-and-hibernate/