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; } }
产生此问题的原因在此记录下来:
有两张表,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 配置形式的加法:
<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>