package com.prms.urms.entity; import java.util.ArrayList; import java.util.Date; import java.util.HashSet; import java.util.List; 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.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; import javax.persistence.Transient; import org.hibernate.annotations.GenericGenerator; import org.hibernate.annotations.NotFound; import org.hibernate.annotations.NotFoundAction; import org.hibernate.annotations.Parameter; /** * Appfunc entity. @author MyEclipse Persistence Tools */ @Entity @Table(name = "appfunc", schema = "moco_ajb") public class Appfunc implements java.io.Serializable, Comparable<Appfunc> { /** * 請看上面多對一的地方↑↑↑ * 註解配置解決方法: *使用hibernate 註解配置實體類的關聯關係,在many-to-one,one-to-one關聯中,一邊引用自另外一邊的屬性,若是屬性值爲某某的數據在數據庫不存在了,hibernate默認會拋出異常。解決此問題,加上以下註解就能夠了: *@NotFound(action=NotFoundAction.IGNORE),意思是找不到引用的外鍵數據時忽略,NotFound默認是exception */ private static final long serialVersionUID = 1L; // Fields private String funcid; private String appid; private String funcno; private String funcname; private String sfqxrz; private String funcdesc; private Integer orderno; private String sfmj; private String funcurl; private String czlxbms; private String qzgnid; private String cjrid; private String cjr; private Date cjrq; private String zjxgrid; private String zhxgr; private Date zjxgrq; private String icons; private Appfunc appfunc; private Set<Appfunc> childAfs = new HashSet<Appfunc>(); private Integer orderNum; private String sysjgnid; // Constructors // 是否權限認證-中文顯示 private String sfqxrzZh; // 是否末級功能-中文顯示 private String sfmjZh; // 上級功能-中文 private String sjgnZh; // 上級功能ID private String sjgnid; // 所屬系統中文 private String appZh; // 全部子級容器 private List<Appfunc> children = new ArrayList<Appfunc>(); /** default constructor */ public Appfunc() { } /** minimal constructor */ public Appfunc(String funcid) { this.funcid = funcid; } /** full constructor */ public Appfunc(String funcid, String appid, String funcno, String funcname, String sfqxrz, String funcdesc, Integer orderno, String sfmj, String funcurl, String czlxbms, String qzgnid, String cjrid, String cjr, Date cjrq, String zjxgrid, String zhxgr, Date zjxgrq) { this.funcid = funcid; this.appid = appid; this.funcno = funcno; this.funcname = funcname; this.sfqxrz = sfqxrz; this.funcdesc = funcdesc; this.orderno = orderno; this.sfmj = sfmj; this.funcurl = funcurl; this.czlxbms = czlxbms; this.qzgnid = qzgnid; this.cjrid = cjrid; this.cjr = cjr; this.cjrq = cjrq; this.zjxgrid = zjxgrid; this.zhxgr = zhxgr; this.zjxgrq = zjxgrq; } // Property accessors @Id @GeneratedValue(generator = "paymentableGenerator") @GenericGenerator(name = "paymentableGenerator", strategy = "com.prms.urms.common.IdGenerator", parameters = { @Parameter(name = "tableName", value = "func") }) @Column(name = "FUNCID", unique = true, nullable = false, length = 20) public String getFuncid() { return this.funcid; } public void setFuncid(String funcid) { this.funcid = funcid; } @Column(name = "APPID", length = 20) public String getAppid() { return this.appid; } public void setAppid(String appid) { this.appid = appid; } @Column(name = "FUNCNO", length = 60) public String getFuncno() { return this.funcno; } public void setFuncno(String funcno) { this.funcno = funcno; } @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "REPORTCONTENT") @NotFound(action=NotFoundAction.IGNORE) //在此添加一個註解就OK了。 public Appfunc getAppfunc() { return appfunc; } public void setAppfunc(Appfunc appfunc) { this.appfunc = appfunc; } @OneToMany(cascade = CascadeType.MERGE, fetch = FetchType.EAGER, mappedBy = "appfunc") public Set<Appfunc> getChildAfs() { return childAfs; } public void setChildAfs(Set<Appfunc> childAfs) { this.childAfs = childAfs; } @Column(name = "FUNCNAME", length = 100) public String getFuncname() { return this.funcname; } public void setFuncname(String funcname) { this.funcname = funcname; } @Column(name = "SFQXRZ", length = 1) public String getSfqxrz() { return this.sfqxrz; } public void setSfqxrz(String sfqxrz) { this.sfqxrz = sfqxrz; } @Column(name = "FUNCDESC", length = 200) public String getFuncdesc() { return this.funcdesc; } public void setFuncdesc(String funcdesc) { this.funcdesc = funcdesc; } @Column(name = "ORDERNO", precision = 22, scale = 0) public Integer getOrderno() { return this.orderno; } public void setOrderno(Integer orderno) { this.orderno = orderno; } @Column(name = "SFMJ", length = 1) public String getSfmj() { return this.sfmj; } public void setSfmj(String sfmj) { this.sfmj = sfmj; } @Column(name = "FUNCURL", length = 100) public String getFuncurl() { return this.funcurl; } public void setFuncurl(String funcurl) { this.funcurl = funcurl; } @Column(name = "CZLXBMS", length = 2) public String getCzlxbms() { return this.czlxbms; } public void setCzlxbms(String czlxbms) { this.czlxbms = czlxbms; } @Column(name = "QZGNID", length = 20) public String getQzgnid() { return this.qzgnid; } public void setQzgnid(String qzgnid) { this.qzgnid = qzgnid; } @Column(name = "CJRID", length = 20) public String getCjrid() { return this.cjrid; } public void setCjrid(String cjrid) { this.cjrid = cjrid; } @Column(name = "CJR", length = 30) public String getCjr() { return this.cjr; } public void setCjr(String cjr) { this.cjr = cjr; } @Temporal(TemporalType.DATE) @Column(name = "CJRQ", length = 7) public Date getCjrq() { return this.cjrq; } public void setCjrq(Date cjrq) { this.cjrq = cjrq; } @Column(name = "ZJXGRID", length = 20) public String getZjxgrid() { return this.zjxgrid; } public void setZjxgrid(String zjxgrid) { this.zjxgrid = zjxgrid; } @Column(name = "ZHXGR", length = 30) public String getZhxgr() { return this.zhxgr; } public void setZhxgr(String zhxgr) { this.zhxgr = zhxgr; } @Temporal(TemporalType.DATE) @Column(name = "ZJXGRQ", length = 7) public Date getZjxgrq() { return this.zjxgrq; } public void setZjxgrq(Date zjxgrq) { this.zjxgrq = zjxgrq; } @Column(name = "ICONS", length = 100) public String getIcons() { return icons; } public void setIcons(String icons) { this.icons = icons; } @Transient public String getSfqxrzZh() { return sfqxrzZh; } public void setSfqxrzZh(String sfqxrzZh) { this.sfqxrzZh = sfqxrzZh; } @Transient public String getSfmjZh() { return sfmjZh; } public void setSfmjZh(String sfmjZh) { this.sfmjZh = sfmjZh; } @Transient public String getSjgnZh() { return sjgnZh; } public void setSjgnZh(String sjgnZh) { this.sjgnZh = sjgnZh; } @Transient public String getSjgnid() { return sjgnid; } public void setSjgnid(String sjgnid) { this.sjgnid = sjgnid; } @Transient public String getAppZh() { return appZh; } public void setAppZh(String appZh) { this.appZh = appZh; } @Column(name = "ORDERNUM", precision = 22, scale = 0) public Integer getOrderNum() { return orderNum; } public void setOrderNum(Integer orderNum) { this.orderNum = orderNum; } @Column(name = "SYSJGNID", length = 120) public String getSysjgnid() { return sysjgnid; } public void setSysjgnid(String sysjgnid) { this.sysjgnid = sysjgnid; } public int compareTo(Appfunc arg0) { return this.getOrderNum().compareTo(arg0.getOrderNum()); } @Transient public List<Appfunc> getChildren() { return children; } public void setChildren(List<Appfunc> children) { this.children = children; } }
產生此問題的緣由在此記錄下來:java
有兩張表,table1和table2. 產生此問題的緣由就是table1裏作了關聯<one-to-one>或者<many-to-one unique="true">(特殊的多對一映射,實際就是一對一)來關聯table2.當hibernate查找的時候,table2裏的數據沒 有與table1相匹配的,這樣就會報No row with the given identifier exists這個錯.(一句話,就是數據的如今hibernate配置能夠基於xml配置文件和註解方式,這兩種方式都能發生這個異常數據庫
如下是XML 配置形式的加法:
app
<many-to-one class="com.art.model.user.UserInfo" fetch="join" name="userInfo" > <column name="userId" unique="true"/> </many-to-one>
<many-to-one class="com.art.model.user.UserInfo" fetch="join" name="userInfo" not-found="ignore"> <column name="userId" unique="true"/> </many-to-one>