hibernate对应关系详解(转)

Posted 花开树梢

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hibernate对应关系详解(转)相关的知识,希望对你有一定的参考价值。

一:Hibernate 一对一连接表双向关联
 
1:模型介绍
 
一个人(Person)对应一个地址(Address)。
 
2:实体(省略gettersetter方法)
 
public class Person11tab_sx {
    private int personid;
    private String name;
    private int age;
    private Address11tab_sx address11tab_sx; 
 
public class Address11tab_sx {
    private int addressid;
    private String addressdetail;
    private Person11tab_sx person11tab_sx;
 
3:表模型 
 
person_11tab_sx;
+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| personid | int(11)      | NO   | PRI | NULL    | auto_increment |
| name     | varchar(255) | YES  |     | NULL    |                |
| age      | int(11)      | YES  |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+
 
join_11tab_sx;
+-----------+---------+------+-----+---------+-------+
| Field     | Type    | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| addressid | int(11) | NO   | UNI |         |       |
| personid  | int(11) | NO   | PRI |         |       |
+-----------+---------+------+-----+---------+-------+ 
 
 
address_11tab_sx;
+---------------+--------------+------+-----+---------+----------------+
| Field         | Type         | Null | Key | Default | Extra          |
+---------------+--------------+------+-----+---------+----------------+
| addressid     | int(11)      | NO   | PRI | NULL    | auto_increment |
| addressdetail | varchar(255) | YES  |     | NULL    |                |
+---------------+--------------+------+-----+---------+----------------+ 
    
  
 
4:映射方法
 
<hibernate-mapping>
    <class name="com.lavasoft.sx._1_1_tab.Person11tab_sx" table="PERSON_11tab_sx">
        <id name="personid">
            <generator class="identity"/>
        </id>
        <property name="name"/>
        <property name="age"/>
        <join table="join_11tab_sx"
              optional="true">
            <key column="personid"
                 unique="true"/>
            <many-to-one name="address11tab_sx"
                         column="addressid"
                         not-null="true"
                         unique="true"/>
        </join>
    </class>
</hibernate-mapping>
 
<hibernate-mapping>
    <class name="com.lavasoft.sx._1_1_tab.Address11tab_sx" table="ADDRESS_11tab_sx">
        <id name="addressid">
            <generator class="identity"/>
        </id>
        <property name="addressdetail"/>
        <join table="join_11tab_sx"
              optional="true"
              inverse="true">
            <key column="addressid"
                 unique="true"/>
            <many-to-one name="person11tab_sx" column="personid"
                         not-null="true" unique="true"/>
        </join>
    </class>
</hibernate-mapping> 
 
5:测试方法
 
public class Test_11tab_sx {
    public static void main(String[] args){
        Address11tab_sx add = new Address11tab_sx();
        Person11tab_sx p = new Person11tab_sx();
 
        add.setAddressdetail("。。。。。");
        p.setAge(12);
        p.setName("。。。。。");
 
        add.setPerson11tab_sx(p);
        p.setAddress11tab_sx(add);
 
        Session session = HibernateUtil.getCurrentSession();
        Transaction tx = session.beginTransaction();
        session.saveOrUpdate(p);
        session.saveOrUpdate(add);
        tx.commit();
        HibernateUtil.closeSession();
    }

 
6:测试结果
 
1) :正常保存.
        session.saveOrUpdate(p);
        session.saveOrUpdate(add);
 
Hibernate: insert into PERSON_11tab_sx (name, age) values (?, ?)
Hibernate: insert into ADDRESS_11tab_sx (addressdetail) values (?)
Hibernate: insert into join_11tab_sx (addressid, personid) values (?, ?)

 

 

 

 

二:Hibernate 一对一主键双向关联
 
1:模型介绍
 
一个人(Person)对应一个地址(Address)。
 
2:实体(省略gettersetter方法)
 
public class Person11pk_sx {
    private int personid;
    private String name;
    private int age;
    private Address11pk_sx address11pk_sx;
 
public class Address11pk_sx {
    private int addressid;
    private String addressdetail;
    private Person11pk_sx person11pk_sx;
 
3:表模型 
 
person_11pk_sx;
+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| personid | int(11)      | NO   | PRI | NULL    | auto_increment |
| name     | varchar(255) | YES  |     | NULL    |                |
| age      | int(11)      | YES  |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+ 
 
address_11pk_sx;
+---------------+--------------+------+-----+---------+-------+
| Field         | Type         | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+-------+
| addressid     | int(11)      | NO   | PRI |         |       |
| addressdetail | varchar(255) | YES  |     | NULL    |       |
+---------------+--------------+------+-----+---------+-------+ 
  
 
4:映射方法
 
<hibernate-mapping>
    <class name="com.lavasoft.sx._1_1_pk.Person11pk_sx" table="PERSON_11pk_sx">
        <id name="personid">
            <generator class="identity"/>
        </id>
        <property name="name"/>
        <property name="age"/>
        <one-to-one name="address11pk_sx" cascade="all"/>
    </class>
</hibernate-mapping>
 
<hibernate-mapping>
    <class name="com.lavasoft.sx._1_1_pk.Address11pk_sx" table="ADDRESS_11pk_sx">
        <id name="addressid">
            <generator class="foreign">
                <param name="property">person11pk_sx</param>
            </generator>
        </id>
        <property name="addressdetail"/>
        <!--表明主键由关联属性生成-->
        <one-to-one name="person11pk_sx" cascade="all" constrained="true"/>
    </class>
</hibernate-mapping> 
 
5:测试方法
 
public class Test_11pk_sx {
    public static void main(String[] args){
        Address11pk_sx add = new Address11pk_sx();
        Person11pk_sx p = new Person11pk_sx();
 
        add.setAddressdetail("。。。。。");
        p.setAge(12);
        p.setName("。。。。。");
 
        add.setPerson11pk_sx(p);
        p.setAddress11pk_sx(add);
 
        Session session = HibernateUtil.getCurrentSession();
        Transaction tx = session.beginTransaction();
        session.save(p);
//        session.save(add);
        tx.commit();
        HibernateUtil.closeSession();
    }

 
6:测试结果
 
1) :正常保存.
        session.save(p);
//        session.save(add);
 
Hibernate: insert into PERSON_11pk_sx (name, age) values (?, ?)
Hibernate: insert into ADDRESS_11pk_sx (addressdetail, addressid) values (?, ?)
 


三:Hibernate 一对一外键双向关联  
   
1:模型介绍  
   
一个人(Person)对应一个地址(Address)。  
   
2:实体(省略gettersetter方法)  
   
public class Person11fk_sx {  
    private int personid;  
    private String name;  
    private int age;  
    private Address11fk_sx address11fk_sx;  
   
public class Address11fk_sx {  
    private int addressid;  
    private String addressdetail;  
    private Person11fk_sx person11fk_sx;  
   
3:表模型   
   
person_11fk_sx;  
+----------+--------------+------+-----+---------+----------------+  
| Field    | Type         | Null | Key | Default | Extra          |  
+----------+--------------+------+-----+---------+----------------+  
| personid | int(11)      | NO   | PRI | NULL    | auto_increment |  
| name     | varchar(255) | YES  |     | NULL    |                |  
| age      | int(11)      | YES  |     | NULL    |                |  
+----------+--------------+------+-----+---------+----------------+  
address_11fk_sx;  
+---------------+--------------+------+-----+---------+----------------+  
| Field         | Type         | Null | Key | Default | Extra          |  
+---------------+--------------+------+-----+---------+----------------+  
| addressid     | int(11)      | NO   | PRI | NULL    | auto_increment |  
| addressdetail | varchar(255) | YES  |     | NULL    |                |  
| person11fk_sx | int(11)      | YES  | UNI | NULL    |                |   
+---------------+--------------+------+-----+---------+----------------+   
    
   
4:映射方法  
   
<hibernate-mapping>  
    <class name="com.lavasoft.sx._1_1_fk.Person11fk_sx" table="PERSON_11fk_sx">  
        <id name="personid">  
            <generator class="identity"/>  
        </id>  
        <property name="name"/>  
        <property name="age"/>  
        <one-to-one name="address11fk_sx"  
                    cascade="all"  
                    property-ref="person11fk_sx"/>  
    </class>  
</hibernate-mapping>  
   
<hibernate-mapping>  
    <class name="com.lavasoft.sx._1_1_fk.Address11fk_sx" table="ADDRESS_11fk_sx">  
        <id name="addressid">  
            <generator class="identity"/>  
        </id>  
        <property name="addressdetail"/>  
        <!--unique="true"表示本实体是"1"-->  
        <many-to-one name="person11fk_sx" unique="true"/>  
    </class>  
</hibernate-mapping>   
   
5:测试方法  
   
public class Test_11fk_sx {  
    public static void main(String[] args) {  
        Address11fk_sx add = new Address11fk_sx();  
        Person11fk_sx p = new Person11fk_sx();  
   
        add.setAddressdetail("。。。。。");  
        p.setAge(12);  
        p.setName("。。。。。");  
   
        add.setPerson11fk_sx(p);  
        p.setAddress11fk_sx(add);  
   
        Session session = HibernateUtil.getCurrentSession();  
        Transaction tx = session.beginTransaction();  
        session.save(p);  
        tx.commit();  
        HibernateUtil.closeSession();  
    }  
}   
   
6:测试结果  
   
1) :正常保存.  
        session.save(p);  
   
Hibernate: insert into PERSON_11fk_sx (name, age) values (?, ?)  
Hibernate: insert into ADDRESS_11fk_sx (addressdetail, person11fk_sx) values (?, ?)  

 

 

 

四:Hibernate 多对多单向关联
 
1:模型介绍
 
多个人(Person)对应多个地址(Address)。
一个人可对应多个地址,一个地址也可以对应多个人。 
2:实体(省略gettersetter方法)
 
public class Personnn {
    private int personid;
    private String name;
    private int age;
    private Set addresses=new HashSet();
 
public class Addressnn {
    private int addressid;
    private String addressdetail;
 
3:表模型 
 
person_nn;
+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| personid | int(11)      | NO   | PRI | NULL    | auto_increment |
| name     | varchar(255) | YES  |     | NULL    |                |
| age      | int(11)      | YES  |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+ 
 
join_nn;
+-----------+---------+------+-----+---------+-------+
| Field     | Type    | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| personid  | int(11) | NO   | PRI |         |       |
| addressid | int(11) | NO   | PRI |         |       |
+-----------+---------+------+-----+---------+-------+ 
 
person_nn;
+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| personid | int(11)      | NO   | PRI | NULL    | auto_increment |
| name     | varchar(255) | YES  |     | NULL    |                |
| age      | int(11)      | YES  |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+ 
  
 
4:映射方法
 
<hibernate-mapping>
    <class name="com.lavasoft.dx._n_n.Personnn" table="PERSON_nn">
        <id name="personid">
            <generator class="identity"/>
        </id>
        <property name="name"/>
        <property name="age"/>
        <!--映射集合属性,join_1ntab是连接表表名-->
        <set name="addresses" table="join_nn" >
            <!--“column="personid"”确定PERSON_1ntab表关联到连接表的外键列名-->
            <key column="personid"/>
            <!--“column="addressid"”关联PERSON_1ntab表的Address1ntab对象的id在连接表中的列名-->
            <many-to-many
                    column="addressid"
                    class="com.lavasoft.dx._n_n.Addressnn"/>
        </set>
    </class>
</hibernate-mapping>
 
<hibernate-mapping>
    <class name="com.lavasoft.dx._n_n.Addressnn" table="ADDRESS_nn">
        <id name="addressid">
            <generator class="identity"/>
        </id>
        <property name="addressdetail"/>
    </class>
</hibernate-mapping> 
  
 
5:测试方法
 
public class Test_nn {
    public static void main(String[] args){
        Addressnn add1=new Addressnn();
        Addressnn add2=new Addressnn();
        Addressnn add3=new Addressnn();
        Personnn p1=new Personnn();
        Personnn p2=new Personnn();
 
        add1.setAddressdetail("。。。。。");
        add2.setAddressdetail("。。。。。");
        add3.setAddressdetail("。。。。。");
        p1.setName("。。。。。");
        p1.setAge(30);
        p2.setName("。。。。。");
        p2.setAge(50);
 
        p1.getAddresses().add(add1);
        p1.getAddresses().add(add2);
        p2.getAddresses().add(add2);
        p2.getAddresses().add(add3);
 
        Session session= HibernateUtil.getCurrentSession();
        Transaction tx=session.beginTransaction();
        session.save(add1);
        session.save(add2);
        session.save(add3);
        session.save(p1);
        session.save(p2);
        tx.commit();
        HibernateUtil.closeSession();
    }

 
6:测试结果
 
1) :正常保存.
        session.save(add1);
        session.save(add2);
        session.save(add3);
        session.save(p1);
        session.save(p2);
 
Hibernate: insert into ADDRESS_nn (addressdetail) values (?)
Hibernate: insert into ADDRESS_nn (addressdetail) values (?)
Hibernate: insert into ADDRESS_nn (addressdetail) values (?)
Hibernate: insert into PERSON_nn (name, age) values (?, ?)
Hibernate: insert into PERSON_nn (name, age) values (?, ?)
Hibernate: insert into join_nn (personid, addressid) values (?, ?)
Hibernate: insert into join_nn (personid, addressid) values (?, ?)
Hibernate: insert into join_nn (personid, addressid) values (?, ?)
Hibernate: insert into join_nn (personid, addressid) values (?, ?)

 

 

 

 


五:Hibernate 多对一连接表单向关联
 
1:模型介绍
 
多个人(Person)对应一个地址(Address)。
 
2:实体(省略gettersetter方法)
 
public class Personn1tab {
    private int personid;
    private String name;
    private int age;
    private Addressn1tab addressn1tab;
 
public class Addressn1tab {
    private int addressid;
    private String addressdetail;
 
3:表模型 
 
address_n1tab;
+---------------+--------------+------+-----+---------+----------------+
| Field         | Type         | Null | Key | Default | Extra          |
+---------------+--------------+------+-----+---------+----------------+
| addressid     | int(11)      | NO   | PRI | NULL    | auto_increment |
| addressdetail | varchar(255) | YES  |     | NULL    |                |
+---------------+--------------+------+-----+---------+----------------+ 
 
join_n1tab;
+------------+---------+------+-----+---------+-------+
| Field      | Type    | Null | Key | Default | Extra |
+------------+---------+------+-----+---------+-------+
| personid   | int(11) | NO   | PRI |         |       |
| addressn1tab | int(11) | YES  | MUL | NULL    |       |
+------------+---------+------+-----+---------+-------+ 
 
person_n1tab;
+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| personid | int(11)      | NO   | PRI | NULL    | auto_increment |
| name     | varchar(255) | YES  |     | NULL    |                |
| age      | int(11)      | YES  |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+ 
  
  
 
4:映射方法
 
<hibernate-mapping>
    <class name="com.lavasoft.dx._n_1_tab.Personn1tab" table="PERSON_n1tab">
        <id name="personid">
            <generator class="identity"/>
        </id>
        <property name="name"/>
        <property name="age"/>
        <!--使用join元素显式确定链接表-->
        <join table="join_n1tab">
            <!--映射关联所用的外键-->
            <key column="personid"/>
            <many-to-one name="addressn1tab"/>
        </join> 
    </class>
</hibernate-mapping>
 
<hibernate-mapping>
    <class name="com.lavasoft.dx._n_1_tab.Addressn1tab" table="ADDRESS_n1tab">
        <id name="addressid">
            <generator class="identity"/>
        </id>
        <property name="addressdetail"/>
    </class>
</hibernate-mapping> 
  
 
5:测试方法
 
public class Test_n1tab {
    public static void main(String[] args){
        Personn1tab p1=new Personn1tab();
        Personn1tab p2=new Personn1tab();
 
        p1.setAge(21);
        p1.setName("p1");
 
        p2.setAge(23);
        p2.setName("p2");
 
        Addressn1tab add=new Addressn1tab();
        add.setAddressdetail("。。。。。");
 
        p1.setAddressn1tab(add);
        p2.setAddressn1tab(add);
 
        Session session=HibernateUtil.getCurrentSession();
        Transaction tx=session.beginTransaction();
        session.save(add);
        session.save(p1);
        session.save(p2);
        tx.commit();
        HibernateUtil.closeSession();
    }

 
6:测试结果
 
1) :正常保存. 推荐这么干!
 
