hibernate中双向一对多关联关系的使用

双向一对多实际上就是一对多和多对一的组合,通过双向一对多可以将关联关系的维护交给被控制对象自己去维护自己的属性,而不用再有主控方来维护关联关系和被控方的属性域,数据库中数据的维护性能提高,sql语句执行效率更高。Inverse=“true”cascade=“call”是主控对象中的两个必须设置的属性,inverse用来将关联关系的维护工作交给被控方自己来处理,不再关心被控方的属性域值,有被控方自己来设置其外键值,cascade是设置级联操作,主控方的任何修改都将引起被控方的修改。

使用实例如下:

Taddress类代码:

package com.inspur.po;

 

/**

 * TAddress entity. @author MyEclipse Persistence Tools

 */

 

public class TAddress implements java.io.Serializable {

 

    // Fields

 

    private Integer id;

    private TUser TUser;

    private String address;

    private String tel;

    private String zipcode;

 

    // Constructors

 

    /** default constructor */

    public TAddress() {

    }

 

    /** full constructor */

    public TAddress(TUser TUser, String address, String tel, String zipcode) {

       this.TUser = TUser;

       this.address = address;

       this.tel = tel;

       this.zipcode = zipcode;

    }

 

    // Property accessors

 

    public Integer getId() {

       return this.id;

    }

 

    public void setId(Integer id) {

       this.id = id;

    }

 

    public TUser getTUser() {

       return this.TUser;

    }

 

    public void setTUser(TUser TUser) {

       this.TUser = TUser;

    }

 

    public String getAddress() {

       return this.address;

    }

 

    public void setAddress(String address) {

       this.address = address;

    }

 

    public String getTel() {

       return this.tel;

    }

 

    public void setTel(String tel) {

       this.tel = tel;

    }

 

    public String getZipcode() {

       return this.zipcode;

    }

 

    public void setZipcode(String zipcode) {

       this.zipcode = zipcode;

    }

 

}

Tuser类代码:

package com.inspur.po;

import java.util.HashSet;

import java.util.Set;

/**

 * TUser entity. @author MyEclipse Persistence Tools

 */

public class TUser implements java.io.Serializable {

       // Fields

       private Integer id;

       private String name;

       private Integer age;

       private Set TAddresses = new HashSet(0);

       // Constructors

       /** default constructor */

       public TUser() {

       }

       /** full constructor */

       public TUser(String name, Integer age, Set TAddresses) {

              this.name = name;

              this.age = age;

              this.TAddresses = TAddresses;

       }

       // Property accessors

       public Integer getId() {

              return this.id;

       }

       public void setId(Integer id) {

              this.id = id;

       }

       public String getName() {

              return this.name;

       }

       public void setName(String name) {

              this.name = name;

       }

       public Integer getAge() {

              return this.age;

       }

       public void setAge(Integer age) {

              this.age = age;

       }

       public Set getTAddresses() {

              return this.TAddresses;

       }

       public void setTAddresses(Set TAddresses) {

              this.TAddresses = TAddresses;

       }

}

Taddress.hbm.xml配置文件如下:

<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!--

    Mapping file autogenerated by MyEclipse Persistence Tools

-->

<hibernate-mapping>

    <class name="com.inspur.po.TAddress" table="t_address" catalog="sample">

        <id name="id" type="java.lang.Integer">

            <column name="id" />

            <generator class="native" />

        </id>

        <many-to-one name="TUser" class="com.inspur.po.TUser" fetch="select">

            <column name="userid" />

        </many-to-one>

        <property name="address" type="java.lang.String">

            <column name="address" length="30" />

        </property>

        <property name="tel" type="java.lang.String">

            <column name="tel" length="20" />

        </property>

        <property name="zipcode" type="java.lang.String">

            <column name="zipcode" length="30" />

        </property>

    </class>

</hibernate-mapping>

Tuser.hbm.xml配置文件如下:

<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!--

    Mapping file autogenerated by MyEclipse Persistence Tools

-->

<hibernate-mapping>

    <class name="com.inspur.po.TUser" table="t_user" catalog="sample">

        <id name="id" type="java.lang.Integer">

            <column name="id" />

            <generator class="native" />

        </id>

        <property name="name" type="java.lang.String">

            <column name="name" length="30" />

        </property>

        <property name="age" type="java.lang.Integer">

            <column name="age" />

        </property>

        <set name="TAddresses" inverse="true" cascade="all">

            <key>

                <column name="userid" />

            </key>

            <one-to-many class="com.inspur.po.TAddress" />

        </set>

    </class>

</hibernate-mapping>

测试类代码如下:

package com.inspur.test;

 

import org.hibernate.HibernateException;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

 

import com.inspur.po.TAddress;

import com.inspur.po.TUser;

 

import junit.framework.TestCase;

 

public class Test extends TestCase {

    Session session=null;

 

    @Override

    protected void setUp() throws Exception {

       Configuration config=new Configuration().configure();

       SessionFactory sessionFactory=config.buildSessionFactory();

       session=sessionFactory.openSession();

    }

 

    @Override

    protected void tearDown() throws Exception {

       session.close();

    }

    public void testInsert(){

       TUser user=new TUser();

       user.setAge(20);

       user.setName("liuzhq");

       TAddress address=new TAddress();

       address.setAddress("hongkang");

       address.setTel("123");

       address.setZipcode("1234344");

       address.setTUser(user);

       user.getTAddresses().add(address);

       Transaction tran=null;

       try{

           tran=session.beginTransaction();

           session.save(user);

           session.flush();

           tran.commit();

       }catch(HibernateException e){

           if(tran!=null){

              try{

                  tran.rollback();

              }catch(HibernateException e1){

                  e1.printStackTrace();

              }

           }

           e.printStackTrace();

       }

    }

   

 

}

原文地址:https://www.cnblogs.com/moonfans/p/2956490.html