`
Wind_ZhongGang
  • 浏览: 263706 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

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());
    }
}

 

  运行测试一结果:

 

  result1

 

   result2

 

  运行测试二结果:

 

  result3

  • 大小: 11 KB
  • 大小: 22.8 KB
  • 大小: 29.8 KB
1
1
分享到:
评论

相关推荐

    Hibernate通过Composite-element配置外联表实例 (码)

    标题中的“Hibernate通过Composite-element配置外联表实例”是指在Java持久化框架Hibernate中,使用Composite-element注解或XML配置来处理复杂类型的属性,这些属性通常涉及到一对多或一对一的关系,但它们被存储在...

    hibernate-mapping

    例如,`&lt;one-to-one&gt;`、`&lt;many-to-one&gt;`、`&lt;one-to-many&gt;`和`&lt;many-to-many&gt;`元素分别对应这四种关系。关联映射通常涉及外键的处理和级联操作。 5. 注解方式的Mapping 除了XML映射,Hibernate还支持注解方式的映射...

    Hibernate中文API大全

    &lt;many-to-one name="purchaseDetails" class="eg.Purchase"/&gt; &lt;many-to-one name="item" class="eg.Item"/&gt; &lt;/composite-element&gt; 在查询中,表达组合元素的语法和关联到其他实体的语法是一样的。 8.3. 组件...

    Hibernate集合映射

    在Order实体中,我们可以定义一个User类型的属性,并使用`&lt;many-to-one&gt;`标签映射。 ```xml &lt;many-to-one name="user" class="com.example.User" column="user_id"/&gt; ``` 然后,我们来了解**多对多映射**。在...

    hibernate常用配置

    - 使用 `&lt;one-to-one&gt;` 元素来实现一对一关联,`&lt;generator class="foreign"&gt;` 指定主键生成器为 foreign,确保与 `idCard` 共享相同的对象标识符(OID)。`constrained="true"` 添加了外键约束,保证 `Citizen` 的...

    Hibernate对象关系

    对于一对一的映射,可以使用`&lt;one-to-one&gt;`标签来实现: ```xml &lt;hibernate-mapping&gt; &lt;one-to-one name="bar" class="com.example.Bar" constrained="true"/&gt; &lt;/hibernate-mapping&gt; ``` 这里`...

    jpa-hibernate-tutorials:带有Spring Boot和Spring-Data-JPA的Hibernate教程

    Hibernate是Java领域广泛使用的对象关系映射(ORM)框架,它可以将Java对象映射到数据库表,实现对象和数据之间的透明交互。Hibernate提供了丰富的API,支持事务管理、查询语言(HQL)和第二级缓存等高级功能。 **...

    hibernate 帮助文档

    - **集合元素映射**:使用`&lt;element&gt;`或`&lt;one-to-many&gt;`元素来映射集合元素。 **1.2.5 双向关联** - **双向关系建立**:建立两个实体之间的双向关联。 - **关联维护**:双向关联中的级联操作和维护策略。 **1.2.6...

    Nhibernate2.2 配置模版

    例如,`bag`、`set`、`list`用于表示一对多关系,`one-to-one`用于一对一,`many-to-many`用于多对多。 4. **继承映射**:Nhibernate支持表继承和类继承两种方式。`&lt;subclass&gt;`和`&lt;joined-subclass&gt;`元素分别对应表...

    详解Java的Hibernat框架中的Map映射与SortedMap映射

    在Java的Hibernate框架中,Map映射和SortedMap映射是两种常见的集合类型,用于处理对象之间的关联关系,特别是处理一对多(One-to-Many)和多对一(Many-to-One)关系。Hibernate是一个强大的对象关系映射(ORM)...

Global site tag (gtag.js) - Google Analytics