所以当它具有相同类型的父级时,创建新的MenuPortlet时遇到问题。父级存在于数据库中,而当我设置了parParent(mp)时它就在其中。
package com.kpsol.version6.model;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
@Entity
@Table(name = "MENU_PORTLET")
@NamedQuery(name = "menuPortlet.findAll", query = "SELECT t FROM MenuPortlet t")
public class MenuPortlet extends Portlet implements Serializable {
private static final long serialVersionUID = 1L;
@TableGenerator(name = "MenuPortlet", table = "NRD_ID", pkColumnName = "TABLENAME", valueColumnName = "NEXTID", pkColumnValue = "MENU_PORTLET", allocationSize = 1)
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "MenuPortlet")
@Column(name = "MENUPORTLETID")
private Long id;
@ManyToOne
@JoinColumn(name = "PARENTID")
MenuPortlet parent;
@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade = { CascadeType.ALL })
@OrderBy("PORTLETORDER")
Set<MenuPortlet> children = new HashSet<MenuPortlet>();
@JoinColumn(name = "USERID")
private Long userId;
@Column(name = "ISUSER")
private Boolean isUser;
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
public Long getUserId() {
return this.userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public MenuPortlet getParent() {
return this.parent;
}
public void setParent(MenuPortlet parent) {
this.parent = parent;
}
public Set<MenuPortlet> getChildren() {
return this.children;
}
public void setChildren(Set<MenuPortlet> children) {
this.children = children;
}
public Boolean getIsUser() {
return this.isUser;
}
public void setIsUser(Boolean isUser) {
this.isUser = isUser;
}
}
这就是我与父母建立关系的方式。我为父母和孩子都尝试了懒惰和渴望的负载,但似乎都没有作用。
portlet.setParent(mp);
portlet = this.portletservice.create(portlet);
mp是正确的MenuPortlet,在那里没有问题。
this.em.persist(portlet);
老实说,没有什么可显示的了。令人讨厌的是,我在其他几个地方也做过类似的事情,没有问题,而且我实际上将相同的代码复制到最后一部分,但仍然无法正常工作。我想念什么?