一方:
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;
}