- 浏览: 264734 次
- 性别:
- 来自: 成都
文章分类
- 全部博客 (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:
Wind_ZhongGang 写道lianglaiyang 写 ...
Spring Security 3多用户登录实现一 -
曾老师:
?????
Spring Security 3用户登录实现之十 用户切换
Hibernate中持久化实体间一对多关联,具体关联关系为多方,单向,连接表关联。
一。Husband
package com.dream.model.couple; import java.util.Set; /** * Created by IntelliJ IDEA. * User: Zhong Gang * Date: 11-10-15 * Time: 下午3:53 */ public class Husband { private Integer id; private String name; public Husband(String name) { this.name = name; } public 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.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"/> </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 Wife(String name, Husband husband) { this.name = name; this.husband = husband; } 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"/>
<join table="couple">
<key column="wifeid"/>
<many-to-one name="husband" column="husbandid" class="com.dream.model.couple.Husband"/>
</join>
</class>
</hibernate-mapping>
三。CoupleDao
package com.dream.dao.standard; import com.dream.model.couple.Husband; import com.dream.model.couple.Wife; 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); void saveOrUpdateWife(Wife wife); }
package com.dream.dao; import com.dream.dao.standard.CoupleDao; import com.dream.model.couple.Husband; import com.dream.model.couple.Wife; 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); } @Override public void saveOrUpdateWife(Wife wife) { getHibernateTemplate().saveOrUpdate(wife); } }
四。CoupleService
package com.dream.service.standard; import com.dream.model.couple.Husband; import com.dream.model.couple.Wife; 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); void saveOrUpdateWife(Wife wife); }
package com.dream.service; import com.dream.dao.standard.CoupleDao; import com.dream.model.couple.Husband; import com.dream.model.couple.Wife; 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); } @Override public void saveOrUpdateWife(Wife wife) { coupleDao.saveOrUpdateWife(wife); } 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.HashSet;
import java.util.List;
import java.util.Set;
/**
* Created by IntelliJ IDEA.
* User: Zhong Gang
* Date: 11-10-15
* Time: 下午4:10
*/
public class HibernateOneToManyJoinTest 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 {
Husband husband = new Husband("husband");
coupleService.saveOrUpdateHusband(husband);
Wife wife1 = new Wife("wife1", husband);
coupleService.saveOrUpdateWife(wife1);
Wife wife2 = new Wife("wife2", husband);
coupleService.saveOrUpdateWife(wife2);
Wife wife3 = new Wife("wife3", husband);
coupleService.saveOrUpdateWife(wife3);
}
}
跑完测试,发出的sql语句
来看看跑完测试后,数据库中相关的数据表及数据:
发表评论
-
Hibernate unique使用
2011-11-28 22:16 1176在Hibernate中对于一对一的关联关系配置 ... -
Hibernate cascade在不同关联关系中的具体表现
2011-11-22 21:47 3141在Hibernate中,针对持久化实体的配置文件中有 ... -
Hibernate element使用
2011-11-20 20:43 1589一.Person package co ... -
Hibernate 单表实现树结构
2011-11-13 21:10 2842来看看通过单表来实现一个树形结构的方式。这里假设有这样 ... -
Hibernate save persist merge探究
2011-10-27 17:59 4139先来看看hibernate-reference.pd ... -
Hibernate Search配置及简单应用
2011-10-25 16:06 2583Hibernate Search是一款集成Apach ... -
Hibernate的三种配置方式
2011-10-24 16:58 1811对于Hibernate的配置方式,有三种,一种是通过 ... -
dynamic-insert,dynamic-update 我有话要说
2011-10-21 17:39 12999在之前的一个项目中,Team leader说在每一个 ... -
Hibernate one-to-many composite-element实现
2011-10-17 16:55 2652Hibernate中持久化实体间一对多关联关系的compo ... -
基于DomainObject的通用数据库CRUD操作
2011-10-17 15:09 1614对于项目中的持久化实体,持久化实体虽然对应着不同的业务对象 ... -
Hibernate one-to-many 双方双向连接表关联
2011-10-17 14:24 1740Hibernate中持久化实体间一对多关联,具体关联关 ... -
Hibernate one-to-many 一方单向连接表关联
2011-10-15 23:17 1706Hibernate中对于持久化实体间的关联关系有 ... -
Hibernate one-to-one 两方双向连接表关联
2011-10-15 17:38 2754Hibernate中对于持久化实体间的关联关系有三种分类, ... -
Hibernate fetch 抓取策略
2011-09-27 18:20 1368Hibernate fetch抓取策略定义了当抓 ... -
Hibernate 关联关系解除
2011-09-27 13:46 1548Hibernate对于持久化实体间的关联关系解 ... -
Hibernate one-to-many cascade探究
2011-09-08 13:20 1159对于Hibernate的one-to-many关联关系中, ... -
Hibernate inverse 深度解析
2011-08-09 22:55 1275inverse 英文意思为反向,倒转的。 Hi ... -
Hibernate ORM one-to-one主键关联关系映射
2011-08-01 22:08 1397Hibernate ORM one-to-one映射主要有 ... -
Hibernate ORM Inheritance
2011-07-31 22:56 1372Hibernate ORM提供了三 ... -
Hibernate many-to-many
2011-07-30 17:20 1897对于持久化实体间多对多的关联关系,在数据库中的反映 ...
相关推荐
关联映射主要包括四种类型:一对一(One-to-One)、一对多(One-to-Many)、多对一(Many-to-One)和多对多(Many-to-Many)。下面我们将逐一探讨这些关联映射的配置方法。 1. **一对一关联映射** - **主键关联**...
关联通常包括以下几种类型:单向`one-to-many`关联、双向`one-to-many`关联、`many-to-one`关联以及`one-to-one`关联。 ##### 单向`one-to-many`关联 - **定义**:单向`one-to-many`关联是指一个实体可以拥有多个...
在多对多单向关联中,两个实体之间可以相互关联,这种关联通常通过第三个表(连接表或关联表)来实现。 **表结构示例**: - **Student表**: - `studentid`: 主键 - `name`: 姓名 - `age`: 年龄 - **Course表**:...
配置上,需要创建一个新的实体类来代表连接表,并在其中定义两个 `many-to-one` 属性,分别对应一对一关联的两个实体。 4. Hibernate 一对多外键单向关联: 在这种关联中,一个实体类(如 Department)可以有多个...
2. **一对多关联**(One-to-Many / Many-to-One) - 外键单向关联 - 连接表单向关联 - 外键双向关联 - 连接表双向关联 3. **多对多关联**(Many-to-Many) - 单向关联 - 双向关联 每种关联类型都有其特定的...
如果是通过连接表实现1-N关联,我们需要使用`<many-to-many>`标签,并设置`unique=true`来限制一对多的关系。连接表的配置需要在`<set>`标签中指定`table`属性。 **优点**:1端可以方便地管理和操作N端的所有对象...
在Person类中添加`private Address address`属性,并在`person.hbm.xml`中配置`<many-to-one>`元素,如`<many-to-one name="address" cascade="all" class="Address" column="address_id"/>`。这表示Person表中的...
- 在 `Person` 类中添加 `Address` 属性,并在映射文件中使用 `<one-to-one>` 或 `<many-to-one>` 进行关联映射。 --- 以上介绍的是 Hibernate 中一对一关联关系的三种不同方式,包括一对一连接表单向关联、一对...
<many-to-one name="address" class="Address" column="address_id"/> ``` - **基于PK**:同样只有一方包含了另一方的引用关联实体属性,但这里通过使用外键作为主键的一部分来实现。 - **映射示例**: ```xml ...
**多对一关联(Many-to-One)** 多对一关联是一对一关联的反转,即多个实体对应一个实体。例如,多个员工可以属于同一个部门。这种关系可以通过`@ManyToOne`注解实现。多对一关联的一方通常会包含对另一方的引用,而...
这使用`<many-to-one>`标签。 ```xml <many-to-one name="address" class="com.example.Address" column="address_id"/> ``` #### 2.1.7 多对一连接表单向关联 与上述相同,但通过连接表进行关联。 #### 2.1.8 多...
对于无连接表的N-1关联,我们可以在实体类的`Person`中添加`Address`对象,并在配置文件中设置`<many-to-one>`标签,指定外键列。而有连接表的N-1关联则需要使用`<join>`元素,通过连接表来建立关联。 B. 单向1-1...
在Java的持久化框架Hibernate中,多对多(Many-to-Many)关联是一种常见的关系映射类型,它用于表示两个实体之间可以有多个对应的关系。本文将深入探讨Hibernate中的多对多单向关联,并结合提供的资源`hibernate_...
- **7.3.2 多对一 (many-to-one)**:介绍使用连接表的多对一关联映射。 - **7.3.3 一对一 (One-to-one)**:说明使用连接表的一对一关联映射。 - **7.3.4 多对多 (many-to-many)**:解释多对多关联的映射。 - **...
在Hibernate中,数据关联分为一对一(One-to-One)、一对多(One-to-Many)、多对一(Many-to-One)和多对多(Many-to-Many)四种类型。每种关联都需要在实体类(Entity)上通过注解进行配置,例如: 1. **一对一...
本文将深入探讨Hibernate中的三种主要映射关系:一对一(One-to-One)、一对多(One-to-Many)以及多对多(Many-to-Many)。 ### 一对一映射(One-to-One) 一对一映射在现实世界中非常常见,例如一个人只有一个...
- `many-to-one`元素用于表示多方对一方的关系。 **单向关联**: **XML配置**: ```xml <!-- 部门的映射文件 --> <hibernate-mapping> <one-to-many class="com.example.Employee"/> </...