嗨,我是 JPA 的新手,当我想保留对象时,我收到以下错误:
java.lang.IllegalArgumentException: Object: null is not a known entity type.
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:437)
at database.TarpineManager.persist(TarpineManager.java:31)
at gijos.InsertToDB.insertLink(InsertToDB.java:34)
at gijos.VilpraParser.run(VilpraParser.java:77)
有我的第一个对象类,我想与其他 XLinksMedia 类建立关系,我认为问题出在
@OneToMany(mappedBy = "xParserLink", targetEntity = XLinksMedia.class, fetch=FetchType.EAGER, cascade = CascadeType.ALL)
private List<XLinksMedia> fotos;
但我不知道出了什么问题:
package database.entity;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
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.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.xml.bind.annotation.XmlRootElement;
@Entity
@Table(name = "x_parser_links")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "XParserLinks.findAll", query = "SELECT x FROM XParserLinks x"),
@NamedQuery(name = "XParserLinks.findByLinkId", query = "SELECT x FROM XParserLinks x WHERE x.linkId = :linkId"),
@NamedQuery(name = "XParserLinks.findByPageId", query = "SELECT x FROM XParserLinks x WHERE x.pageId = :pageId"),
@NamedQuery(name = "XParserLinks.findByLink", query = "SELECT x FROM XParserLinks x WHERE x.link = :link"),
@NamedQuery(name = "XParserLinks.findByLevel", query = "SELECT x FROM XParserLinks x WHERE x.level = :level"),
@NamedQuery(name = "XParserLinks.findByLinkType", query = "SELECT x FROM XParserLinks x WHERE x.linkType = :linkType"),
@NamedQuery(name = "XParserLinks.findByCreateDate", query = "SELECT x FROM XParserLinks x WHERE x.createDate = :createDate"),
@NamedQuery(name = "XParserLinks.findByDelDate", query = "SELECT x FROM XParserLinks x WHERE x.delDate = :delDate")})
public class XParserLinks implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "link_id")
private Integer linkId;
@Column(name = "page_id")
private Integer pageId;
@Column(name = "link")
private String link;
@Column(name = "level")
private Integer level;
@Column(name = "link_type")
private Short linkType;
@Column(name = "create_date")
@Temporal(TemporalType.TIMESTAMP)
private Date createDate;
@Column(name = "del_date")
@Temporal(TemporalType.TIMESTAMP)
private Date delDate;
@JoinColumn(name = "tev_link_id")
@OneToOne
private XParserLinks tevas;
@OneToMany(mappedBy = "xParserLink", targetEntity = XLinksMedia.class, fetch=FetchType.EAGER, cascade = CascadeType.ALL)
private List<XLinksMedia> fotos;
public XParserLinks() {
}
public XParserLinks(Integer linkId) {
this.linkId = linkId;
}
public Integer getLinkId() {
return linkId;
}
public void setLinkId(Integer linkId) {
this.linkId = linkId;
}
public Integer getPageId() {
return pageId;
}
public void setPageId(Integer pageId) {
this.pageId = pageId;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public Integer getLevel() {
return level;
}
public void setLevel(Integer level) {
this.level = level;
}
public Short getLinkType() {
return linkType;
}
public void setLinkType(Short linkType) {
this.linkType = linkType;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public Date getDelDate() {
return delDate;
}
public void setDelDate(Date delDate) {
this.delDate = delDate;
}
public XParserLinks getTevas() {
return tevas;
}
public void setTevas(XParserLinks tevas) {
this.tevas = tevas;
}
public List<XLinksMedia> getFotos() {
return fotos;
}
public void setFotos(List<XLinksMedia> fotos) {
this.fotos = fotos;
}
@Override
public int hashCode() {
int hash = 0;
hash += (linkId != null ? linkId.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof XParserLinks)) {
return false;
}
XParserLinks other = (XParserLinks) object;
if ((this.linkId == null && other.linkId != null) || (this.linkId != null && !this.linkId.equals(other.linkId))) {
return false;
}
return true;
}
@Override
public String toString() {
return "database.entity.XParserLinks[ linkId=" + linkId + " ]";
}
}
fotos对象的第二类
package database.entity;
import java.io.Serializable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;
@Entity
@Table(name = "x_links_media")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "XLinksMedia.findAll", query = "SELECT x FROM XLinksMedia x"),
@NamedQuery(name = "XLinksMedia.findByLinkId", query = "SELECT x FROM XLinksMedia x WHERE x.xLinksMediaPK.linkId = :linkId"),
@NamedQuery(name = "XLinksMedia.findByImage", query = "SELECT x FROM XLinksMedia x WHERE x.xLinksMediaPK.image = :image")})
public class XLinksMedia implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
protected XLinksMediaPK xLinksMediaPK;
@ManyToOne
@JoinColumn(name = "link_id")
private XParserLinks xParserLink;
public XLinksMedia() {
}
public XLinksMedia(XLinksMediaPK xLinksMediaPK) {
this.xLinksMediaPK = xLinksMediaPK;
}
public XLinksMedia(int linkId, String image) {
this.xLinksMediaPK = new XLinksMediaPK(linkId, image);
}
public XLinksMediaPK getXLinksMediaPK() {
return xLinksMediaPK;
}
public void setXLinksMediaPK(XLinksMediaPK xLinksMediaPK) {
this.xLinksMediaPK = xLinksMediaPK;
}
public XParserLinks getxParserLink() {
return xParserLink;
}
public void setxParserLink(XParserLinks xParserLink) {
this.xParserLink = xParserLink;
}
@Override
public int hashCode() {
int hash = 0;
hash += (xLinksMediaPK != null ? xLinksMediaPK.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof XLinksMedia)) {
return false;
}
XLinksMedia other = (XLinksMedia) object;
if ((this.xLinksMediaPK == null && other.xLinksMediaPK != null) || (this.xLinksMediaPK != null && !this.xLinksMediaPK.equals(other.xLinksMediaPK))) {
return false;
}
return true;
}
@Override
public String toString() {
return "database.entity.XLinksMedia[ xLinksMediaPK=" + xLinksMediaPK + " ]";
}
}
持久化对象是这个方法:
obj= new XParserLinks();
obj.setCreateDate(new Date());
obj.setLevel(1);
obj.setPageId(2);
obj.setLink(link);
obj.setLinkType((short) 2);
TarpineManager.startTransaction();
TarpineManager.persist(obj);
System.out.println("NAUJAS LINKAS");
TarpineManager.commitTransaction();