`
giga_Zhang
  • 浏览: 153535 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Hibernate关联一对多关系单向映射的两种方式

阅读更多

第一种方式:在数据库中不需要中间表,而是直接在多方的表中加以列一方的id;

看一下建表的SQL

 

sql 代码
  1. create table people(id bigint not null auto_increment primary key,name varchar(20) not null);   
  2. create table location(id bigint not null auto_increment,peopleId bigint not null,addr varchar(20) not null,primary key(id,peopleId));   

在location表中添加一个peopleId的字段以存储people表的id,已确定关联关系;

下面分别看一下两个类的映射文件:

People类:

xml 代码
  1. <?xml version="1.0"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC    
  3.     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"   
  4.     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  5. <hibernate-mapping package="org.test.bean">  
  6.   
  7.     <class name="People" table="people">  
  8.         <id name="id" column="id">  
  9.             <generator class="native"/>  
  10.         </id>  
  11.         <property name="name" type="string" column="name"/>  
  12.         <set name="locations" cascade="save-update" lazy="false">  
  13.             <key column="peopleId"/>  
  14.             <one-to-many class="Location"/>  
  15.         </set>  
  16.     </class>  
  17.        
  18. </hibernate-mapping>  

Location 类:

xml 代码
  1. <?xml version="1.0"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC    
  3.     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"   
  4.     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  5. <hibernate-mapping    
  6.     package="org.test.bean">  
  7.   
  8.     <class name="Location" table="location">  
  9.         <id name="id" column="id">  
  10.             <generator class="native"/>  
  11.         </id>  
  12.         <property name="peopleId" type="integer" column="peopleId"/>  
  13.         <property name="addr" type="string" column="addr"/>  
  14.     </class>  
  15.        
  16. </hibernate-mapping>  

这样就可以简单的实现一对多的关联关系的映射。关于set的设置还有很多属性,可以自己参考查阅。

第二种方式:通过中间表关联关系双方,把存在关系双方的id存储在中间表中;

看一下sql语句:

sql 代码
  1. create table person(id bigint not null auto_increment primary key,name varchar(20) not null);   
  2. create table personaddress(personId bigint not null, addressId bigint not nullprimary key (personId, addressId) );   
  3. create table address(id bigint not null auto_increment primary key,addr varchar(20) not null);   

可以看到共建立了三张表,person、personaddress、address;其中personaddress表中存储的是关联双方的id;

下面看一下映射文件:

Person类:

xml 代码
  1. <?xml version="1.0"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC    
  3.     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"   
  4.     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  5. <hibernate-mapping package="org.test.bean">  
  6.   
  7.     <class name="Person" table="person">  
  8.         <id name="id" column="id">  
  9.             <generator class="native"/>  
  10.         </id>  
  11.         <property name="name" type="string" column="name"/>  
  12.         <set name="addresses" table="personaddress" lazy="false">  
  13.             <key column="personId"/>  
  14.             <many-to-many column="addressId"  
  15.                 class="Address"/>  
  16.         </set>  
  17.     </class>  
  18.        
  19. </hibernate-mapping>  

Address类:

xml 代码
  1. <?xml version="1.0"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC    
  3.     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"   
  4.     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  5. <hibernate-mapping    
  6.     package="org.test.bean">  
  7.   
  8.     <class name="Address" table="address">  
  9.         <id name="id" column="id">  
  10.             <generator class="native"/>  
  11.         </id>  
  12.         <property name="addr" type="string" column="addr"/>  
  13.     </class>  
  14.        
  15. </hibernate-mapping>  

这样就可以实现一对多关系的映射。

大家看了也许就会明白,废话不说了。

另外看一下hibernate配置文件,把上面的映射文件全部导入hibernate文件中就可以享受hibernate之旅了:

 

xml 代码
  1. <!DOCTYPE hibernate-configuration PUBLIC   
  2.     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"   
  3.     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  4.   
  5. <hibernate-configuration>  
  6.     <session-factory name="foo">  
  7.   
  8.         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
  9.         <property name="connection.url">jdbc:mysql://localhost:3306/relation</property>  
  10.         <property name="connection.username">root</property>  
  11.         <property name="connection.password">mysql</property>          
  12.            
  13.         <property name="show_sql">true</property>  
  14.            
  15.         <mapping resource="org/test/bean/Person.hbm.xml"/>  
  16.         <mapping resource="org/test/bean/Address.hbm.xml"/>  
  17.         <mapping resource="org/test/bean/People.hbm.xml"/>  
  18.         <mapping resource="org/test/bean/Location.hbm.xml"/>  
  19.                        
  20.     </session-factory>  
  21.        
  22. </hibernate-configuration>  

 

看一下简单的SessionFactory工具类:

java 代码
  1. package org.test.hibernate;   
  2.   
  3. import org.hibernate.SessionFactory;   
  4. import org.hibernate.cfg.Configuration;   
  5.   
  6. public class HibernateUtil {   
  7.   
  8.     public HibernateUtil() {   
  9.         // TODO Auto-generated constructor stub   
  10.     }   
  11.   
  12.     private static final SessionFactory sessionFactory;   
  13.   
  14.     static {   
  15.         try {   
  16.             // Create the SessionFactory from hibernate.cfg.xml   
  17.             sessionFactory = new Configuration().configure().buildSessionFactory();   
  18.         } catch (Throwable ex) {   
  19.             // Make sure you log the exception, as it might be swallowed   
  20.             System.err.println("Initial SessionFactory creation failed." + ex);   
  21.             throw new ExceptionInInitializerError(ex);   
  22.         }   
  23.     }   
  24.   
  25.     public static SessionFactory getSessionFactory() {   
  26.         return sessionFactory;   
  27.     }   
  28.   
  29. }   
  • 描述: 表结构
  • 大小: 52.4 KB
  • 描述: 表结构
  • 大小: 59.5 KB
分享到:
评论

相关推荐

    hibernate one-to-one 一对一唯一外键关联映射_单向 and 双向

    Hibernate 一对一唯一外键关联...Hibernate的一对一唯一外键关联映射是一种强大的关联方式,能够描述两个实体之间的唯一关联关系。在实际应用中,需要根据实际情况选择合适的关联方式,以确保数据的正确性和一致性。

    Hibernate_Annotation关联映射

    通过在被拥有的实体端(owned entity)增加一个外键列来实现一对多单向关联是很少见的,也是不推荐的,建议通过一个联接表来实现这种关联(下面会讲到)。 @JoinColoumn批注来描述这种单向关联关系 @Entity Public class...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...

    Hibernate+中文文档

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...

    HibernateAPI中文版.chm

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...

    hibernate3.2中文文档(chm格式)

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...

    Hibernate中文详细学习文档

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...

    Hibernate 中文 html 帮助文档

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,...

    Hibernate注释大全收藏

    这种策略支持双向的一对多关联,但不支持 IDENTIFY 生成器策略,因为ID必须在多个表间共享。一旦使用就不能使用AUTO和IDENTIFY生成器。 每个类层次结构一张表 @Entity @Inheritance(strategy=InheritanceType....

    Hibernate教程

    22.2. 双向的一对多关系(Bidirectional one-to-many) 22.3. 级联生命周期(Cascading lifecycle) 22.4. 级联与未保存值(Cascades and unsaved-value) 22.5. 结论 23. 示例:Weblog 应用程序 23.1. 持久化类 ...

    hibernate 体系结构与配置 参考文档(html)

    一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及...

    最全Hibernate 参考文档

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 三重关联...

    精通hibernate:对象持久化技术孙卫琴第二版part2

    本章介绍一对多关联关系的映射方法,重点介绍inverse属性和cascade属性的用法。本章还将介绍通过Hibernate API来保存、修改和删除具有关联关系的对象的方法。 7.1 建立多对一的单向关联关系 148 7.1.1 [many-to-...

    Hibernate3的帮助文档

    7.2.5. 一对多关联(One-to-many Associations) 7.3. 高级集合映射(Advanced collection mappings) 7.3.1. 有序集合(Sorted collections) 7.3.2. 双向关联(Bidirectional associations) 7.3.3. 三重关联...

    hibernate3.04中文文档.chm

    22.2. 双向的一对多关系(Bidirectional one-to-many) 22.3. 级联生命周期(Cascading lifecycle) 22.4. 级联与未保存值(Cascades and unsaved-value) 22.5. 结论 23. 示例:Weblog 应用程序 23.1. 持久化类 ...

    hibernate 框架详解

    一对多关联(One-to-many Associations) 7.3. 高级集合映射(Advanced collection mappings) 7.3.1. 有序集合(Sorted collections) 7.3.2. 双向关联(Bidirectional associations) 7.3.3. 三重关联...

    Hibernate3+中文参考文档

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 三重关联...

    Hibernate参考文档

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,...

    精通Hibernate:对象持久化技术第二版part3

    本章介绍一对多关联关系的映射方法,重点介绍inverse属性和cascade属性的用法。本章还将介绍通过Hibernate API来保存、修改和删除具有关联关系的对象的方法。 7.1 建立多对一的单向关联关系 148 7.1.1 [many-to-...

    Hibernate使用技巧汇总

    property-ref:关联类中用于与主控类相关联的属性名,默认为关联类的主键属性名 单向一对多需在一方配置,双向一对多需在双方进行配置 8.lazy=false:被动方的记录由hibernate负责记取,之后存放在主控...

Global site tag (gtag.js) - Google Analytics