- 浏览: 263698 次
- 性别:
- 来自: 成都
文章分类
- 全部博客 (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 one-to-many composite-element实现
Hibernate中持久化实体间一对多关联关系的composite-element的实现方式。
一。DomainObject
package com.dream.model.couple; /** * Created by IntelliJ IDEA. * User: Zhong Gang * Date: 10/17/11 * Time: 1:05 PM */ public class DomainObject { private Integer id; private Integer version; }
二。Wife
package com.dream.model.couple; /** * Created by IntelliJ IDEA. * User: Zhong Gang * Date: 10/17/11 * Time: 1:06 PM */ public class Wife{ private String name; private Husband husband; public Wife(String name) { this.name = name; } public Wife() { } public Husband husband() { return this.husband; } public void setHusband(Husband husband) { this.husband = husband; } public Husband getHusband() { return husband; } }
三。Husband
package com.dream.model.couple;
import java.util.Set;
/**
* Created by IntelliJ IDEA.
* User: Zhong Gang
* Date: 10/17/11
* Time: 1:06 PM
*/
public class Husband extends DomainObject {
private String name;
private Set<Wife> wifes;
public Husband(String name, Set<Wife> wifes) {
this.name = name;
this.wifes = wifes;
}
public Husband() {
}
public String name() {
return name;
}
public Set<Wife> wifes() {
return this.wifes;
}
}
<?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> <version name="version" column="version" type="java.lang.Integer"/> <property name="name" column="name" type="java.lang.String"/> <set name="wifes" cascade="all" table="couple" lazy="false"> <key column="husbandid"/> <composite-element class="com.dream.model.couple.Wife"> <parent name="husband"/> <property name="name" column="name" type="java.lang.String"/> </composite-element> </set> </class> </hibernate-mapping>
四。测试
package com.dream.couple; import com.dream.model.couple.DomainObject; 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.Iterator; import java.util.Set; /** * Created by IntelliJ IDEA. * User: Zhong Gang * Date: 10/17/11 * Time: 1:31 PM */ public class HibernateOneToManyCompositeElementTest 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 testOneToManyCompositeElement() throws Exception { Wife wife1 = new Wife("wife1"); Wife wife2 = new Wife("wife2"); Wife wife3 = new Wife("wife3"); Set<Wife> wifes = new HashSet<Wife>(); wifes.add(wife1); wifes.add(wife2); wifes.add(wife3); Husband husband = new Husband("husband", wifes); coupleService.saveOrUpdate(husband); } public void testCompositeElement() throws Exception { Husband husband = (Husband) coupleService.findById(Husband.class, Integer.valueOf(1)); Set<Wife> wifes = husband.wifes(); assertEquals(3, wifes.size()); } }
运行测试一结果:
运行测试二结果:
发表评论
-
Hibernate unique使用
2011-11-28 22:16 1155在Hibernate中对于一对一的关联关系配置 ... -
Hibernate cascade在不同关联关系中的具体表现
2011-11-22 21:47 3131在Hibernate中,针对持久化实体的配置文件中有 ... -
Hibernate element使用
2011-11-20 20:43 1577一.Person package co ... -
Hibernate 单表实现树结构
2011-11-13 21:10 2831来看看通过单表来实现一个树形结构的方式。这里假设有这样 ... -
Hibernate save persist merge探究
2011-10-27 17:59 4128先来看看hibernate-reference.pd ... -
Hibernate Search配置及简单应用
2011-10-25 16:06 2576Hibernate Search是一款集成Apach ... -
Hibernate的三种配置方式
2011-10-24 16:58 1802对于Hibernate的配置方式,有三种,一种是通过 ... -
dynamic-insert,dynamic-update 我有话要说
2011-10-21 17:39 12983在之前的一个项目中,Team leader说在每一个 ... -
基于DomainObject的通用数据库CRUD操作
2011-10-17 15:09 1599对于项目中的持久化实体,持久化实体虽然对应着不同的业务对象 ... -
Hibernate one-to-many 双方双向连接表关联
2011-10-17 14:24 1729Hibernate中持久化实体间一对多关联,具体关联关 ... -
Hibernate one-to-many 多方单向连接表关联
2011-10-15 23:37 1287Hibernate中持久化实体间一对多 ... -
Hibernate one-to-many 一方单向连接表关联
2011-10-15 23:17 1692Hibernate中对于持久化实体间的关联关系有 ... -
Hibernate one-to-one 两方双向连接表关联
2011-10-15 17:38 2746Hibernate中对于持久化实体间的关联关系有三种分类, ... -
Hibernate fetch 抓取策略
2011-09-27 18:20 1356Hibernate fetch抓取策略定义了当抓 ... -
Hibernate 关联关系解除
2011-09-27 13:46 1540Hibernate对于持久化实体间的关联关系解 ... -
Hibernate one-to-many cascade探究
2011-09-08 13:20 1152对于Hibernate的one-to-many关联关系中, ... -
Hibernate inverse 深度解析
2011-08-09 22:55 1266inverse 英文意思为反向,倒转的。 Hi ... -
Hibernate ORM one-to-one主键关联关系映射
2011-08-01 22:08 1393Hibernate ORM one-to-one映射主要有 ... -
Hibernate ORM Inheritance
2011-07-31 22:56 1358Hibernate ORM提供了三 ... -
Hibernate many-to-many
2011-07-30 17:20 1889对于持久化实体间多对多的关联关系,在数据库中的反映 ...
相关推荐
标题中的“Hibernate通过Composite-element配置外联表实例”是指在Java持久化框架Hibernate中,使用Composite-element注解或XML配置来处理复杂类型的属性,这些属性通常涉及到一对多或一对一的关系,但它们被存储在...
例如,`<one-to-one>`、`<many-to-one>`、`<one-to-many>`和`<many-to-many>`元素分别对应这四种关系。关联映射通常涉及外键的处理和级联操作。 5. 注解方式的Mapping 除了XML映射,Hibernate还支持注解方式的映射...
<many-to-one name="purchaseDetails" class="eg.Purchase"/> <many-to-one name="item" class="eg.Item"/> </composite-element> 在查询中,表达组合元素的语法和关联到其他实体的语法是一样的。 8.3. 组件...
在Order实体中,我们可以定义一个User类型的属性,并使用`<many-to-one>`标签映射。 ```xml <many-to-one name="user" class="com.example.User" column="user_id"/> ``` 然后,我们来了解**多对多映射**。在...
- 使用 `<one-to-one>` 元素来实现一对一关联,`<generator class="foreign">` 指定主键生成器为 foreign,确保与 `idCard` 共享相同的对象标识符(OID)。`constrained="true"` 添加了外键约束,保证 `Citizen` 的...
对于一对一的映射,可以使用`<one-to-one>`标签来实现: ```xml <hibernate-mapping> <one-to-one name="bar" class="com.example.Bar" constrained="true"/> </hibernate-mapping> ``` 这里`...
Hibernate是Java领域广泛使用的对象关系映射(ORM)框架,它可以将Java对象映射到数据库表,实现对象和数据之间的透明交互。Hibernate提供了丰富的API,支持事务管理、查询语言(HQL)和第二级缓存等高级功能。 **...
- **集合元素映射**:使用`<element>`或`<one-to-many>`元素来映射集合元素。 **1.2.5 双向关联** - **双向关系建立**:建立两个实体之间的双向关联。 - **关联维护**:双向关联中的级联操作和维护策略。 **1.2.6...
例如,`bag`、`set`、`list`用于表示一对多关系,`one-to-one`用于一对一,`many-to-many`用于多对多。 4. **继承映射**:Nhibernate支持表继承和类继承两种方式。`<subclass>`和`<joined-subclass>`元素分别对应表...
在Java的Hibernate框架中,Map映射和SortedMap映射是两种常见的集合类型,用于处理对象之间的关联关系,特别是处理一对多(One-to-Many)和多对一(Many-to-One)关系。Hibernate是一个强大的对象关系映射(ORM)...