Hibernate一对多单向关联映射

原创 2007年10月15日 19:17:00

                   一对多关联映射(单向)

 

1、People.hbm.xml

 

<?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-lazy="false">  
   
<class name="com.persistent.People" table="people">     
     
<id name="id" column="peopleId" unsaved-value="0">    
        
<generator class="increment">        
        
</generator>    
     
</id>     
     
<property name="name" column="name"></property>     
     
<set name="addresses">
        
<key column="peopleId" not-null="true"/>
        
<one-to-many class="com.persistent.Address"/>
     
</set>     
   
</class>
</hibernate-mapping>

 

2、address.hbm.xml

 

 

<?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>
  
<class name="com.persistent.Address" table="address">  
    
<id name="id" column="addressId" unsaved-value="0">
        
<generator class="increment">
        
</generator>
    
</id>
     
<property name="peopleId" column="peopleId" 
                    insert
="false" update="false">
    
</property>    
    
<property name="addressName" column="addressName"></property>
  
</class>  
</hibernate-mapping>

 

3、测试程序

 

  

Session session = HibernateSessionFactory.getSession();
session.beginTransaction();
List result 
= session.createQuery("from People").list();    
session.getTransaction().commit();
for(int i = 0; i<result.size(); i++){
   People people 
= (People)result.get(i);
   System.out.println(people.getId()
+"    "+people.getName());
   Set addresses 
= people.getAddresses();
  
for(Iterator it = addresses.iterator(); it.hasNext();){
      Address address 
= (Address)it.next();
      System.out.println(address.getId()
+" "
                        
+address.getPeopleId()+" "
                        
+address.getAddressName());
   }

}
        

 

 

如果加了阴影部分,则运行结果为:

Hibernate: select people0_.peopleId as peopleId0_, people0_.name as name0_ from people people0_

Hibernate: select addresses0_.peopleId as peopleId1_, addresses0_.addressId as addressId1_, addresses0_.addressId as addressId1_0_, addresses0_.peopleId as peopleId1_0_, addresses0_.addressName as addressN3_1_0_ from address addresses0_ where addresses0_.peopleId=?

Hibernate: select addresses0_.peopleId as peopleId1_, addresses0_.addressId as addressId1_, addresses0_.addressId as addressId1_0_, addresses0_.peopleId as peopleId1_0_, addresses0_.addressName as addressN3_1_0_ from address addresses0_ where addresses0_.peopleId=?

1    lucy

2   1    HubeiWuhan

1   1    Hunan Shaoyang

2    lily

如果不加阴影部分,则运行结果为

Hibernate: select people0_.peopleId as peopleId0_, people0_.name as name0_ from people people0_

1    lucy

Hibernate: select addresses0_.peopleId as peopleId1_, addresses0_.addressId as addressId1_, addresses0_.addressId as addressId1_0_, addresses0_.peopleId as peopleId1_0_, addresses0_.addressName as addressN3_1_0_ from address addresses0_ where addresses0_.peopleId=?

1   1    Hunan Shaoyang

2   1    HubeiWuhan

2    lily

Hibernate: select addresses0_.peopleId as peopleId1_, addresses0_.addressId as addressId1_, addresses0_.addressId as addressId1_0_, addresses0_.peopleId as peopleId1_0_, addresses0_.addressName as addressN3_1_0_ from address addresses0_ where addresses0_.peopleId=?

默认default-lazytrue,只有当程序中访问到PeopleAddresses时才会从数据库中加载。设置为false后,程序在加载People时立即就把它关联的Addresses全部加载进来。

另外,只有在People端设置才有效。

【SSH进阶之路】Hibernate映射——多对一单向关联映射(四)

基本映射是对一个实体进行映射,关联映射就是处理多个实体之间的关系,将关联关系映射到数据库中,所谓的关联关系在对象模型中有一个或多个引用。...
  • jiuqiyuliang
  • jiuqiyuliang
  • 2014-10-22 08:15:25
  • 31963

hibernate单向一对多关联映射(注解版)

  • 2017年05月04日 15:30
  • 20KB
  • 下载

【Hibernate步步为营】--(一对多映射)之单向关联

上篇文章讨论了双向关联的一对一映射,用了两个章节,主要是从主键和外键两种关联映射展开详细讨论,双向关联的映射需要在两个映射文件中分别添加相互的对应关系,斌刚在相应的类中添加对应的关联类的属性,这样在一...
  • zhang_xinxiu
  • zhang_xinxiu
  • 2014-05-24 09:06:12
  • 5867

hibernate5(10)注解映射[2]一对多单向关联

一对多单向关联映射在实际的博客网站中,用户肯定需要获取自己所写的文章,这时可以建立用户(一)对文章(多)的单向关联映射。 先来看我们的一方配置实例package com.zeng.model;imp...
  • qwe6112071
  • qwe6112071
  • 2016-04-07 09:26:33
  • 6261

Hibernate关联映射——一对多(多对一)

Hibernate关联映射——一对多(多对一)我们以客户(Customer)与订单(Order)为例来讲解Hibernate关联映射中的一对多关联关系。 首先肯定是搭建好Hibernate的开发环境...
  • yerenyuan_pku
  • yerenyuan_pku
  • 2017-04-13 02:36:59
  • 1759

【SSH进阶之路】Hibernate映射——一对多关联映射(七)

一对多关联映射和多对一关联映射的映射原理是一致的,都是在多的一端加入一个外键,指向一的一端。关联关系都是由多端维护,一端维护多端的关系,在加载一端时,可以将多端加载上来。...
  • jiuqiyuliang
  • jiuqiyuliang
  • 2014-11-16 08:10:16
  • 17013

Hibernate从入门到精通(4)- 单向 一对多 多对一

单向一对多在one的一方进行操作,多对一在many的一方进行操作。
  • u011499992
  • u011499992
  • 2015-10-30 15:17:34
  • 707

hibernate关联映射详解

  • 2011年06月29日 20:02
  • 24KB
  • 下载

【SSH进阶之路】Hibernate映射——多对多关联映射(八)

多对多映射是现实生活中最常见的映射,也是最容易理解的映射。废话少说,直接开始。   映射原理        不论是单向关联还是双向关联都是通过第三张表,将两个表中的主键放到第三张做一个关联。用第三张表...
  • jiuqiyuliang
  • jiuqiyuliang
  • 2014-11-22 08:14:13
  • 15278

hibernate单向一对多关联映射(one-to-many)XML与注解版

单向一对多关联映射原理:  hibernate对于一对多的关系的处理,是通过操作classess端,间接操作或者自动操作Student端。比如添加,我直接添加classess端的数据,多个S...
  • u011781521
  • u011781521
  • 2017-05-04 15:37:06
  • 1083
收藏助手
不良信息举报
您举报文章:Hibernate一对多单向关联映射
举报原因:
原因补充:

(最多只允许输入30个字)