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. }   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值