- 浏览: 259717 次
- 性别:
- 来自: 成都
文章分类
- 全部博客 (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:
yo8237233 写道你这样的话如果上传文件超过了50000 ...
Spring集成Commons fileupload,文件上传 -
yo8237233:
你这样的话如果上传文件超过了50000000就会报异常
Spring集成Commons fileupload,文件上传 -
zhuzhiguosnail:
<div class="quote_title ...
Spring Security 3多用户登录实现一 -
曾老师:
?????
Spring Security 3用户登录实现之十 用户切换
Hibernate中对于持久化实体间的关联关系有三种分类,第一个是一对一,第二个是一对多,第三个是多对多,对于多对一的关联关系认定为是一对多的反向,所以从实质上看这两者是一样的。而对于关联关系的设置,通常的做法是,对于一对一,将关联关系可以放在两方中的任何一方,而对于一对多,将关联关系放置在多方,由多方来控制一对多的关联关系,而对于多对多,需要将关联关系同时放置在两方,即由两方共同来控制多对多的关联关系,不过由于这样的做法带来很大的数据冗余,及其它不合理之处,所以这时会开辟第三张表用来映射多对多的关联关系。那么对于一对多或一对一的关联关系,我想将它们的关联关系用第三张表来表示,应该如何做呢?这就引出来了Hibernate的join的用法,它为我们提供了将一对一或多对多关联关系以第三张表的方式来表示,而不是由关联关系的两方的其中一方去控制。
一。Husband
package com.dream.model.couple; /** * Created by IntelliJ IDEA. * User: Zhong Gang * Date: 11-10-15 * Time: 下午3:53 */ public class Husband { private Integer id; private String name; private Wife wife; public Husband(String name) { this.name = name; } public Husband() { } public Husband(String name, Wife wife) { this.name = name; this.wife = wife; } public Wife wife() { return this.wife; } }
<?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.couple.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"/>
<join table="couple">
<key column="husbandid"/>
<many-to-one name="wife" lazy="false" column="wifeid" class="com.dream.model.couple.Wife" unique="true"
cascade="save-update"/>
</join>
</class>
</hibernate-mapping>
二。Wife
package com.dream.model.couple; /** * Created by IntelliJ IDEA. * User: Zhong Gang * Date: 11-10-15 * Time: 下午3:52 */ public class Wife { private Integer id; private String name; private Husband husband; public Wife(String name) { this.name = name; } public Wife() { } public String name() { return this.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.couple.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"/>
<one-to-one name="husband" class="com.dream.model.couple.Husband"/>
</class>
</hibernate-mapping>
三。CoupleDao
package com.dream.dao.standard; import com.dream.model.couple.Husband; import java.util.List; /** * Created by IntelliJ IDEA. * User: Zhong Gang * Date: 11-10-15 * Time: 下午4:24 */ public interface CoupleDao { void saveOrUpdateHusband(Husband husband); List<Husband> findHusbandsByName(String name); }
package com.dream.dao; import com.dream.dao.standard.CoupleDao; import com.dream.model.couple.Husband; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import java.util.List; /** * Created by IntelliJ IDEA. * User: Zhong Gang * Date: 11-10-15 * Time: 下午4:24 */ public class CoupleDaoImpl extends HibernateDaoSupport implements CoupleDao { @Override public void saveOrUpdateHusband(Husband husband) { getHibernateTemplate().saveOrUpdate(husband); } @Override public List<Husband> findHusbandsByName(String name) { return getHibernateTemplate().find("from Husband husband where husband.name=?", name); } }
四。CoupleService
package com.dream.service.standard; import com.dream.model.couple.Husband; import java.util.List; /** * Created by IntelliJ IDEA. * User: Zhong Gang * Date: 11-10-15 * Time: 下午4:23 */ public interface CoupleService { void saveOrUpdateHusband(Husband husband); List<Husband> loadHusbandsByName(String name); }
package com.dream.service; import com.dream.dao.standard.CoupleDao; import com.dream.model.couple.Husband; import com.dream.service.standard.CoupleService; import java.util.List; /** * Created by IntelliJ IDEA. * User: Zhong Gang * Date: 11-10-15 * Time: 下午4:23 */ public class CoupleServiceImpl implements CoupleService { private CoupleDao coupleDao; @Override public void saveOrUpdateHusband(Husband husband) { coupleDao.saveOrUpdateHusband(husband); } @Override public List<Husband> loadHusbandsByName(String name) { return coupleDao.findHusbandsByName(name); } public void setCoupleDao(CoupleDao coupleDao) { this.coupleDao = coupleDao; } }
五。testDB
db.url=jdbc:mysql://localhost:3306/couple?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf-8 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
六。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>classpath:/hibernate_mappings/Husband.hbm.xml</value> <value>classpath:/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="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>
七。Test
package com.dream.couple; import com.dream.model.couple.Husband; import com.dream.model.couple.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.List; /** * Created by IntelliJ IDEA. * User: Zhong Gang * Date: 11-10-15 * Time: 下午4:10 */ public class HibernateJoinTest 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 testOneToOneJoin() throws Exception { Wife wife = new Wife("wife"); Husband husband = new Husband("husband", wife); coupleService.saveOrUpdateHusband(husband); } public void testFindHusband() throws Exception { List<Husband> husbands = coupleService.loadHusbandsByName("husband"); assertEquals(1, husbands.size()); Husband husband = husbands.get(0); Wife wife = husband.wife(); assertEquals("wife", wife.name()); } }
跑完第一个测试,发出的sql语句
跑完第二个测试,发出的sql语句
Hibernate: select husband0_.id as id0_, husband0_.name as name0_, husband0_1_.wifeid as wifeid1_ from husband husband0_ inner join couple husband0_1_ on husband0_.id=husband0_1_.husbandid where husband0_.name=? Hibernate: select wife0_.id as id2_1_, wife0_.name as name2_1_, husband1_.id as id0_0_, husband1_.name as name0_0_, husband1_1_.wifeid as wifeid1_0_ from wife wife0_ left outer join husband husband1_ on wife0_.id=husband1_.id left outer join couple husband1_1_ on husband1_.id=husband1_1_.husbandid where wife0_.id=? Hibernate: select wife0_.id as id2_1_, wife0_.name as name2_1_, husband1_.id as id0_0_, husband1_.name as name0_0_, husband1_1_.wifeid as wifeid1_0_ from wife wife0_ left outer join husband husband1_ on wife0_.id=husband1_.id left outer join couple husband1_1_ on husband1_.id=husband1_1_.husbandid where wife0_.id=?
来看看跑完测试后,数据库中相关的数据表:
发表评论
-
Hibernate unique使用
2011-11-28 22:16 1106在Hibernate中对于一对一的关联关系配置 ... -
Hibernate cascade在不同关联关系中的具体表现
2011-11-22 21:47 3081在Hibernate中,针对持久化实体的配置文件中有 ... -
Hibernate element使用
2011-11-20 20:43 1537一.Person package co ... -
Hibernate 单表实现树结构
2011-11-13 21:10 2791来看看通过单表来实现一个树形结构的方式。这里假设有这样 ... -
Hibernate save persist merge探究
2011-10-27 17:59 4082先来看看hibernate-reference.pd ... -
Hibernate Search配置及简单应用
2011-10-25 16:06 2535Hibernate Search是一款集成Apach ... -
Hibernate的三种配置方式
2011-10-24 16:58 1769对于Hibernate的配置方式,有三种,一种是通过 ... -
dynamic-insert,dynamic-update 我有话要说
2011-10-21 17:39 12918在之前的一个项目中,Team leader说在每一个 ... -
Hibernate one-to-many composite-element实现
2011-10-17 16:55 2609Hibernate中持久化实体间一对多关联关系的compo ... -
基于DomainObject的通用数据库CRUD操作
2011-10-17 15:09 1554对于项目中的持久化实体,持久化实体虽然对应着不同的业务对象 ... -
Hibernate one-to-many 双方双向连接表关联
2011-10-17 14:24 1697Hibernate中持久化实体间一对多关联,具体关联关 ... -
Hibernate one-to-many 多方单向连接表关联
2011-10-15 23:37 1249Hibernate中持久化实体间一对多 ... -
Hibernate one-to-many 一方单向连接表关联
2011-10-15 23:17 1643Hibernate中对于持久化实体间的关联关系有 ... -
Hibernate fetch 抓取策略
2011-09-27 18:20 1312Hibernate fetch抓取策略定义了当抓 ... -
Hibernate 关联关系解除
2011-09-27 13:46 1502Hibernate对于持久化实体间的关联关系解 ... -
Hibernate one-to-many cascade探究
2011-09-08 13:20 1114对于Hibernate的one-to-many关联关系中, ... -
Hibernate inverse 深度解析
2011-08-09 22:55 1228inverse 英文意思为反向,倒转的。 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 one-to-one 一对一唯一外键关联映射_单向 and 双向
免费为大家服务 Hibernate one-to-many-annotation
如果在”一“一端维护一对多关联关系,hibernate会发出多余的udpate语句,所以我们一般在多的一端来维护关联关系。 加上inverse="true"这个属性,就可以强制在多的一端维护关系了。
NULL 博文链接:https://xiangtui.iteye.com/blog/680938
深入理解hibernate many-to-one(多对一)及 cascade(级联).
Hibernate-one-to-many
hibernate many-to-many级联保存,级联更新,级联删除
NULL 博文链接:https://yinxiaoyong.iteye.com/blog/426752
Hibernate应用例子one-to-one
hibernate-commons-annotations-4.0.1.Final.jar hibernate-core-4.1.12.Final.jar hibernate-ehcache-4.1.12.Final.jar hibernate-entitymanager-4.1.12.Final.jar hibernate-jpa-2.0-api-1.0.1.Final.jar ...
hibernate:one-to-one 的学习小经验
赠送jar包:hibernate-jpa-2.1-api-1.0.2.Final.jar; 赠送原API文档:hibernate-jpa-2.1-api-1.0.2.Final-javadoc.jar; 赠送源代码:hibernate-jpa-2.1-api-1.0.2.Final-sources.jar; 赠送Maven依赖信息文件:...
HibernateTools-3.2.4,Hibernate工具包,开发使用,欢迎下载
NULL 博文链接:https://zhouxianglh.iteye.com/blog/569471
赠送jar包:hibernate-jpa-2.1-api-1.0.2.Final.jar; 赠送原API文档:hibernate-jpa-2.1-api-1.0.2.Final-javadoc.jar; 赠送源代码:hibernate-jpa-2.1-api-1.0.2.Final-sources.jar; 赠送Maven依赖信息文件:...
NULL 博文链接:https://zhangyulong.iteye.com/blog/856191
NULL 博文链接:https://dreamzhong.iteye.com/blog/1201615
NULL 博文链接:https://dreamzhong.iteye.com/blog/1201437
NULL 博文链接:https://zhouxianglh.iteye.com/blog/568484
HibernateTools-3.2.4.Beta1-R200810311334.zip