- 浏览: 259690 次
- 性别:
- 来自: 成都
文章分类
- 全部博客 (87)
- Java (6)
- Frameworks (1)
- JavaWeb (3)
- Spring (6)
- Hibernate (26)
- Maven (2)
- Plugin (1)
- Velocity (1)
- Freemarker (1)
- Spring security (18)
- Google (5)
- Guice (5)
- rmi (1)
- Annotation (1)
- Binding (1)
- commons-fileupload (3)
- fileupload (3)
- ehcache (1)
- ApplicationContext (1)
- Resource (1)
- Validator (1)
- PropertyEditor (1)
- one-to-many (5)
- cascade (2)
- MessageSource (1)
- Serialize (1)
- Serializable (1)
- delete (1)
- delete-orphan (1)
- fetch (1)
- one-to-one (1)
- join (4)
- DomainObject (1)
- CRUD (1)
- composite-element (1)
- Hibernate ORM (14)
- dynamic-insert (1)
- dynamic-update (1)
- Search (1)
- DDD (0)
- Job (0)
- element (1)
- Unique (1)
- Group (1)
- tomcat (1)
- https (1)
- mysql (1)
最新评论
-
xurunchengof:
[url][url][url][img][img][img][ ...
Spring Security 3多用户登录实现之三 验证过滤器 -
Wind_ZhongGang:
<div class="quote_title ...
Spring集成Commons fileupload,文件上传 -
yo8237233:
你这样的话如果上传文件超过了50000000就会报异常
Spring集成Commons fileupload,文件上传 -
zhuzhiguosnail:
Wind_ZhongGang 写道lianglaiyang 写 ...
Spring Security 3多用户登录实现一 -
曾老师:
?????
Spring Security 3用户登录实现之十 用户切换
Hibernate 关联关系解除
Hibernate对于持久化实体间的关联关系解除,有两种实现方式,一种是将关联关系实体中彻底删除,而另一种则是将关联关系的外键值设为空,即NULL。两种方式在配置文件中的体现为为cascade设置不同的值。
一。Husband
package com.dream.model.join; import java.util.Set; /** * Created by IntelliJ IDEA. * User: Zhong Gang * Date: 9/26/11 * Time: 5:46 PM */ public class Husband { private Integer id; private String name; private Set<Wife> wifes; public Husband(String name) { this.name = name; } public Husband() { } public Husband(String name, Set<Wife> wifes) { this.name = name; this.wifes = wifes; } public Set<Wife> getWifes() { return wifes; } public Integer getId() { return id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
<?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 default-access="field"> <class name="com.dream.model.join.Husband" table="husband" dynamic-insert="true" dynamic-update="true"> <id name="id" column="id" type="java.lang.Integer"> <generator class="native"/> </id> <property name="name" column="name" type="java.lang.String"/> <set name="wifes" table="wife" cascade="all-delete-orphan"> <key column="husbandid"/> <one-to-many class="com.dream.model.join.Wife"/> </set> </class> </hibernate-mapping>
二。Wife
package com.dream.model.join; /** * Created by IntelliJ IDEA. * User: Zhong Gang * Date: 9/26/11 * Time: 5:47 PM */ public class Wife { private Integer id; private String name; private Husband husband; public Wife(String name) { this.name = name; } public Wife() { } public Wife(String name, Husband husband) { this.name = name; this.husband = husband; } public String getName() { return name; } public Husband getHusband() { return husband; } }
<?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 default-access="field"> <class name="com.dream.model.join.Wife" table="wife" dynamic-insert="true" dynamic-update="true"> <id name="id" column="id" type="java.lang.Integer"> <generator class="native"/> </id> <property name="name" column="name" type="java.lang.String"/> <many-to-one name="husband" class="com.dream.model.join.Husband" column="husbandid"/> </class> </hibernate-mapping>
三。CoupleDao
package com.dream.dao.standard; import com.dream.model.join.Husband; import com.dream.model.join.Wife; import java.util.Set; /** * Created by IntelliJ IDEA. * User: Zhong Gang * Date: 9/26/11 * Time: 5:51 PM */ public interface CoupleDao { Husband findHusbandById(Integer id); void saveOrUpdateHusband(Husband husband); void deleteHusband(Husband husband); }
package com.dream.dao; import com.dream.dao.standard.CoupleDao; import com.dream.model.join.Husband; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import java.util.List; /** * Created by IntelliJ IDEA. * User: Zhong Gang * Date: 9/26/11 * Time: 5:52 PM */ public class CoupleDaoImpl extends HibernateDaoSupport implements CoupleDao { public Husband findHusbandById(Integer id) { List<Husband> husbands = getHibernateTemplate().find("from Husband husband where husband.id=?", id); if (husbands.isEmpty()) { return null; } return husbands.get(0); } public void saveOrUpdateHusband(Husband husband) { getHibernateTemplate().saveOrUpdate(husband); } public void deleteHusband(Husband husband) { getHibernateTemplate().delete(husband); } }
四。CoupleService
package com.dream.service.standard; import com.dream.model.join.Husband; /** * Created by IntelliJ IDEA. * User: Zhong Gang * Date: 9/26/11 * Time: 5:53 PM */ public interface CoupleService { Husband loadHusbandById(Integer id); void saveOrUpdateHusband(Husband husband); void deleteHusbandById(Integer id); }
package com.dream.service; import com.dream.dao.standard.CoupleDao; import com.dream.exception.DataNotExistException; import com.dream.model.join.Husband; import com.dream.service.standard.CoupleService; /** * Created by IntelliJ IDEA. * User: Zhong Gang * Date: 9/26/11 * Time: 5:53 PM */ public class CoupleServiceImpl implements CoupleService { private CoupleDao coupleDao; public Husband loadHusbandById(Integer id) { Husband husband = coupleDao.findHusbandById(id); if (husband == null) { throw new DataNotExistException(); } return husband; } public void saveOrUpdateHusband(Husband husband) { coupleDao.saveOrUpdateHusband(husband); } public void deleteHusbandById(Integer id) { Husband husband = coupleDao.findHusbandById(id); coupleDao.deleteHusband(husband); } public void setCoupleDao(CoupleDao coupleDao) { this.coupleDao = coupleDao; } }
package com.dream.exception; /** * Created by IntelliJ IDEA. * User: Zhong Gang * Date: 9/26/11 * Time: 6:01 PM */ public class DataNotExistException extends RuntimeException { public DataNotExistException() { super("The data you find does not exist in the database."); } }
五。testDatasource
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" default-autowire="byName"> <context:property-placeholder location="classpath:testDB.properties"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${db.driver}"/> <property name="url" value="${db.url}"/> <property name="username" value="${db.username}"/> <property name="password" value="${db.password}"/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mappingLocations"> <list> <value>/hibernate_mappings/Husband.hbm.xml</value> <value>/hibernate_mappings/Wife.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop> </props> </property> </bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="service" expression="execution(* com.dream.service..*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="service"/> </aop:config> <bean id="coupleDao" class="com.dream.dao.CoupleDaoImpl"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <bean id="coupleService" class="com.dream.service.CoupleServiceImpl"> <property name="coupleDao" ref="coupleDao"/> </bean> </beans>
六。testDB
db.url=jdbc:mysql://localhost:3306/test_fetch db.driver=com.mysql.jdbc.Driver db.username=root db.password=root hibernate.dialect=org.hibernate.dialect.MySQL5Dialect hibernate.show_sql=true hibernate.hbm2ddl.auto=update hibernate.jdbc.batch_size=100
七。TestCase
package com.fetch; import com.dream.model.join.Husband; import com.dream.model.join.Wife; import com.dream.service.standard.CoupleService; import junit.framework.TestCase; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.HashSet; import java.util.Set; /** * Created by IntelliJ IDEA. * User: Zhong Gang * Date: 9/26/11 * Time: 6:05 PM */ public class HibernateCascadeTest extends TestCase { private CoupleService coupleService; @Override public void setUp() throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext("classpath:testDatasource.xml"); coupleService = (CoupleService) context.getBean("coupleService"); } public void testCascadeAllDelete() throws Exception { Wife wife1 = new Wife("Wife1"); Wife wife2 = new Wife("Wife2"); Set<Wife> wifes = new HashSet<Wife>(); wifes.add(wife1); wifes.add(wife2); Husband husband = new Husband("Husband1", wifes); coupleService.saveOrUpdateHusband(husband); coupleService.deleteHusbandById(husband.getId()); } }
Scenario 1:cascade="all-delete-orphan" in Husband.hbm.xml
Hibernate: insert into husband (name) values (?) Hibernate: insert into wife (name) values (?) Hibernate: insert into wife (name) values (?) Hibernate: update wife set husbandid=? where id=? Hibernate: update wife set husbandid=? where id=? Hibernate: select husband0_.id as id0_, husband0_.name as name0_ from husband husband0_ where husband0_.id=? Hibernate: select wifes0_.husbandid as husbandid0_1_, wifes0_.id as id1_, wifes0_.id as id1_0_, wifes0_.name as name1_0_, wifes0_.husbandid as husbandid1_0_ from wife wifes0_ where wifes0_.husbandid=? Hibernate: update wife set husbandid=null where husbandid=? Hibernate: delete from wife where id=? Hibernate: delete from wife where id=? Hibernate: delete from husband where id=?
Scenario 2:cascade="all" in Husband.hbm.xml
结论同上
Scenario 3:cascade="delete" in Husband.hbm.xml
结论同上
Scenario 4:cascade="delete-orphan" in Husband.hbm.xml
结论同上
Scenario 5:cascade="save-update" in Husband.hbm.xml
Hibernate: insert into husband (name) values (?) Hibernate: insert into wife (name) values (?) Hibernate: insert into wife (name) values (?) Hibernate: update wife set husbandid=? where id=? Hibernate: update wife set husbandid=? where id=? Hibernate: select husband0_.id as id0_, husband0_.name as name0_ from husband husband0_ where husband0_.id=? Hibernate: update wife set husbandid=null where husbandid=? Hibernate: delete from husband where id=?
发表评论
-
Hibernate unique使用
2011-11-28 22:16 1106在Hibernate中对于一对一的关联关系配置 ... -
Hibernate cascade在不同关联关系中的具体表现
2011-11-22 21:47 3080在Hibernate中,针对持久化实体的配置文件中有 ... -
Hibernate element使用
2011-11-20 20:43 1537一.Person package co ... -
Hibernate 单表实现树结构
2011-11-13 21:10 2790来看看通过单表来实现一个树形结构的方式。这里假设有这样 ... -
Hibernate save persist merge探究
2011-10-27 17:59 4081先来看看hibernate-reference.pd ... -
Hibernate Search配置及简单应用
2011-10-25 16:06 2534Hibernate Search是一款集成Apach ... -
Hibernate的三种配置方式
2011-10-24 16:58 1769对于Hibernate的配置方式,有三种,一种是通过 ... -
dynamic-insert,dynamic-update 我有话要说
2011-10-21 17:39 12917在之前的一个项目中,Team leader说在每一个 ... -
Hibernate one-to-many composite-element实现
2011-10-17 16:55 2609Hibernate中持久化实体间一对多关联关系的compo ... -
基于DomainObject的通用数据库CRUD操作
2011-10-17 15:09 1553对于项目中的持久化实体,持久化实体虽然对应着不同的业务对象 ... -
Hibernate one-to-many 双方双向连接表关联
2011-10-17 14:24 1697Hibernate中持久化实体间一对多关联,具体关联关 ... -
Hibernate one-to-many 多方单向连接表关联
2011-10-15 23:37 1248Hibernate中持久化实体间一对多 ... -
Hibernate one-to-many 一方单向连接表关联
2011-10-15 23:17 1643Hibernate中对于持久化实体间的关联关系有 ... -
Hibernate one-to-one 两方双向连接表关联
2011-10-15 17:38 2712Hibernate中对于持久化实体间的关联关系有三种分类, ... -
Hibernate fetch 抓取策略
2011-09-27 18:20 1311Hibernate fetch抓取策略定义了当抓 ... -
Hibernate one-to-many cascade探究
2011-09-08 13:20 1114对于Hibernate的one-to-many关联关系中, ... -
Hibernate inverse 深度解析
2011-08-09 22:55 1227inverse 英文意思为反向,倒转的。 Hi ... -
Hibernate ORM one-to-one主键关联关系映射
2011-08-01 22:08 1344Hibernate ORM one-to-one映射主要有 ... -
Hibernate ORM Inheritance
2011-07-31 22:56 1312Hibernate ORM提供了三 ... -
Hibernate many-to-many
2011-07-30 17:20 1850对于持久化实体间多对多的关联关系,在数据库中的反映 ...
相关推荐
hibernate关联关系
Hibernate 关联关系映射分类
Hibernate关联关系练习【全】
详细讲解hibernate的关联关系,以及用实例讲解,非常详细
Hibernate关联关系映射 单向关联 │ ├─ 一对一外键单向关联 │ ├─ 一对一主键单向关联 │ ├─ 一对一连接表单向关联 │ ├─ 一对多外键单向关联 │ ├─ 一对多连接表单向关联 │ ├─ 多对一外键单向关联 │ ...
hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,包含4个说明文档,分别详细解说了hibernate关联映射的关联关系,...
Hibernate关联关系.docHibernate关联关系.doc
映射一对多关联关系 以Customer和Order为例: 一对多: 每个Customer可以有一个或者多个Order,因此Customer中应该有一个集合类型的属性,来引用所有关联的Order对象。 多对一。全实例讲解
Hibernate关联关系总结 一对多 多对一 多对多
大致总结了一下Hibernate关联关系。提供相关的一些例子进行测试。
hibernate里关联关系映射,各种关系解析,帮助大家搞清楚hibernate的关系映射
hibernate关联关系简单项目,也可以只运行Java文件不用JSP页面,运用MySQL
Hibernate4中映射关系图解。
Hibernate关联关系映射.CHM Hibernate文档相关
讲诉了关联的创建和使用 整理成了一份txt的文档 整个创建到使用的步骤都写在里面了
hibernate关联关系多对一实例
针对hibernate中关于对象间的关联关系的配置
Hibernate关联关系映射实例速查,帮助初学者学习。
Hibernate关联关系配置,一对一,一对多,多对多,联合主键等的配置