Hibernate 一對多配置 級聯操作(級聯失敗問題分析解決)

weixin_33843409發表於2017-03-16

一方:

package com.xdfstar.domain;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
* Title. <br>
* Description.
* <p>
* Copyright: Copyright (c) 2016年7月11日 下午4:21:49
* <p>
* 2016-3-21 Company: 北京新東方學校
* <p>
* Author: jinxudong@xdf.cn
* <p>
* Version: 1.0
* <p>
*/
public class AwardModel implements Serializable {

/** */
private static final long serialVersionUID = 1L;

private String uid;

private String image;

private String award_name;

private int number;

private int integral;

private int state;

private Set<DrawRecordModel> cjrecordsSet = new HashSet<DrawRecordModel>();

/** 提供人 */
private String provider;

/** 提供機構 */
private String organization;

/** 價值描述 只作為描述 不做運算 */
private String price;

/** 禮品入庫時間 */
private Date create_time;

/** 禮品有效期 */
private Date end_time;

/** 活動id */
private ActivityModel activityModel;

private String remark;

/** 0:積分功能 1:抽獎功能 */
private int type;


public ActivityModel getActivityModel() {
return activityModel;
}


public void setActivityModel(ActivityModel activityModel) {
this.activityModel = activityModel;
}

public int getType() {
return type;
}

public void setType(int type) {
this.type = type;
}

public String getProvider() {
return provider;
}

public void setProvider(String provider) {
this.provider = provider;
}

public String getOrganization() {
return organization;
}

public void setOrganization(String organization) {
this.organization = organization;
}

public String getPrice() {
return price;
}

public void setPrice(String price) {
this.price = price;
}

public Date getCreate_time() {
return create_time;
}

public void setCreate_time(Date create_time) {
this.create_time = create_time;
}

public Date getEnd_time() {
return end_time;
}

public void setEnd_time(Date end_time) {
this.end_time = end_time;
}


public String getRemark() {
return remark;
}

public void setRemark(String remark) {
this.remark = remark;
}

public String getUid() {
return uid;
}

public Set<DrawRecordModel> getCjrecordsSet() {
return cjrecordsSet;
}

public void setCjrecordsSet(Set<DrawRecordModel> cjrecordsSet) {
this.cjrecordsSet = cjrecordsSet;
}

public void setUid(String uid) {
this.uid = uid;
}

public String getImage() {
return image;
}

public void setImage(String image) {
this.image = image;
}

public String getAward_name() {
return award_name;
}

public void setAward_name(String award_name) {
this.award_name = award_name;
}

public int getNumber() {
return number;
}

public void setNumber(int number) {
this.number = number;
}

public int getIntegral() {
return integral;
}

public void setIntegral(int integral) {
this.integral = integral;
}

public int getState() {
return state;
}

public void setState(int state) {
this.state = state;
}

}

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.xdfstar.domain.AwardModel" table="t_award">
<id name="uid" column="uid" type="string">
<generator class="assigned" />
</id>
<property name="image" column="image" type="string" />
<property name="award_name" column="award_name" type="string" />
<property name="number" column="number" type="integer" />
<property name="integral" column="integral" type="integer" />
<property name="state" column="state" type="integer" />
<set name="cjrecordsSet" cascade="save-update,delete" inverse="true">
<key column="aid"></key>
<one-to-many class="com.xdfstar.domain.DrawRecordModel" />
</set>
<property name="provider" column="provider" type="string" />
<property name="organization" column="organization" type="string" />
<property name="price" column="price" type="string" />
<property name="create_time" column="create_time" type="date" />
<property name="end_time" column="end_time" type="date" />
<many-to-one name="activityModel" class="com.xdfstar.domain.ActivityModel"
column="act_id"></many-to-one>
<property name="remark" column="remark" type="string" />
<property name="type" column="type" type="integer" />
</class>
</hibernate-mapping>

 

多方:

public class DrawRecordModel {

private String uid;

// 抽獎人id
private String eid;

private String ename;


public String getEname() {
return ename;
}


public void setEname(String ename) {
this.ename = ename;
}

/** 抽獎時間 */
private Date cj_time;

private AwardModel awardModel;

public AwardModel getAwardModel() {
return awardModel;
}

public void setAwardModel(AwardModel awardModel) {
this.awardModel = awardModel;
}

public String getUid() {
return uid;
}

public void setUid(String uid) {
this.uid = uid;
}

 


public String getEid() {
return eid;
}


public void setEid(String eid) {
this.eid = eid;
}

public Date getCj_time() {
return cj_time;
}

public void setCj_time(Date cj_time) {
this.cj_time = cj_time;
}

}

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.xdfstar.domain.DrawRecordModel" table="t_draw_record">
<id name="uid" column="uid" type="string">
<generator class="assigned" />
</id>
<property name="eid" column="eid"></property>
<property name="cj_time" column="cj_time" type="date" />
<many-to-one name="awardModel" class="com.xdfstar.domain.AwardModel"
column="aid"></many-to-one>
</class>
</hibernate-mapping>

配置正確 刪除修改級聯不起作用分析:

刪除直接用delete(object)刪除 

該object包含了所刪資料的id,但是其他欄位為空,外來鍵關聯不上,當前資料記錄正常刪除,級聯失效

正確的邏輯應該是查出這個object  他有完整的資料 再進行delete ,update同樣如此,問題完美解決。

正確寫法:

@Override
public int deleteAwardModel(AwardModel awardModel) {
// TODO Auto-generated method stub
awardModel=(AwardModel) hibernateTemplate.getSessionFactory().getCurrentSession().get(AwardModel.class, awardModel.getUid());
hibernateTemplate.delete(awardModel);
return 1;
}

 

錯誤寫法:

@Override
public int deleteAwardModel(AwardModel awardModel) {
// TODO Auto-generated method stub
hibernateTemplate.delete(awardModel);
return 1;
}

相關文章