Java 谷歌搜索过,但在hibernate中找不到带注释的双向映射解决方案
我已经在谷歌上搜索了这个解决方案,但是我找不到带有注释的解决方案。让我解释一下这个问题。 我有3个实体(个人资料、消息、评论)。我已经在三个实体之间进行了映射Java 谷歌搜索过,但在hibernate中找不到带注释的双向映射解决方案,java,hibernate,Java,Hibernate,我已经在谷歌上搜索了这个解决方案,但是我找不到带有注释的解决方案。让我解释一下这个问题。 我有3个实体(个人资料、消息、评论)。我已经在三个实体之间进行了映射 One Profile can post N Messages One Message can post by Profile and has N comments Many Comment can post by One Profile and identified by One Message @Entity @Table(name
One Profile can post N Messages
One Message can post by Profile and has N comments
Many Comment can post by One Profile and identified by One Message
@Entity
@Table(name = "profile")
public class ProfileEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "profile_id")
private long profileId;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "user_name")
private String userName;
@Column(name = "password")
private String password;
@OneToMany(cascade = CascadeType.REMOVE, orphanRemoval = true, mappedBy = "profiles")
private List<CommentEntity> comments;
@OneToMany(cascade = CascadeType.REMOVE, orphanRemoval = true, mappedBy = "profiles")
private List<MessageEntity> messages;
public long getProfileId() {
return profileId;
}
public void setProfileId(long profileId) {
this.profileId = profileId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public List<CommentEntity> getComments() {
return comments;
}
public void setComments(List<CommentEntity> comments) {
this.comments = comments;
}
public List<MessageEntity> getMessages() {
return messages;
}
public void setMessages(List<MessageEntity> messages) {
this.messages = messages;
}
}
@Entity
@Table(name = "message")
public class MessageEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "message_id")
private long messageId;
@Column(name = "message")
private String message;
@Column(name = "created_on")
private String ceatedOn;
@ManyToOne
@JoinColumn(name = "profile_id")
private ProfileEntity profiles;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "messages")
private Set<CommentEntity> comments;
@Transient
private List<Link> linkList = new ArrayList<Link>();
public long getMessageId() {
return messageId;
}
public void setMessageId(long messageId) {
this.messageId = messageId;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getCeatedOn() {
return ceatedOn;
}
public void setCeatedOn(String ceatedOn) {
this.ceatedOn = ceatedOn;
}
public ProfileEntity getProfiles() {
return profiles;
}
public void setProfiles(ProfileEntity profiles) {
this.profiles = profiles;
}
public Set<CommentEntity> getComments() {
return comments;
}
public void setComments(Set<CommentEntity> comments) {
this.comments = comments;
}
public List<Link> getLinkList() {
return linkList;
}
public void setLinkList(List<Link> linkList) {
this.linkList = linkList;
}
public void addLink(String link, String rel) {
Link lnk = new Link();
lnk.setLink(link);
lnk.setRel(rel);
linkList.add(lnk);
}
}
@Entity
@Table(name = "comment")
public class CommentEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "comment_id")
private long commentId;
@Column(name = "comment")
private String comment;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "profile_id")
private ProfileEntity profiles;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "message_id")
private MessageEntity messages;
public long getCommentId() {
return commentId;
}
public void setCommentId(long commentId) {
this.commentId = commentId;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public ProfileEntity getProfiles() {
return profiles;
}
public void setProfiles(ProfileEntity profiles) {
this.profiles = profiles;
}
public MessageEntity getMessages() {
return messages;
}
public void setMessages(MessageEntity messages) {
this.messages = messages;
}
}
一个配置文件可以发布N条消息
一条消息可以通过配置文件发布,并且有N条注释
许多评论可以通过一个配置文件发布,并通过一条消息识别
@实体
@表(name=“profile”)
公共类概要文件实体{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
@列(name=“profile\u id”)
私有长profileId;
@列(name=“first_name”)
私有字符串名;
@列(name=“last_name”)
私有字符串lastName;
@列(name=“user\u name”)
私有字符串用户名;
@列(name=“password”)
私有字符串密码;
@OneToMany(cascade=CascadeType.REMOVE,orphanremove=true,mappedBy=“profiles”)
私人名单评论;
@OneToMany(cascade=CascadeType.REMOVE,orphanremove=true,mappedBy=“profiles”)
私人列表消息;
公共长getProfileId(){
返回profileId;
}
public void setProfileId(长profileId){
this.profileId=profileId;
}
公共字符串getFirstName(){
返回名字;
}
public void setFirstName(字符串firstName){
this.firstName=firstName;
}
公共字符串getLastName(){
返回姓氏;
}
public void setLastName(字符串lastName){
this.lastName=lastName;
}
公共字符串getUserName(){
返回用户名;
}
public void setUserName(字符串用户名){
this.userName=用户名;
}
公共字符串getPassword(){
返回密码;
}
public void setPassword(字符串密码){
this.password=密码;
}
公共列表getComments(){
返回评论;
}
公共注释(列出注释){
this.comments=注释;
}
公共列表getMessages(){
返回消息;
}
公共消息(列出消息){
this.messages=消息;
}
}
@实体
@表(name=“message”)
公共类消息实体{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
@列(name=“message\u id”)
私有长消息ID;
@列(name=“message”)
私有字符串消息;
@列(name=“已创建”)
私人字符串;
@许多酮
@JoinColumn(name=“profile\u id”)
私人档案和实体档案;
@OneToMany(fetch=FetchType.EAGER,cascade=CascadeType.ALL,orphanRemoving=true,mappedBy=“messages”)
私人评论;
@短暂的
private List linkList=new ArrayList();
公共长getMessageId(){
返回messageId;
}
public void setMessageId(长messageId){
this.messageId=messageId;
}
公共字符串getMessage(){
返回消息;
}
公共无效设置消息(字符串消息){
this.message=消息;
}
公共字符串getCeatedOn(){
返回赛顿;
}
公共void setCeatedOn(字符串ceatedOn){
this.ceatedOn=ceatedOn;
}
公共配置文件实体getProfiles(){
返回配置文件;
}
公共void集合概要文件(概要文件实体概要文件){
this.profiles=profiles;
}
公共集getComments(){
返回评论;
}
公共void setComments(Set comments){
this.comments=注释;
}
公共列表getLinkList(){
返回链接列表;
}
公共无效集合链接列表(列表链接列表){
this.linkList=linkList;
}
公共void addLink(字符串链接,字符串rel){
链接lnk=新链接();
lnk.setLink(链接);
lnk.setRel(rel);
linkList.add(lnk);
}
}
@实体
@表(name=“comment”)
公共类实体{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
@列(name=“comment\u id”)
私人长评论ID;
@列(name=“comment”)
私有字符串注释;
@manytone(fetch=FetchType.EAGER)
@JoinColumn(name=“profile\u id”)
私人档案和实体档案;
@manytone(fetch=FetchType.EAGER)
@JoinColumn(name=“message\u id”)
私有消息实体消息;
公共长getCommentId(){
返回commentId;
}
public void setCommentId(长commentId){
this.commentId=commentId;
}
公共字符串getComment(){
回复评论;
}
公共void setComment(字符串注释){
this.comment=注释;
}
公共配置文件实体getProfiles(){
返回配置文件;
}
公共void集合概要文件(概要文件实体概要文件){
this.profiles=profiles;
}
public MessageEntity getMessages(){
返回消息;
}
公共无效设置消息(消息实体消息){
this.messages=消息;
}
}
问题:
我可以选择或删除配置文件,但当我尝试选择消息时,它不会在ReST客户端上呈现。我怀疑这会导致循环映射。在这方面,有人能帮我吗?试着添加一个急切的获取。如果您有:
@OneToMany
替换为添加
@OneToMany (fetch=FetchType.EAGER)
当您调用对象时,它将自动填充您的列表
如果您想了解有关它的详细信息,请查看此链接:这可能是一种循环映射状态,原因可能正是:因为您在
commentity
和MessageEntity
的关系中声明了FetchType.EAGER
如果您为所有对象声明FetchType.LAZY
,则可以消除循环映射状态
顺便说一句,stacktrace和/或报告的错误消息会更有帮助。我也尝试过使用Earge fetch。没用。我没有收到任何错误,但根据我的分析,它将进入循环映射状态。删除@manytone注释上的所有急切获取