        session.save(p1);
        session.save(p2);
 
Hibernate: insert into ADDRESS_n1tab (addressdetail) values (?)
Hibernate: insert into PERSON_n1tab (name, age) values (?, ?)
Hibernate: insert into join_n1tab (address11fk, personid) values (?, ?)
Hibernate: insert into PERSON_n1tab (name, age) values (?, ?)
Hibernate: insert into join_n1tab (address11fk, personid) values (?, ?)
 
2) :正常保存.
        session.save(p1);
        session.save(p2);
        session.save(add);
 
 Hibernate: insert into PERSON_n1tab (name, age) values (?, ?)
 Hibernate: insert into join_n1tab (address11fk, personid) values (?, ?)
 Hibernate: insert into PERSON_n1tab (name, age) values (?, ?)
 Hibernate: insert into join_n1tab (address11fk, personid) values (?, ?)

 

 

 

 

 

 

 

六:Hibernate 多对一外键单向关联
 
1:模型介绍
 
多个人(Person)对应一个地址(Address)。
 
2:实体(省略gettersetter方法)
 
public class Personn1fk {
    private int personid;
    private String name;
    private int age;
    private Addressn1fk addressn1fk;
 
public class Addressn1fk {
    private int addressid;
    private String addressdetail;
 
3:表模型 
 
address_n1kf;
+---------------+--------------+------+-----+---------+----------------+
| Field         | Type         | Null | Key | Default | Extra          |
+---------------+--------------+------+-----+---------+----------------+
| addressid     | int(11)      | NO   | PRI | NULL    | auto_increment |
| addressdetail | varchar(255) | YES  |     | NULL    |                |
+---------------+--------------+------+-----+---------+----------------+ 
 
person_n1kf;
+-----------+--------------+------+-----+---------+----------------+
| Field     | Type         | Null | Key | Default | Extra          |
+-----------+--------------+------+-----+---------+----------------+
| personid  | int(11)      | NO   | PRI | NULL    | auto_increment |
| name      | varchar(255) | YES  |     | NULL    |                |
| age       | int(11)      | YES  |     | NULL    |                |
| addressId | int(11)      | YES  | MUL | NULL    |                | 
+-----------+--------------+------+-----+---------+----------------+ 
    
 
4:映射方法
 
<hibernate-mapping>
    <class name="com.lavasoft.dx._n_1_fk.Personn1fk" table="PERSON_n1fk">
        <id name="personid">
            <generator class="identity"/>
        </id>
        <property name="name"/>
        <property name="age"/>
        <!--用来映射关联PO columnAddress在该表中的外键列名-->
        <many-to-one name="addressn1fk" column="addressId"/>
    </class>
</hibernate-mapping>
 
<hibernate-mapping>
    <class name="com.lavasoft.dx._n_1_fk.Addressn1fk" table="ADDRESS_n1fk">
        <id name="addressid">
            <generator class="identity"/>
        </id>
        <property name="addressdetail"/>
    </class>
</hibernate-mapping> 
  
 
5:测试方法
 
public class Test_n1fk {
    public static void main(String[] args){
        Personn1fk p1=new Personn1fk();
        Personn1fk p2=new Personn1fk();
 
        p1.setAge(21);
        p1.setName("p1");
 
        p2.setAge(23);
        p2.setName("p2");
 
        Addressn1fk add=new Addressn1fk();
        add.setAddressdetail("。。。。。");
 
        p1.setAddressn1fk(add);
        p2.setAddressn1fk(add); 
 
        Session session=HibernateUtil.getCurrentSession();
        Transaction tx=session.beginTransaction();
        session.save(add);
        session.save(p1);
        session.save(p2); 
        tx.commit();
        HibernateUtil.closeSession();
    }

 
6:测试结果
 
1) :正常保存. 推荐这么干!
 
