用户评论模块
用户评论模块分析
编写Comment表字段
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)privateLong id;//评论时间
privateString commentTime;//评论内容
privateString commentContent;//这条评论的所有回复
@OneToMany(targetEntity = Reply.class)
@JoinColumn(name="comment_id")private Set replys = new HashSet();//这条评论是评论谁的
@OneToOne
@JoinColumn(name= "comment_user_id")privateUser commentUser;//这条评论是谁发的
@ManyToOne(targetEntity = User.class)
@JoinColumn(name="user_id")privateUser user;//同意
@ManyToMany(mappedBy = "agreeComments")private Set agreeUsers = new HashSet();//不同意
@ManyToMany(mappedBy = "disagreeComments")private Set disagreeUsers = new HashSet();
一个用户发n条回复,一个回复回复一个用户
一条回复回复一条评论,一条评论下面有多条回复
谁发的回复 多对一
这条回复是回复哪一个评论的 多对一
这条回复是回复哪一个回复的 多对一
有那些回复是回复本条回复的 多对多
修改特定的Domain实体层,User用户实体,Comment评论实体层,Repy回复实体层
packagecom.Gary.betobe.domain;importjava.util.HashSet;importjava.util.Set;importjavax.persistence.CascadeType;importjavax.persistence.Entity;importjavax.persistence.GeneratedValue;importjavax.persistence.GenerationType;importjavax.persistence.Id;importjavax.persistence.JoinColumn;importjavax.persistence.JoinTable;importjavax.persistence.Lob;importjavax.persistence.ManyToMany;importjavax.persistence.OneToMany;
@Entitypublic classUser {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)privateLong id;privateString username;privateString password;privateString email;privateString firstName;privateString lastName;privateString displayName;//个人首页
privateString webUrl;privateString phone;//个人描述
@Lob//长文本 ->lob对应mysql的数据类型 longtext
privateString description;//social Link
privateString qqLink;privateString weixinLink;//封面头像
privateString coverImage;//头像
privateString headImage;//创建时间
privateString createTime;//用户发的所有评论
@OneToMany(targetEntity = Comment.class)
@JoinColumn(name= "user_id")private Set comments = new HashSet();//我关注了哪些用户//该用户关注了哪些用户 在保存的时候,会级联保存所有临时对象
@ManyToMany(cascade =CascadeType.PERSIST)
@JoinTable(
name="user_follow",
joinColumns= @JoinColumn(name = "user_id"),
inverseJoinColumns= @JoinColumn(name = "follow_id")
)private Set follows = new HashSet();//用户,同意了哪些评论
@ManyToMany
@JoinTable(
name="agree_user_comment",
joinColumns= @JoinColumn(name="user_id"),
inverseJoinColumns= @JoinColumn(name = "comment_id")
)private Set agreeComments = new HashSet();//用户,不同意了哪些评论
@ManyToMany
@JoinTable(
name="disagree_user_comment",
joinColumns= @JoinColumn(name="user_id"),
inverseJoinColumns= @JoinColumn(name="comment_id")
)private Set disagreeComments = new HashSet();//用户同意了哪些回复
@ManyToMany
@JoinTable(
name="agree_user_reply",
joinColumns= @JoinColumn(name="user_id"),
inverseJoinColumns= @JoinColumn(name="reply_id")
)private Set agreeReplys = new HashSet();//用户不同意了哪些回复
@ManyToMany
@JoinTable(
name="disagree_user_reply",
joinColumns=@JoinColumn(name="user_id"),
inverseJoinColumns= @JoinColumn(name="reply_id")
)private Set disagreeReplys = new HashSet<>();//JPA的标准
protectedUser()
{
}
@OverridepublicString toString() {return "User [id=" + id + ", username=" + username + ", password=" + password + ", email=" +email+ ", firstName=" + firstName + ", lastName=" + lastName + ", displayName=" + displayName + ", webUrl="
+ webUrl + ", phone=" + phone + ", description=" + description + ", qqLink=" + qqLink + ", weixinLink="
+ weixinLink + ", coverImage=" + coverImage + ", headImage=" + headImage + ", createTime=" +createTime+ ", comments=" + comments + ", follows=" + follows + ", agreeComments=" +agreeComments+ ", disagreeComments=" + disagreeComments + ", agreeReplys=" + agreeReplys + ", disagreeReplys="
+ disagreeReplys + "]";
}publicUser(Long id, String username, String password, String email, String firstName, String lastName,
String displayName, String webUrl, String phone, String description, String qqLink, String weixinLink,
String coverImage, String headImage, String createTime, Set comments, Setfollows,
Set agreeComments, Set disagreeComments, SetagreeReplys,
SetdisagreeReplys) {super();this.id =id;this.username =username;this.password =password;this.email =email;this.firstName =firstName;this.lastName =lastName;this.displayName =displayName;this.webUrl =webUrl;this.phone =phone;this.description =description;this.qqLink =qqLink;this.weixinLink =weixinLink;this.coverImage =coverImage;this.headImage =headImage;this.createTime =createTime;this.comments =comments;this.follows =follows;this.agreeComments =agreeComments;this.disagreeComments =disagreeComments;this.agreeReplys =agreeReplys;this.disagreeReplys =disagreeReplys;
}public SetgetAgreeReplys() {returnagreeReplys;
}public void setAgreeReplys(SetagreeReplys) {this.agreeReplys =agreeReplys;
}public SetgetDisagreeReplys() {returndisagreeReplys;
}public void setDisagreeReplys(SetdisagreeReplys) {this.disagreeReplys =disagreeReplys;
}public SetgetComments() {returncomments;
}public void setComments(Setcomments) {this.comments =comments;
}public SetgetAgreeComments() {returnagreeComments;
}public void setAgreeComments(SetagreeComments) {this.agreeComments =agreeComments;
}public SetgetDisagreeComments() {returndisagreeComments;
}public void setDisagreeComments(SetdisagreeComments) {this.disagreeComments =disagreeComments;
}publicLong getId() {returnid;
}public voidsetId(Long id) {this.id =id;
}publicString getUsername() {returnusername;
}public voidsetUsername(String username) {this.username =username;
}publicString getPassword() {returnpassword;
}public voidsetPassword(String password) {this.password =password;
}publicString getEmail() {returnemail;
}public voidsetEmail(String email) {this.email =email;
}publicString getFirstName() {returnfirstName;
}public voidsetFirstName(String firstName) {this.firstName =firstName;
}publicString getLastName() {returnlastName;
}public voidsetLastName(String lastName) {this.lastName =lastName;
}publicString getWebUrl() {returnwebUrl;
}public voidsetWebUrl(String webUrl) {this.webUrl =webUrl;
}publicString getPhone() {returnphone;
}public voidsetPhone(String phone) {this.phone =phone;
}publicString getDescription() {returndescription;
}public voidsetDescription(String description) {this.description =description;
}publicString getQqLink() {returnqqLink;
}public voidsetQqLink(String qqLink) {this.qqLink =qqLink;
}publicString getWeixinLink() {returnweixinLink;
}public voidsetWeixinLink(String weixinLink) {this.weixinLink =weixinLink;
}publicString getCoverImage() {returncoverImage;
}public voidsetCoverImage(String coverImage) {this.coverImage =coverImage;
}publicString getHeadImage() {returnheadImage;
}public voidsetHeadImage(String headImage) {this.headImage =headImage;
}publicString getCreateTime() {returncreateTime;
}public voidsetCreateTime(String createTime) {this.createTime =createTime;
}public SetgetFollows() {returnfollows;
}public void setFollows(Setfollows) {this.follows =follows;
}publicString getDisplayName() {returndisplayName;
}public voidsetDisplayName(String displayName) {this.displayName =displayName;
}
}
User.java
packagecom.Gary.betobe.domain;importjava.util.HashSet;importjava.util.Set;importjavax.persistence.Entity;importjavax.persistence.GeneratedValue;importjavax.persistence.GenerationType;importjavax.persistence.Id;importjavax.persistence.JoinColumn;importjavax.persistence.ManyToMany;importjavax.persistence.ManyToOne;importjavax.persistence.OneToMany;importjavax.persistence.OneToOne;
@Entitypublic classComment {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)privateLong id;//评论时间
privateString commentTime;//评论内容
privateString commentContent;//这条评论的所有回复
@OneToMany(targetEntity = Reply.class)
@JoinColumn(name="comment_id")private Set replys = new HashSet();//这条评论是评论谁的
@OneToOne
@JoinColumn(name= "comment_user_id")privateUser commentUser;//这条评论是谁发的
@ManyToOne(targetEntity = User.class)
@JoinColumn(name="user_id")privateUser user;//同意
@ManyToMany(mappedBy = "agreeComments")private Set agreeUsers = new HashSet();//不同意
@ManyToMany(mappedBy = "disagreeComments")private Set disagreeUsers = new HashSet();public SetgetAgreeUsers() {returnagreeUsers;
}public void setAgreeUsers(SetagreeUsers) {this.agreeUsers =agreeUsers;
}public SetgetDisagreeUsers() {returndisagreeUsers;
}public void setDisagreeUsers(SetdisagreeUsers) {this.disagreeUsers =disagreeUsers;
}//JPA标准
protectedComment()
{
}public Comment(Long id, String commentTime, String commentContent, Setreplys, User commentUser, User user,
Set agreeUsers, SetdisagreeUsers) {super();this.id =id;this.commentTime =commentTime;this.commentContent =commentContent;this.replys =replys;this.commentUser =commentUser;this.user =user;this.agreeUsers =agreeUsers;this.disagreeUsers =disagreeUsers;
}publicLong getId() {returnid;
}public voidsetId(Long id) {this.id =id;
}publicString getCommentTime() {returncommentTime;
}public voidsetCommentTime(String commentTime) {this.commentTime =commentTime;
}publicString getCommentContent() {returncommentContent;
}public voidsetCommentContent(String commentContent) {this.commentContent =commentContent;
}public SetgetReplys() {returnreplys;
}public void setReplys(Setreplys) {this.replys =replys;
}publicUser getCommentUser() {returncommentUser;
}public voidsetCommentUser(User commentUser) {this.commentUser =commentUser;
}publicUser getUser() {returnuser;
}public voidsetUser(User user) {this.user =user;
}
}
Comment.java
packagecom.Gary.betobe.domain;importjava.util.HashSet;importjava.util.Set;importjavax.persistence.Entity;importjavax.persistence.GeneratedValue;importjavax.persistence.GenerationType;importjavax.persistence.Id;importjavax.persistence.JoinColumn;importjavax.persistence.JoinTable;importjavax.persistence.ManyToMany;importjavax.persistence.ManyToOne;//回复,回复的是谁(评论还是回复)
@Entitypublic classReply {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)privateLong id;//回复内容
privateString replyContent;//回复时间
privateString replyTime;//谁发的回复
@ManyToOne(targetEntity = User.class)
@JoinColumn(name= "user_id")privateUser user;//这条回复是回复哪一个评论的
@ManyToOne(targetEntity = Comment.class)
@JoinColumn(name="comment_id")privateComment comment;//这条回复是回复哪一个回复的
@ManyToOne(targetEntity = Reply.class)
@JoinColumn(name="reply_id")privateReply reply;//有哪些回复是回复本条回复的
@ManyToMany
@JoinTable(
name="reply_reply",
joinColumns=@JoinColumn(name="reply_id"),
inverseJoinColumns=@JoinColumn(name="reply_reply_id")
)private Set replys = new HashSet();
@ManyToMany(mappedBy= "disagreeReplys")private Set disagreeUsers = new HashSet();
@ManyToMany(mappedBy= "agreeReplys")private Set agreeUsers = new HashSet();//JPA标准
protectedReply()
{
}publicReply(Long id, String replyContent, String replyTime, User user, Comment comment, Reply reply,
Set replys, Set disagreeUsers, SetagreeUsers) {super();this.id =id;this.replyContent =replyContent;this.replyTime =replyTime;this.user =user;this.comment =comment;this.reply =reply;this.replys =replys;this.disagreeUsers =disagreeUsers;this.agreeUsers =agreeUsers;
}publicString getReplyContent() {returnreplyContent;
}public voidsetReplyContent(String replyContent) {this.replyContent =replyContent;
}publicString getReplyTime() {returnreplyTime;
}public voidsetReplyTime(String replyTime) {this.replyTime =replyTime;
}publicLong getId() {returnid;
}public voidsetId(Long id) {this.id =id;
}publicUser getUser() {returnuser;
}public voidsetUser(User user) {this.user =user;
}publicComment getComment() {returncomment;
}public voidsetComment(Comment comment) {this.comment =comment;
}publicReply getReply() {returnreply;
}public voidsetReply(Reply reply) {this.reply =reply;
}public SetgetReplys() {returnreplys;
}public void setReplys(Setreplys) {this.replys =replys;
}public SetgetDisagreeUsers() {returndisagreeUsers;
}public void setDisagreeUsers(SetdisagreeUsers) {this.disagreeUsers =disagreeUsers;
}public SetgetAgreeUsers() {returnagreeUsers;
}public void setAgreeUsers(SetagreeUsers) {this.agreeUsers =agreeUsers;
}
}
Reply.java
此时数据库中出现了很多表
agree_user_comment表:用户同意了哪些评论
agree_user_reply表:用户同意了哪些回复
disagree_user_comment表:用户不同意哪些评论
disagree_user_reply表:用户不同意哪些回复
comment表:评论表
reply表:回复表
persistent_logins:记住我功能表
reply_reply表:回复回复表
用户评论
profile-comments中设置回复表单样式
Reply:Gary
Said: