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;
}
撒科打诨
相关分类