4. Bidirectional associations
<o:p> </o:p>
4.1. one to many / many to one
A bidirectional many-to-one association is the most common kind of association. (This is the standard parent/child relationship.)
xml 代码
- <class name="Person">
- <id name="id" column="personId">
- <generator class="native"/>
- </id>
- <many-to-one name="address"
- column="addressId"
- not-null="true"/>
- </class>
- <class name="Address">
- <id name="id" column="addressId">
- <generator class="native"/>
- </id>
- <set name="people" inverse="true">
- <key column="addressId"/>
- <one-to-many class="Person"/>
- </set>
- </class>
sql 代码
- create table Person ( personId bigint not null primary key, addressId bigint not null )
- create table Address ( addressId bigint not null primary key )
If you use a List (or other indexed collection) you need to set the key column of the foreign key to not null,and let Hibernate manage the association from the collections side to maintain the index of each element (making the other side virtually inverse by setting update="false"and insert="false"):
xml 代码
- <class name="Person">
- <id name="id"/>
- ...
- <many-to-one name="address"
- column="addressId"
- not-null="true"
- insert="false"
- update="false"/>
- </class>
- <class name="Address">
- <id name="id"/>
- ...
- <list name="people">
- <key column="addressId" not-null="true"/>
- <list-index column="peopleIdx"/>
- <one-to-many class="Person"/>
- </list>
- </class>
It is important that you define not-null="true"on the <key>element of the collection mapping if the underlying foreign key column is NOT NULL. Don't only declare not-null="true" on a possible nested <column> element, but on the <key>element.
4.2. one to one
A bidirectional one-to-one association on a foreign key is quite common.
xml 代码
- <class name="Person">
- <id name="id" column="personId">
- <generator class="native"/>
- </id>
- <many-to-one name="address"
- column="addressId"
- unique="true"
- not-null="true"/>
- </class>
- <class name="Address">
- <id name="id" column="addressId">
- <generator class="native"/>
- </id>
- <one-to-one name="person" property-ref="address"/>
- </class>
sql 代码
- create table Person ( personId bigint not null primary key, addressId bigint not null unique )
- create table Address ( addressId bigint not null primary key )
A bidirectional one-to-one association on a primary key uses the special id generator.
xml 代码
- <class name="Person">
- <id name="id" column="personId">
- <generator class="native"/>
- </id>
- <one-to-one name="address"/>
- </class>
- <class name="Address">
- <id name="id" column="personId">
- <generator class="foreign">
- <param name="property">person</param>
- </generator>
- </id>
- <one-to-one name="person" constrained="true"/>
- </class>
sql 代码
- create table Person ( personId bigint not null primary key )
- create table Address ( personId bigint not null primary key )
5. Bidirectional associations with join tables
5.1. one to many / many to one
A bidirectional one-to-many association on a join table. Note that the inverse="true" can go on either end of the association, on the collection, or on the join.
xml 代码
- <class name="Person">
- <id name="id" column="personId">
- <generator class="native"/>
- </id>
- <set name="addresses" table="PersonAddress">
- <key column="personId"/>
- <many-to-many column="addressId"
- unique="true"
- class="Address"/>
- </set>
- </class>
- <class name="Address">
- <id name="id" column="addressId">
- <generator class="native"/>
- </id>
- <join table="PersonAddress"
- inverse="true"
- optional="true">
- <key column="addressId"/>
- <many-to-one name="person"
- column="personId"
- not-null="true"/>
- </join>
- </class>
sql 代码
- create table Person ( personId bigint not null primary key )
- create table PersonAddress ( personId bigint not null, addressId bigint not null primary key )
- create table Address ( addressId bigint not null primary key )
5.2. one to one
A bidirectional one-to-one association on a join table is extremely unusual, but possible.
xml 代码
- <class name="Person">
- <id name="id" column="personId">
- <generator class="native"/>
- </id>
- <join table="PersonAddress"
- optional="true">
- <key column="personId" unique="true"/>
- <many-to-one name="address"
- column="addressId"
- not-null="true"
- unique="true"/>
- </join>
- </class>
- <class name="Address">
- <id name="id" column="addressId">
- <generator class="native"/>
- </id>
- <join table="PersonAddress"
- optional="true"
- inverse="true">
- <key column="addressId" unique="true"/>
- <many-to-one name="person"
- column="personId"
- not-null="true"
- unique="true"/>
- </join>
- </class>
sql 代码
- create table Person ( personId bigint not null primary key )
- create table PersonAddress ( personId bigint not null primary key, addressId bigint not null unique )
- create table Address ( addressId bigint not null primary key )
5.3. many to many
Finally, we have a bidirectional many-to-many association.
xml 代码
- <class name="Person">
- <id name="id" column="personId">
- <generator class="native"/>
- </id>
- <set name="addresses" table="PersonAddress">
- <key column="personId"/>
- <many-to-many column="addressId"
- class="Address"/>
- </set>
- </class>
- <class name="Address">
- <id name="id" column="addressId">
- <generator class="native"/>
- </id>
- <set name="people" inverse="true" table="PersonAddress">
- <key column="addressId"/>
- <many-to-many column="personId"
- class="Person"/>
- </set>
- </class>
sql 代码
- create table Person ( personId bigint not null primary key )
- create table PersonAddress ( personId bigint not null, addressId bigint not null, primary key (personId, addressId) )
- create table Address ( addressId bigint not null primary key )
6. More complex association mappings
More complex association joins are extremely rare. Hibernate makes it possible to handle more complex situations using SQL fragments embedded in the mapping document. For example, if a table with historical account information data defines accountNumber, effectiveEndDate and effectiveStartDatecolumns, mapped as follows:
xml 代码
- <properties name="currentAccountKey">
- <property name="accountNumber" type="string" not-null="true"/>
- <property name="currentAccount" type="boolean">
- <formula>case when effectiveEndDate is null then 1 else 0 end</formula>
- </property>
- </properties>
- <property name="effectiveEndDate" type="date"/>
- <property name="effectiveStateDate" type="date" not-null="true"/>
Then we can map an association to the current instance (the one with null effectiveEndDate) using:
xml 代码
- <many-to-one name="currentAccountInfo"
- property-ref="currentAccountKey"
- class="AccountInfo">
- <column name="accountNumber"/>
- <formula>'1'</formula>
- </many-to-one>
In a more complex example, imagine that the association between Employeeand Organizationis maintained in an Employment table full of historical employment data. Then an association to the employee's most recent employer (the one with the most recent startDate) might be mapped this way:
xml 代码
- <join>
- <key column="employeeId"/>
- <subselect>
- Select employeeId, orgId
- from Employments
- group by orgId
- having startDate = max(startDate)
- </subselect>
- <many-to-one name="mostRecentEmployer"
- class="Organization"
- column="orgId"/>
- </join>
You can get quite creative with this functionality, but it is usually more practical to handle these kinds of cases using HQL or a criteria query.
分享到:
相关推荐
Hibernate实体关联关系映射--学习总结.pdf
Hibernate4(关系映射-事务-原理-性能和二级缓存-最佳实践).Hibernate4(关系映射-事务-原理-性能和二级缓存-最佳实践).
Hibernate 实体关联关系映射 学习总结 把一对一 一对多 单向 双向 主键 外键 链接表等讲的比较清楚
Hibernate 关联关系映射分类
hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,包含4个说明文档,分别详细解说了hibernate关联映射的关联关系,...
Hibernate关联关系映射 单向关联 │ ├─ 一对一外键单向关联 │ ├─ 一对一主键单向关联 │ ├─ 一对一连接表单向关联 │ ├─ 一对多外键单向关联 │ ├─ 一对多连接表单向关联 │ ├─ 多对一外键单向关联 │ ...
hibernate里关联关系映射,各种关系解析,帮助大家搞清楚hibernate的关系映射
Hibernate4中映射关系图解。
Hibernate实体关联关系映射--学习总结,让同仁们更好的学习Hiebernate
Hibernate 一对多关联映射的测试,主要使用部门与员工的级联关系进行添加测试.
hibernate 对象关系映射总结hibernate 对象关系映射总结hibernate 对象关系映射总结
Hibernate关联关系映射.CHM Hibernate文档相关
多对多关联映射---教师与学生表的级联增删改查小例子测试,代码简洁易懂,有SQL文件和使用说明.
hibernate的关联关系映射,所有描述以选课系统这个案例来说明
Hibernate关联关系映射实例速查,帮助初学者学习。
Hibernate对象关系映射一对多 很基础等文档
赠送jar包:hibernate-jpa-2.1-api-1.0.2.Final.jar; 赠送原API文档:hibernate-jpa-2.1-api-1.0.2.Final-javadoc.jar; 赠送源代码:hibernate-jpa-2.1-api-1.0.2.Final-sources.jar; 赠送Maven依赖信息文件:...
Hibernate_关联关系映射配置详解,希望能帮助广大java爱好者
Hibernate_关联关系映射配置