[转帖]Hibernate one-to-many composite-element实现_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 4319 | 回复: 0   主题: [转帖]Hibernate one-to-many composite-element实现        下一篇 
fozhyn
注册用户
等级:上士
经验:317
发帖:101
精华:0
注册:2011-10-18
状态:离线
发送短消息息给fozhyn 加好友    发送短消息息给fozhyn 发消息
发表于: IP:您无权察看 2011-10-19 15:05:06 | [全部帖] [楼主帖] 楼主

    Hibernate中持久化实体间一对多关联关系的composite-element的实现方式。

    一。DomainObject

Java代码   北京联动北方科技有限公司


package com.dream.model.couple;
public class DomainObject {
      private Integer id;
      private Integer version;
}


  二。Wife

Java代码   北京联动北方科技有限公司


package com.dream.model.couple;
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

Java代码   北京联动北方科技有限公司


package com.dream.model.couple;
import java.util.Set;
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;
      }
}<span style="white-space: normal; background-color: #ffffff;"> </span>


Java代码   北京联动北方科技有限公司


<?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>


  四。测试

Java代码   北京联动北方科技有限公司


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


  运行测试一结果:

    北京联动北方科技有限公司

    北京联动北方科技有限公司

    运行测试二结果:

    北京联动北方科技有限公司




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论