第一种方式:在数据库中不需要中间表,而是直接在多方的表中加以列一方的id;
看一下建表的SQL
sql 代码
- create table people(id bigint not null auto_increment primary key,name varchar(20) not null);
- 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 代码
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="org.test.bean">
-
- <class name="People" table="people">
- <id name="id" column="id">
- <generator class="native"/>
- </id>
- <property name="name" type="string" column="name"/>
- <set name="locations" cascade="save-update" lazy="false">
- <key column="peopleId"/>
- <one-to-many class="Location"/>
- </set>
- </class>
-
- </hibernate-mapping>
Location 类:
xml 代码
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping
- package="org.test.bean">
-
- <class name="Location" table="location">
- <id name="id" column="id">
- <generator class="native"/>
- </id>
- <property name="peopleId" type="integer" column="peopleId"/>
- <property name="addr" type="string" column="addr"/>
- </class>
-
- </hibernate-mapping>
这样就可以简单的实现一对多的关联关系的映射。关于set的设置还有很多属性,可以自己参考查阅。
第二种方式:通过中间表关联关系双方,把存在关系双方的id存储在中间表中;
看一下sql语句:
sql 代码
- create table person(id bigint not null auto_increment primary key,name varchar(20) not null);
- create table personaddress(personId bigint not null, addressId bigint not null, primary key (personId, addressId) );
- create table address(id bigint not null auto_increment primary key,addr varchar(20) not null);
可以看到共建立了三张表,person、personaddress、address;其中personaddress表中存储的是关联双方的id;
下面看一下映射文件:
Person类:
xml 代码
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="org.test.bean">
-
- <class name="Person" table="person">
- <id name="id" column="id">
- <generator class="native"/>
- </id>
- <property name="name" type="string" column="name"/>
- <set name="addresses" table="personaddress" lazy="false">
- <key column="personId"/>
- <many-to-many column="addressId"
- class="Address"/>
- </set>
- </class>
-
- </hibernate-mapping>
Address类:
xml 代码
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping
- package="org.test.bean">
-
- <class name="Address" table="address">
- <id name="id" column="id">
- <generator class="native"/>
- </id>
- <property name="addr" type="string" column="addr"/>
- </class>
-
- </hibernate-mapping>
这样就可以实现一对多关系的映射。
大家看了也许就会明白,废话不说了。
另外看一下hibernate配置文件,把上面的映射文件全部导入hibernate文件中就可以享受hibernate之旅了:
xml 代码
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
-
- <hibernate-configuration>
- <session-factory name="foo">
-
- <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
- <property name="connection.url">jdbc:mysql://localhost:3306/relation</property>
- <property name="connection.username">root</property>
- <property name="connection.password">mysql</property>
-
- <property name="show_sql">true</property>
-
- <mapping resource="org/test/bean/Person.hbm.xml"/>
- <mapping resource="org/test/bean/Address.hbm.xml"/>
- <mapping resource="org/test/bean/People.hbm.xml"/>
- <mapping resource="org/test/bean/Location.hbm.xml"/>
-
- </session-factory>
-
- </hibernate-configuration>
看一下简单的SessionFactory工具类:
java 代码
- package org.test.hibernate;
-
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
-
- public class HibernateUtil {
-
- public HibernateUtil() {
-
- }
-
- private static final SessionFactory sessionFactory;
-
- static {
- try {
-
- sessionFactory = new Configuration().configure().buildSessionFactory();
- } catch (Throwable ex) {
-
- System.err.println("Initial SessionFactory creation failed." + ex);
- throw new ExceptionInInitializerError(ex);
- }
- }
-
- public static SessionFactory getSessionFactory() {
- return sessionFactory;
- }
-
- }
- 描述: 表结构
- 大小: 52.4 KB
- 描述: 表结构
- 大小: 59.5 KB
分享到:
相关推荐
Hibernate 一对一唯一外键关联...Hibernate的一对一唯一外键关联映射是一种强大的关联方式,能够描述两个实体之间的唯一关联关系。在实际应用中,需要根据实际情况选择合适的关联方式,以确保数据的正确性和一致性。
通过在被拥有的实体端(owned entity)增加一个外键列来实现一对多单向关联是很少见的,也是不推荐的,建议通过一个联接表来实现这种关联(下面会讲到)。 @JoinColoumn批注来描述这种单向关联关系 @Entity Public class...
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. 双向关联...
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. 双向关联...
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. 双向关联...
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. 双向关联...
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. 双向关联...
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. 双向关联,...
这种策略支持双向的一对多关联,但不支持 IDENTIFY 生成器策略,因为ID必须在多个表间共享。一旦使用就不能使用AUTO和IDENTIFY生成器。 每个类层次结构一张表 @Entity @Inheritance(strategy=InheritanceType....
22.2. 双向的一对多关系(Bidirectional one-to-many) 22.3. 级联生命周期(Cascading lifecycle) 22.4. 级联与未保存值(Cascades and unsaved-value) 22.5. 结论 23. 示例:Weblog 应用程序 23.1. 持久化类 ...
一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及...
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. 三重关联...
本章介绍一对多关联关系的映射方法,重点介绍inverse属性和cascade属性的用法。本章还将介绍通过Hibernate API来保存、修改和删除具有关联关系的对象的方法。 7.1 建立多对一的单向关联关系 148 7.1.1 [many-to-...
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. 三重关联...
22.2. 双向的一对多关系(Bidirectional one-to-many) 22.3. 级联生命周期(Cascading lifecycle) 22.4. 级联与未保存值(Cascades and unsaved-value) 22.5. 结论 23. 示例:Weblog 应用程序 23.1. 持久化类 ...
一对多关联(One-to-many Associations) 7.3. 高级集合映射(Advanced collection mappings) 7.3.1. 有序集合(Sorted collections) 7.3.2. 双向关联(Bidirectional associations) 7.3.3. 三重关联...
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. 三重关联...
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. 双向关联,...
本章介绍一对多关联关系的映射方法,重点介绍inverse属性和cascade属性的用法。本章还将介绍通过Hibernate API来保存、修改和删除具有关联关系的对象的方法。 7.1 建立多对一的单向关联关系 148 7.1.1 [many-to-...
property-ref:关联类中用于与主控类相关联的属性名,默认为关联类的主键属性名 单向一对多需在一方配置,双向一对多需在双方进行配置 8.lazy=false:被动方的记录由hibernate负责记取,之后存放在主控...