Hibernate 一對多配置 級聯操作(級聯失敗問題分析解決)
一方:
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;
}
相關文章
- 解決 mybatis一對多分頁問題 聯級查詢MyBatis
- 關於Hibernate一對多關聯儲存問題
- Spring Boot整合Mybatis完成級聯一對多CRUD操作Spring BootMyBatis
- MySQL級聯複製的同步問題(一)MySql
- git clone失敗問題解決Git
- hibernate(八) Hibernate檢索策略(類級別,關聯級別,批量檢索)詳解
- hibernate(六) cascade(級聯)和inverse關係詳解
- 解決eclipse中android專案關聯android library project失敗問題EclipseAndroidProject
- hbase啟動失敗問題解決
- mongodb啟動失敗問題解決MongoDB
- MyBatis初級實戰之六:一對多關聯查詢MyBatis
- Laravel-admin 三級聯動 or 多級聯動 編輯 and 新建Laravel
- MindSpore聯邦學習框架解決行業級難題聯邦學習框架行業
- 跨平臺級聯dataguard配置
- hibernate懶載入導致多表聯合查詢失敗
- 解決Nessus外掛更新失敗問題
- 幾行程式碼實現ListView的多級聯動——多級聯動就是如此簡單行程View
- 【DG】Oracle之級聯DG--(cascade dg) --(一主一備一級聯)Oracle
- 系統升級到10.13之後cocoapods安裝失敗問題解決辦法
- Kubernetes slave節點升級至1.3版本註冊失敗問題解決
- MySQL 5.5級聯複製配置流程MySql
- hibernate二級快取問題!快取
- MyBatis表關聯 一對多 多對一 多對多MyBatis
- 解決一個.NET聯接SQL的問題 (轉)SQL
- Oracle 級聯表更新和SQLServer 級聯表更新OracleSQLServer
- Hibernate級聯關係的說明和優化優化
- JavaScript省市級聯效果詳解JavaScript
- pandas -- DataFrame的級聯以及合併操作
- Thinkpad T14升級Windows11ver22h2失敗問題解決小記ThinkPadWindows
- 模型關聯一對多模型
- MacOS升級到Monterey後python SSL握手失敗問題MacPython
- 操作hibernate多主鍵的問題?
- 阿里雲聯合帆軟釋出企業級BI分析解決方案,解決資料應用難題阿里
- hibernate配置級聯刪除時報錯:could not execute statement; SQL [n/a]; constraint [null]SQLAINull
- win10升級老失敗的解決方法_win10更新失敗怎麼辦Win10
- POSTGRESQL 小版本升級失敗後的原因分析SQL
- spring-data-jpa一對多、多對多雙向關聯,查詢操作的時候進入死迴圈問題Spring
- 合約 USDT 轉賬失敗的問題解決