Hibernate异常No row with the given identifier exists 解决方法

产生此问题的原因:
有两张表,删除主键数据,导致外键数据找不到引用了,这样就会报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>

红色是修改的部分。意思是当对应的数据不存在时 忽略掉,用null值填充。该属性默认值:exception 。

注解配置解决方法:
使用hibernate 注解配置实体类的关联关系,在many-to-one,one-to-one关联中,一边引用自另一边的属性,如果属性值为某某的数据在数据库不存在了,hibernate默认会抛出异常。解决此问题,加上如下注解就可以了:
[color=red]@NotFound(action=NotFoundAction.IGNORE),意思是找不到引用的外键数据时忽略,NotFound默认是exception[/color]
如果是使用的freemarker的话
使用

<#if rankchange.dictNowRanks?exists>${rankchange.dictNowRanks.vcName}</#if>

由于dictNowRanks找不到会抛出exception,所以?exists都是true,都是会进入的。使用@NotFound(action=NotFoundAction.IGNORE之后,找不到则忽略,相当于返回空
下面贴出hibernate 注解的实例代码
@Entity
@Table(name = "PM_BSC_RANK_CHANGE")
@SequenceGenerator(name="sg", sequenceName="SEQ_PM_BSC_RANK_CHANGE", allocationSize=1)
@org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true)
public class PmBscRankChange extends BizEntity{

// Fields

private Long pid;
private PmBscStaffInfo pmBscStaffInfo;
private Date vcNowDate;
private Date vcChgDate;
private String vcRemark;
private Long vcIfedit;
private Long vcType;
private Date vcEndDate;

//private Date vcCreateDate;

private PmBscDict dictNowRanks;//调动前职级
private PmBscDict dictChgRanks;//调动后职级
// Constructors

/** default constructor */
public PmBscRankChange() {
}

/** minimal constructor */
public PmBscRankChange(Long pid) {
this.pid = pid;
}

// Property accessors
@Id
@Column(name = "PID", unique = true, nullable = false, precision = 20, scale = 0)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="sg")
public Long getPid() {
return this.pid;
}

public void setPid(Long pid) {
this.pid = pid;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "VC_STAFF_ID")
public PmBscStaffInfo getPmBscStaffInfo() {
return this.pmBscStaffInfo;
}

public void setPmBscStaffInfo(PmBscStaffInfo pmBscStaffInfo) {
this.pmBscStaffInfo = pmBscStaffInfo;
}

@Column(name = "VC_NOW_DATE")
public Date getVcNowDate() {
return this.vcNowDate;
}

public void setVcNowDate(Date vcNowDate) {
this.vcNowDate = vcNowDate;
}

@Column(name = "VC_CHG_DATE")
public Date getVcChgDate() {
return this.vcChgDate;
}

public void setVcChgDate(Date vcChgDate) {
this.vcChgDate = vcChgDate;
}

@Column(name = "VC_REMARK", length = 1000)
public String getVcRemark() {
return this.vcRemark;
}

public void setVcRemark(String vcRemark) {
this.vcRemark = vcRemark;
}


@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "VC_NOW_RANK", columnDefinition="NUMBER(20)", nullable=false)
@NotFound(action=NotFoundAction.IGNORE)
public PmBscDict getDictNowRanks() {
return dictNowRanks;
}

public void setDictNowRanks(PmBscDict dictNowRanks) {
this.dictNowRanks = dictNowRanks;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "VC_CHG_RANK", columnDefinition="NUMBER(20)", nullable=false)
@NotFound(action=NotFoundAction.IGNORE)
public PmBscDict getDictChgRanks() {
return dictChgRanks;
}

public void setDictChgRanks(PmBscDict dictChgRanks) {
this.dictChgRanks = dictChgRanks;
}

@Column(name = "VC_IFEDIT")
public Long getVcIfedit() {
return vcIfedit;
}

public void setVcIfedit(Long vcIfedit) {
this.vcIfedit = vcIfedit;
}

@Column(name = "VC_TYPE")
public Long getVcType() {
return vcType;
}

public void setVcType(Long vcType) {
this.vcType = vcType;
}

@Column(name = "VC_END_DATE")
public Date getVcEndDate() {
return vcEndDate;
}

public void setVcEndDate(Date vcEndDate) {
this.vcEndDate = vcEndDate;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值