        session.save(p1);
        session.save(p2);
 
Hibernate: insert into ADDRESS_n1kf (addressdetail) values (?)
Hibernate: insert into PERSON_n1kf (name, age, addressId) values (?, ?, ?)
Hibernate: insert into PERSON_n1kf (name, age, addressId) values (?, ?, ?)
 
2) :正常保存.
        session.save(p1);
        session.save(p2);
        session.save(add);
 
Hibernate: insert into PERSON_n1kf (name, age, addressId) values (?, ?, ?)
Hibernate: insert into PERSON_n1kf (name, age, addressId) values (?, ?, ?)
Hibernate: insert into ADDRESS_n1kf (addressdetail) values (?)
Hibernate: update PERSON_n1kf set name=?, age=?, addressId=? where personid=?
Hibernate: update PERSON_n1kf set name=?, age=?, addressId=? where personid=?
 
3) :正常保存.
        session.save(add);
//        session.save(p1);
//        session.save(p2);
 
Hibernate: insert into ADDRESS_n1kf (addressdetail) values (?)
 
4) : 发生异常,不能保存.
//        session.save(add);
        session.save(p1);
        session.save(p2);
Hibernate: insert into PERSON_n1kf (name, age, addressId) values (?, ?, ?)
Hibernate: insert into PERSON_n1kf (name, age, addressId) values (?, ?, ?)
Exception in thread "main" org.hibernate.TransientObjectException: com.lavasoft.dx._n_1_

 

 

 

 

 

: Hibernate 一对多连接表单向关联
 
 
1:模型介绍
 
一个人(Person)对应多个地址(Address),比如家庭地址、公司地址。
 
2:实体(省略gettersetter方法)
 
public class Person1ntab {
    private int personid;
    private String name;
    private int age;
    private Set addresses=new HashSet();
 
public class Address1nfk implements Serializable {
    private int addressid;
    private String addressdetail;
 
3:表模型 
 
join_1ntab;
+-----------+---------+------+-----+---------+-------+
| Field     | Type    | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| personid  | int(11) | NO   | PRI |         |       |
| addressid | int(11) | NO   | PRI |         |       |
+-----------+---------+------+-----+---------+-------+ 
 
person_1ntab;
+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| personid | int(11)      | NO   | PRI | NULL    | auto_increment |
| name     | varchar(255) | YES  |     | NULL    |                |
| age      | int(11)      | YES  |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+ 
 
address_1ntab;
+---------------+--------------+------+-----+---------+----------------+
| Field         | Type         | Null | Key | Default | Extra          |
+---------------+--------------+------+-----+---------+----------------+
| addressid     | int(11)      | NO   | PRI | NULL    | auto_increment |
| addressdetail | varchar(255) | YES  |     | NULL    |                |
+---------------+--------------+------+-----+---------+----------------+ 
  
 
4:映射方法
 
<hibernate-mapping>
    <class name="com.lavasoft.dx._1_n_tab.Person1ntab" table="PERSON_1ntab">
        <id name="personid">
            <generator class="identity"/>
        </id>
        <property name="name"/>
        <property name="age"/>
        <!--映射集合属性,join_1ntab是连接表表名-->
        <set name="addresses"
             table="join_1ntab"
                >
            <!--“column="personid"”确定PERSON_1ntab表关联到连接表的外键列名-->
            <key column="personid"/>
            <!--“column="addressid"”关联PERSON_1ntab表的Address1ntab对象的id在连接表中的列名-->
            <!--“unique="true"表示1-NPerson1ntab1Address1ntab是多”-->
            <many-to-many
                    column="addressid"
                    unique="true"
                    class="com.lavasoft.dx._1_n_tab.Address1ntab"/>
        </set> 
    </class>
</hibernate-mapping>
 
<hibernate-mapping>
    <class name="com.lavasoft.dx._1_n_tab.Address1ntab" table="ADDRESS_1ntab">
        <id name="addressid">
            <generator class="identity"/>
        </id>
        <property name="addressdetail"/>
    </class>
</hibernate-mapping>
 
 
6:测试方法
 
public class Test_1ntab {
    public static void main(String[] args){
        Address1ntab add1=new Address1ntab();
        Address1ntab add2=new Address1ntab();
        Address1ntab add3=new Address1ntab();
        Person1ntab p1=new Person1ntab();
        Person1ntab p2=new Person1ntab();
 
        add1.setAddressdetail("。。。。。");
        add2.setAddressdetail("。。。。。");
        add3.setAddressdetail("。。。。。");
        p1.setName("。。。。。");
        p1.setAge(30);
        p2.setName("。。。。。");
        p2.setAge(50);
 
        p1.getAddresses().add(add1);
        p1.getAddresses().add(add2);
        //p2.getAddresses().add(add2);
        p2.getAddresses().add(add3);
 
        Session session= HibernateUtil.getCurrentSession();
        Transaction tx=session.beginTransaction();
        session.save(add1);
        session.save(add2);
        session.save(add3);
        session.save(p1);
        session.save(p2);
        tx.commit();
        HibernateUtil.closeSession();
    }

 
6:测试结果
 
1) :正常保存.
        session.save(add1);
        session.save(add2);
        session.save(add3);
        session.save(p1);
        session.save(p2);


Hibernate: insert into PERSON_1nfk (name, age) values (?, ?)
Hibernate: insert into ADDRESS_1nfk (addressdetail) values (?)
Hibernate: insert into ADDRESS_1nfk (addressdetail) values (?)
Hibernate: update ADDRESS_1nfk set personid=? where addressid=?
Hibernate: update ADDRESS_1nfk set personid=? where addressid=?

 

 


八:Hibernate 一对多外键单向关联
 
    这种情况很少见,但Hibernate同样允许采用连接表关联1-1.有连接表的1-1同样只需要将N-1many-to-one元素增加unique="true"属性即可。
 
1:模型介绍
 
一个人(Person)对应多个地址(Address),比如家庭地址、公司地址。
 
2:实体(省略gettersetter方法)
 
public class Person1nfk implements Serializable {
    private int p

以上是关于hibernate对应关系详解(转)的主要内容,如果未能解决你的问题,请参考以下文章

Hibernate 对象关系映射文件

小白学习之Hibernate 学习总结

( 转)Hibernate常用API

hibernate 表关系映射详解之多对多

(转)Hibernate框架基础——一对多关联关系映射

Hibernate——数据访问层的架构模式<转>