Home  >  Article  >  Database  >  Hibernate关联关系映射(单向篇)

Hibernate关联关系映射(单向篇)

WBOY
WBOYOriginal
2016-06-07 14:51:211117browse

Hibernate关联关系可分为单向关联和双向关联两大类。单向关联可以分为一对一、一对多、多对一和多对多4种关联方式,而多向关联可以分为一对一、一对多和多对多3种关联方式。 Hibernate连接管理类HibernateUtil.java public class HibernateUtil { private st

Hibernate关联关系可分为单向关联和双向关联两大类。单向关联可以分为一对一、一对多、多对一和多对多4种关联方式,而多向关联可以分为一对一、一对多和多对多3种关联方式。

Hibernate连接管理类HibernateUtil.java

<code class=" hljs cs"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> HibernateUtil {
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> SessionFactory sessionFactory;

    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> final ThreadLocal<Session> threadLocal = <span class="hljs-keyword">new</span> ThreadLocal<Session>();

    <span class="hljs-keyword">static</span>{
        <span class="hljs-keyword">try</span>{
            Configuration cfg = <span class="hljs-keyword">new</span> Configuration().configure();
            sessionFactory = cfg.buildSessionFactory();
        }<span class="hljs-keyword">catch</span>(Throwable ex){
            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> ExceptionInInitializerError(ex);
        }
    }

    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> SessionFactory <span class="hljs-title">getSessionFactory</span>(){
        <span class="hljs-keyword">return</span> sessionFactory;
    }

    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> Session <span class="hljs-title">getSession</span>() throws HibernateException{
        Session session = (Session)threadLocal.<span class="hljs-keyword">get</span>();
        <span class="hljs-keyword">if</span>(session == <span class="hljs-keyword">null</span> || !session.isOpen()){
            session = (sessionFactory != <span class="hljs-keyword">null</span>)?sessionFactory.openSession():<span class="hljs-keyword">null</span>;
            threadLocal.<span class="hljs-keyword">set</span>(session);
        }

        <span class="hljs-keyword">return</span> session;
    }

    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">closeSession</span>() throws HibernateException{
        Session session = (Session)threadLocal.<span class="hljs-keyword">get</span>();
        threadLocal.<span class="hljs-keyword">set</span>(<span class="hljs-keyword">null</span>);
        <span class="hljs-keyword">if</span>(session != <span class="hljs-keyword">null</span>){
            session.close();
        }
    }

    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">shutdown</span>(){
        getSessionFactory().close();
    }

}</code>

1.单向的一对一关联

(1)通过主键关联

通过主键关联,是指两个数据表之间通过主键建立一对一的关联关系。这两张表的主键值是相同的,一张表改动时,另一张也会相关的发生改变,从而避免多余字段被创建,但基于主键关联的持久化类(其对应的数据表称为从表)不能拥有自己的主键生成策略,它的主键由关联类负责生成。另外,增加one-to-one元素来关联属性,必须为one-to-one元素增加constrained=”true”属性,表明该类主键由关联类生成。

示例:

实体类:

User.java

<code class=" hljs cs"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> User {
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> userid;
    <span class="hljs-keyword">private</span> String name;
    <span class="hljs-keyword">private</span> String password;
    <span class="hljs-keyword">private</span> Address address;
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">getUserid</span>() {
        <span class="hljs-keyword">return</span> userid;
    }
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setUserid</span>(<span class="hljs-keyword">int</span> userid) {
        <span class="hljs-keyword">this</span>.userid = userid;
    }
    <span class="hljs-keyword">public</span> String <span class="hljs-title">getName</span>() {
        <span class="hljs-keyword">return</span> name;
    }
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setName</span>(String name) {
        <span class="hljs-keyword">this</span>.name = name;
    }
    <span class="hljs-keyword">public</span> String <span class="hljs-title">getPassword</span>() {
        <span class="hljs-keyword">return</span> password;
    }
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setPassword</span>(String password) {
        <span class="hljs-keyword">this</span>.password = password;
    }
    <span class="hljs-keyword">public</span> Address <span class="hljs-title">getAddress</span>() {
        <span class="hljs-keyword">return</span> address;
    }
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setAddress</span>(Address address) {
        <span class="hljs-keyword">this</span>.address = address;
    }   
}</code>

Address.java

<code class=" hljs cs"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> Address {
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> addressid;
    <span class="hljs-keyword">private</span> String addressinfo;
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">getAddressid</span>() {
        <span class="hljs-keyword">return</span> addressid;
    }
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setAddressid</span>(<span class="hljs-keyword">int</span> addressid) {
        <span class="hljs-keyword">this</span>.addressid = addressid;
    }
    <span class="hljs-keyword">public</span> String <span class="hljs-title">getAddressinfo</span>() {
        <span class="hljs-keyword">return</span> addressinfo;
    }
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setAddressinfo</span>(String addressinfo) {
        <span class="hljs-keyword">this</span>.addressinfo = addressinfo;
    }


}</code>

表结构:

user表:

这里写图片描述

address表:

这里写图片描述

配置文件:

User.hbm.xml

<code class=" hljs applescript"><hibernate-mapping>
    <<span class="hljs-type">class</span> <span class="hljs-property">name</span>=<span class="hljs-string">"com.entity.User"</span> table=<span class="hljs-string">"user"</span>>
        <<span class="hljs-property">id</span> <span class="hljs-property">name</span>=<span class="hljs-string">"id"</span> type=<span class="hljs-string">"java.lang.Integer"</span> column=<span class="hljs-string">"user_id"</span>>
            <generator <span class="hljs-type">class</span>=<span class="hljs-string">"foreign"</span>>
                <param <span class="hljs-property">name</span>=<span class="hljs-string">"property"</span>>address</param>
            </generator>
        </<span class="hljs-property">id</span>>

        <<span class="hljs-keyword">property</span> <span class="hljs-property">name</span>=<span class="hljs-string">"name"</span> type=<span class="hljs-string">"java.lang.String"</span> column=<span class="hljs-string">"name"</span>>
        </<span class="hljs-keyword">property</span>>
        <<span class="hljs-keyword">property</span> <span class="hljs-property">name</span>=<span class="hljs-string">"password"</span> type=<span class="hljs-string">"java.lang.String"</span> column=<span class="hljs-string">"password"</span>>
        </<span class="hljs-keyword">property</span>>
        <one-<span class="hljs-keyword">to</span>-one <span class="hljs-property">name</span>=<span class="hljs-string">"address"</span> <span class="hljs-type">class</span>=<span class="hljs-string">"Address"</span> constrained=<span class="hljs-string">"true"</span>></one-<span class="hljs-keyword">to</span>-one>

    </<span class="hljs-type">class</span>>
</hibernate-mapping></code>

Address.hbm.xml

<code class=" hljs applescript"><hibernate-mapping>
    <<span class="hljs-type">class</span> <span class="hljs-property">name</span>=<span class="hljs-string">"com.entity.Address"</span> table=<span class="hljs-string">"address"</span>>
        <<span class="hljs-property">id</span> <span class="hljs-property">name</span>=<span class="hljs-string">"addressid"</span> type=<span class="hljs-string">"java.lang.Integer"</span> column=<span class="hljs-string">"addressid"</span>>
            <generator <span class="hljs-type">class</span>=<span class="hljs-string">"identity"</span>/>
        </<span class="hljs-property">id</span>>

        <<span class="hljs-keyword">property</span> <span class="hljs-property">name</span>=<span class="hljs-string">"addressinfo"</span> type=<span class="hljs-string">"java.lang.String"</span> column=<span class="hljs-string">"addressinfo"</span>>
        </<span class="hljs-keyword">property</span>>


    </<span class="hljs-type">class</span>>
</hibernate-mapping></code>

测试类Test.java

<code class=" hljs cs"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> Test {
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span>(String[] args){
        Session session = HibernateUtil.getSession();
        Transaction tx = session.beginTransaction();

        User u = <span class="hljs-keyword">new</span> User();
        u.setName(<span class="hljs-string">"sbw"</span>);
        u.setPassword(<span class="hljs-string">"123"</span>);

        Address a = <span class="hljs-keyword">new</span> Address();
        a.setAddressinfo(<span class="hljs-string">"heu"</span>);
        u.setAddress(a);

        session.save(a);
        session.save(u);
        tx.commit();

        HibernateUtil.closeSession();

    }
}</code>

运行结果:

这里写图片描述

(2)通过外键关联

通过外键关联时两张数据表的主键是不同的,通过在一张表中添加外键列来保持一对一的关系。配置外键关联关系时需要使用many-to-one元素。因为通过外键关联的一对一关系,本质上是多对一关系的特例。因此,只需在many-to-one元素中增加unique=“true”属性即可,这相当于在多的一端增加了唯一性的约束,表示多的一端也必须是唯一的,这样就变成为单向的一对一关系了。

示例:

实体类与通过主键关联相同。

表结构:

user表

这里写图片描述

address表

这里写图片描述

配置文件:

User.hbm.xml

<code class=" hljs applescript"><hibernate-mapping>
    <<span class="hljs-type">class</span> <span class="hljs-property">name</span>=<span class="hljs-string">"com.entity.User"</span> table=<span class="hljs-string">"user"</span>>
        <<span class="hljs-property">id</span> <span class="hljs-property">name</span>=<span class="hljs-string">"userid"</span> type=<span class="hljs-string">"java.lang.Integer"</span> column=<span class="hljs-string">"userid"</span>>
            <generator <span class="hljs-type">class</span>=<span class="hljs-string">"identity"</span>/>
        </<span class="hljs-property">id</span>>

        <<span class="hljs-keyword">property</span> <span class="hljs-property">name</span>=<span class="hljs-string">"name"</span> type=<span class="hljs-string">"java.lang.String"</span> column=<span class="hljs-string">"name"</span>>
        </<span class="hljs-keyword">property</span>>
        <<span class="hljs-keyword">property</span> <span class="hljs-property">name</span>=<span class="hljs-string">"password"</span> type=<span class="hljs-string">"java.lang.String"</span> column=<span class="hljs-string">"password"</span>>
        </<span class="hljs-keyword">property</span>>

        <many-<span class="hljs-keyword">to</span>-one <span class="hljs-property">name</span>=<span class="hljs-string">"address"</span> <span class="hljs-type">class</span>=<span class="hljs-string">"com.entity.Address"</span> unique=<span class="hljs-string">"true"</span> column=<span class="hljs-string">"addressid"</span>></many-<span class="hljs-keyword">to</span>-one>

    </<span class="hljs-type">class</span>>
</hibernate-mapping></code>

Address.hbm.xml

<code class=" hljs applescript"><hibernate-mapping>
    <<span class="hljs-type">class</span> <span class="hljs-property">name</span>=<span class="hljs-string">"com.entity.Address"</span> table=<span class="hljs-string">"address"</span>>
        <<span class="hljs-property">id</span> <span class="hljs-property">name</span>=<span class="hljs-string">"addressid"</span> type=<span class="hljs-string">"java.lang.Integer"</span> column=<span class="hljs-string">"addressid"</span>>
            <generator <span class="hljs-type">class</span>=<span class="hljs-string">"identity"</span>/>
        </<span class="hljs-property">id</span>>

        <<span class="hljs-keyword">property</span> <span class="hljs-property">name</span>=<span class="hljs-string">"addressinfo"</span> type=<span class="hljs-string">"java.lang.String"</span> column=<span class="hljs-string">"addressinfo"</span>>
        </<span class="hljs-keyword">property</span>>


    </<span class="hljs-type">class</span>>
</hibernate-mapping></code>

测试类与通过主键关联一样

运行结果:

这里写图片描述

2.单向一对多关联

单向的一对多关联映射关系主要是通过外键来关联的。一对多的关联映射是在表示多的一方的数据表中增加一个外键,并由“一”的一方指向“多”的一方。单向的一对多关联的持久化类里需要包含一个集合属性,在“一”的一方访问“多”的一方时,“多”的一方将以集合的形式来体现。

示例:

实体类:

User.java

<code class=" hljs cs"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> User {
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> userid;
    <span class="hljs-keyword">private</span> String name;
    <span class="hljs-keyword">private</span> String password;
    <span class="hljs-keyword">private</span> Set<Address> address = <span class="hljs-keyword">new</span> HashSet<Address>();

    <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">getUserid</span>() {
        <span class="hljs-keyword">return</span> userid;
    }
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setUserid</span>(<span class="hljs-keyword">int</span> userid) {
        <span class="hljs-keyword">this</span>.userid = userid;
    }
    <span class="hljs-keyword">public</span> String <span class="hljs-title">getName</span>() {
        <span class="hljs-keyword">return</span> name;
    }
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setName</span>(String name) {
        <span class="hljs-keyword">this</span>.name = name;
    }
    <span class="hljs-keyword">public</span> String <span class="hljs-title">getPassword</span>() {
        <span class="hljs-keyword">return</span> password;
    }
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setPassword</span>(String password) {
        <span class="hljs-keyword">this</span>.password = password;
    }
    <span class="hljs-keyword">public</span> Set<Address> <span class="hljs-title">getAddress</span>() {
        <span class="hljs-keyword">return</span> address;
    }
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setAddress</span>(Set<Address> address) {
        <span class="hljs-keyword">this</span>.address = address;
    }       
}</code>

Address.java

<code class=" hljs cs"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> Address {
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> addressid;
    <span class="hljs-keyword">private</span> String addressinfo;

    <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">getAddressid</span>() {
        <span class="hljs-keyword">return</span> addressid;
    }
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setAddressid</span>(<span class="hljs-keyword">int</span> addressid) {
        <span class="hljs-keyword">this</span>.addressid = addressid;
    }
    <span class="hljs-keyword">public</span> String <span class="hljs-title">getAddressinfo</span>() {
        <span class="hljs-keyword">return</span> addressinfo;
    }
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setAddressinfo</span>(String addressinfo) {
        <span class="hljs-keyword">this</span>.addressinfo = addressinfo;
    }   
}</code>

表结构:

User表

这里写图片描述

Address表

这里写图片描述

配置文件

User.hbm.xml

<code class=" hljs applescript"><hibernate-mapping>
    <<span class="hljs-type">class</span> <span class="hljs-property">name</span>=<span class="hljs-string">"com.entity.User"</span> table=<span class="hljs-string">"user"</span>>
        <<span class="hljs-property">id</span> <span class="hljs-property">name</span>=<span class="hljs-string">"userid"</span> type=<span class="hljs-string">"java.lang.Integer"</span> column=<span class="hljs-string">"userid"</span>>
            <generator <span class="hljs-type">class</span>=<span class="hljs-string">"identity"</span>/>
        </<span class="hljs-property">id</span>>

        <<span class="hljs-keyword">property</span> <span class="hljs-property">name</span>=<span class="hljs-string">"name"</span> type=<span class="hljs-string">"java.lang.String"</span> column=<span class="hljs-string">"name"</span>>
        </<span class="hljs-keyword">property</span>>
        <<span class="hljs-keyword">property</span> <span class="hljs-property">name</span>=<span class="hljs-string">"password"</span> type=<span class="hljs-string">"java.lang.String"</span> column=<span class="hljs-string">"password"</span>>
        </<span class="hljs-keyword">property</span>>

        <<span class="hljs-keyword">set</span> <span class="hljs-property">name</span>=<span class="hljs-string">"address"</span> table=<span class="hljs-string">"address"</span> inverse=<span class="hljs-string">"false"</span> lazy=<span class="hljs-string">"true"</span>>
            <key>
                <column <span class="hljs-property">name</span>=<span class="hljs-string">"userid"</span>/>
            </key>

            <one-<span class="hljs-keyword">to</span>-many <span class="hljs-type">class</span>=<span class="hljs-string">"com.entity.User"</span>/>
        </<span class="hljs-keyword">set</span>>

    </<span class="hljs-type">class</span>>
</hibernate-mapping></code>

Address.hbm.xml

<code class=" hljs applescript"><hibernate-mapping>
    <<span class="hljs-type">class</span> <span class="hljs-property">name</span>=<span class="hljs-string">"com.entity.Address"</span> table=<span class="hljs-string">"address"</span>>
        <<span class="hljs-property">id</span> <span class="hljs-property">name</span>=<span class="hljs-string">"addressid"</span> type=<span class="hljs-string">"java.lang.Integer"</span> column=<span class="hljs-string">"addressid"</span>>
            <generator <span class="hljs-type">class</span>=<span class="hljs-string">"identity"</span>/>
        </<span class="hljs-property">id</span>>

        <<span class="hljs-keyword">property</span> <span class="hljs-property">name</span>=<span class="hljs-string">"addressinfo"</span> type=<span class="hljs-string">"java.lang.String"</span> column=<span class="hljs-string">"addressinfo"</span>>
        </<span class="hljs-keyword">property</span>>


    </<span class="hljs-type">class</span>>
</hibernate-mapping></code>

测试代码

Test.java

<code class=" hljs avrasm">public class Test {
    public static void main(String[] args){
        Session session = HibernateUtil<span class="hljs-preprocessor">.getSession</span>()<span class="hljs-comment">;</span>
        Transaction tx = session<span class="hljs-preprocessor">.beginTransaction</span>()<span class="hljs-comment">;</span>

        User u = new User()<span class="hljs-comment">;</span>
        u<span class="hljs-preprocessor">.setName</span>(<span class="hljs-string">"sbw"</span>)<span class="hljs-comment">;</span>
        u<span class="hljs-preprocessor">.setPassword</span>(<span class="hljs-string">"123"</span>)<span class="hljs-comment">;</span>

        Address a = new Address()<span class="hljs-comment">;</span>
        a<span class="hljs-preprocessor">.setAddressinfo</span>(<span class="hljs-string">"heu"</span>)<span class="hljs-comment">;</span>
        Address a1 = new Address()<span class="hljs-comment">;</span>
        a1<span class="hljs-preprocessor">.setAddressinfo</span>(<span class="hljs-string">"hrb"</span>)<span class="hljs-comment">;</span>

        u<span class="hljs-preprocessor">.getAddress</span>()<span class="hljs-preprocessor">.add</span>(a)<span class="hljs-comment">;</span>
        u<span class="hljs-preprocessor">.getAddress</span>()<span class="hljs-preprocessor">.add</span>(a1)<span class="hljs-comment">;</span>

        session<span class="hljs-preprocessor">.save</span>(a)<span class="hljs-comment">;</span>
        session<span class="hljs-preprocessor">.save</span>(a1)<span class="hljs-comment">;</span>
        session<span class="hljs-preprocessor">.save</span>(u)<span class="hljs-comment">;</span>
        tx<span class="hljs-preprocessor">.commit</span>()<span class="hljs-comment">;</span>

        HibernateUtil<span class="hljs-preprocessor">.closeSession</span>()<span class="hljs-comment">;</span>

    }
}</code>

运行结果:

这里写图片描述

3.单向的多对一关联

单向的多对一关联映射关系也是通过外键来关联的。多对一的映射方式类似于一对多的映射方式,不过它的映射关系是由“多”的一方指向“一”的一方。在表示“多”的一方的数据表中增加一个外键来指向表示“一”的一方的数据表,“一”的一方作为主表,”多“的一方作为从表。

示例:

实体类

User.java

<code class=" hljs cs"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> User {
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> userid;
    <span class="hljs-keyword">private</span> String name;
    <span class="hljs-keyword">private</span> String password;
    <span class="hljs-keyword">private</span> Address address = <span class="hljs-keyword">new</span> Address();

    <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">getUserid</span>() {
        <span class="hljs-keyword">return</span> userid;
    }
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setUserid</span>(<span class="hljs-keyword">int</span> userid) {
        <span class="hljs-keyword">this</span>.userid = userid;
    }
    <span class="hljs-keyword">public</span> String <span class="hljs-title">getName</span>() {
        <span class="hljs-keyword">return</span> name;
    }
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setName</span>(String name) {
        <span class="hljs-keyword">this</span>.name = name;
    }
    <span class="hljs-keyword">public</span> String <span class="hljs-title">getPassword</span>() {
        <span class="hljs-keyword">return</span> password;
    }
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setPassword</span>(String password) {
        <span class="hljs-keyword">this</span>.password = password;
    }
    <span class="hljs-keyword">public</span> Address <span class="hljs-title">getAddress</span>() {
        <span class="hljs-keyword">return</span> address;
    }
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setAddress</span>(Address address) {
        <span class="hljs-keyword">this</span>.address = address;
    }       
}</code>

Address.java

<code class=" hljs cs"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> Address {
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> addressid;
    <span class="hljs-keyword">private</span> String addressinfo;

    <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">getAddressid</span>() {
        <span class="hljs-keyword">return</span> addressid;
    }
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setAddressid</span>(<span class="hljs-keyword">int</span> addressid) {
        <span class="hljs-keyword">this</span>.addressid = addressid;
    }
    <span class="hljs-keyword">public</span> String <span class="hljs-title">getAddressinfo</span>() {
        <span class="hljs-keyword">return</span> addressinfo;
    }
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setAddressinfo</span>(String addressinfo) {
        <span class="hljs-keyword">this</span>.addressinfo = addressinfo;
    }   
}</code>

表结构:

user表

这里写图片描述

address表

这里写图片描述

配置文件:

User.hbm.xml

<code class=" hljs applescript"><hibernate-mapping>
    <<span class="hljs-type">class</span> <span class="hljs-property">name</span>=<span class="hljs-string">"com.entity.User"</span> table=<span class="hljs-string">"user"</span>>
        <<span class="hljs-property">id</span> <span class="hljs-property">name</span>=<span class="hljs-string">"userid"</span> type=<span class="hljs-string">"java.lang.Integer"</span> column=<span class="hljs-string">"userid"</span>>
            <generator <span class="hljs-type">class</span>=<span class="hljs-string">"identity"</span>/>
        </<span class="hljs-property">id</span>>

        <<span class="hljs-keyword">property</span> <span class="hljs-property">name</span>=<span class="hljs-string">"name"</span> type=<span class="hljs-string">"java.lang.String"</span> column=<span class="hljs-string">"name"</span>>
        </<span class="hljs-keyword">property</span>>
        <<span class="hljs-keyword">property</span> <span class="hljs-property">name</span>=<span class="hljs-string">"password"</span> type=<span class="hljs-string">"java.lang.String"</span> column=<span class="hljs-string">"password"</span>>
        </<span class="hljs-keyword">property</span>>

        <many-<span class="hljs-keyword">to</span>-one <span class="hljs-property">name</span>=<span class="hljs-string">"address"</span> column=<span class="hljs-string">"addressid"</span>/> 

    </<span class="hljs-type">class</span>>
</hibernate-mapping></code>

Address.hbm.xml

<code class=" hljs applescript"><hibernate-mapping>
    <<span class="hljs-type">class</span> <span class="hljs-property">name</span>=<span class="hljs-string">"com.entity.Address"</span> table=<span class="hljs-string">"address"</span>>
        <<span class="hljs-property">id</span> <span class="hljs-property">name</span>=<span class="hljs-string">"addressid"</span> type=<span class="hljs-string">"java.lang.Integer"</span> column=<span class="hljs-string">"addressid"</span>>
            <generator <span class="hljs-type">class</span>=<span class="hljs-string">"identity"</span>/>
        </<span class="hljs-property">id</span>>

        <<span class="hljs-keyword">property</span> <span class="hljs-property">name</span>=<span class="hljs-string">"addressinfo"</span> type=<span class="hljs-string">"java.lang.String"</span> column=<span class="hljs-string">"addressinfo"</span>>
        </<span class="hljs-keyword">property</span>>


    </<span class="hljs-type">class</span>>
</hibernate-mapping></code>

测试类Test.java

<code class=" hljs avrasm">public class Test {
    public static void main(String[] args){
        Session session = HibernateUtil<span class="hljs-preprocessor">.getSession</span>()<span class="hljs-comment">;</span>
        Transaction tx = session<span class="hljs-preprocessor">.beginTransaction</span>()<span class="hljs-comment">;</span>

        User u = new User()<span class="hljs-comment">;</span>
        u<span class="hljs-preprocessor">.setName</span>(<span class="hljs-string">"sbw"</span>)<span class="hljs-comment">;</span>
        u<span class="hljs-preprocessor">.setPassword</span>(<span class="hljs-string">"123"</span>)<span class="hljs-comment">;</span>

        User u1 = new User()<span class="hljs-comment">;</span>
        u<span class="hljs-preprocessor">.setName</span>(<span class="hljs-string">"gaoya"</span>)<span class="hljs-comment">;</span>
        u<span class="hljs-preprocessor">.setPassword</span>(<span class="hljs-string">"456"</span>)<span class="hljs-comment">;</span>

        Address a = new Address()<span class="hljs-comment">;</span>
        a<span class="hljs-preprocessor">.setAddressinfo</span>(<span class="hljs-string">"heu"</span>)<span class="hljs-comment">;</span>

        u<span class="hljs-preprocessor">.setAddress</span>(a)<span class="hljs-comment">;</span>
        u1<span class="hljs-preprocessor">.setAddress</span>(a)<span class="hljs-comment">;</span>

        session<span class="hljs-preprocessor">.save</span>(a)<span class="hljs-comment">;</span>
        session<span class="hljs-preprocessor">.save</span>(u)<span class="hljs-comment">;</span>
        session<span class="hljs-preprocessor">.save</span>(u1)<span class="hljs-comment">;</span>
        tx<span class="hljs-preprocessor">.commit</span>()<span class="hljs-comment">;</span>

        HibernateUtil<span class="hljs-preprocessor">.closeSession</span>()<span class="hljs-comment">;</span>

    }
}</code>

测试结果:

这里写图片描述

4.单向的多对多关联

多对多关联在数据库中是比较常见的,它利用中间表将两个主表关联起来。中间表的作用是将两张表的主键作为其外键,通过外键建立这两张表的映射关系。在单向的多对多关联中,需要在主控端的类定义中增加一个Set集合属性,使得被关联一方的类的实例以集合的形式存在。

示例:

实体类:

User.java

<code class=" hljs cs"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> User {
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> userid;
    <span class="hljs-keyword">private</span> String name;
    <span class="hljs-keyword">private</span> String password;
    <span class="hljs-keyword">private</span> Set<Address> address = <span class="hljs-keyword">new</span> HashSet<Address>();

    <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">getUserid</span>() {
        <span class="hljs-keyword">return</span> userid;
    }
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setUserid</span>(<span class="hljs-keyword">int</span> userid) {
        <span class="hljs-keyword">this</span>.userid = userid;
    }
    <span class="hljs-keyword">public</span> String <span class="hljs-title">getName</span>() {
        <span class="hljs-keyword">return</span> name;
    }
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setName</span>(String name) {
        <span class="hljs-keyword">this</span>.name = name;
    }
    <span class="hljs-keyword">public</span> String <span class="hljs-title">getPassword</span>() {
        <span class="hljs-keyword">return</span> password;
    }
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setPassword</span>(String password) {
        <span class="hljs-keyword">this</span>.password = password;
    }
    <span class="hljs-keyword">public</span> Set<Address> <span class="hljs-title">getAddress</span>() {
        <span class="hljs-keyword">return</span> address;
    }
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setAddress</span>(Set<Address> address) {
        <span class="hljs-keyword">this</span>.address = address;
    }       
}</code>

Address.java

<code class=" hljs cs"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> Address {
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> addressid;
    <span class="hljs-keyword">private</span> String addressinfo;

    <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">getAddressid</span>() {
        <span class="hljs-keyword">return</span> addressid;
    }
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setAddressid</span>(<span class="hljs-keyword">int</span> addressid) {
        <span class="hljs-keyword">this</span>.addressid = addressid;
    }
    <span class="hljs-keyword">public</span> String <span class="hljs-title">getAddressinfo</span>() {
        <span class="hljs-keyword">return</span> addressinfo;
    }
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setAddressinfo</span>(String addressinfo) {
        <span class="hljs-keyword">this</span>.addressinfo = addressinfo;
    }   
}</code>

表结构:

user表:

这里写图片描述

user_address表:

这里写图片描述

address表:

这里写图片描述

配置文件:

User.hbm.xml

<code class=" hljs applescript"><hibernate-mapping>
    <<span class="hljs-type">class</span> <span class="hljs-property">name</span>=<span class="hljs-string">"com.entity.User"</span> table=<span class="hljs-string">"user"</span>>
        <<span class="hljs-property">id</span> <span class="hljs-property">name</span>=<span class="hljs-string">"userid"</span> type=<span class="hljs-string">"java.lang.Integer"</span> column=<span class="hljs-string">"userid"</span>>
            <generator <span class="hljs-type">class</span>=<span class="hljs-string">"identity"</span>/>
        </<span class="hljs-property">id</span>>

        <<span class="hljs-keyword">property</span> <span class="hljs-property">name</span>=<span class="hljs-string">"name"</span> type=<span class="hljs-string">"java.lang.String"</span> column=<span class="hljs-string">"name"</span>>
        </<span class="hljs-keyword">property</span>>
        <<span class="hljs-keyword">property</span> <span class="hljs-property">name</span>=<span class="hljs-string">"password"</span> type=<span class="hljs-string">"java.lang.String"</span> column=<span class="hljs-string">"password"</span>>
        </<span class="hljs-keyword">property</span>>

        <<span class="hljs-keyword">set</span> <span class="hljs-property">name</span>=<span class="hljs-string">"address"</span> table=<span class="hljs-string">"user_address"</span>>
            <key column=<span class="hljs-string">"userid"</span>/>
            <many-<span class="hljs-keyword">to</span>-many column=<span class="hljs-string">"addressid"</span> <span class="hljs-type">class</span>=<span class="hljs-string">"com.entity.Address"</span>/>
        </<span class="hljs-keyword">set</span>> 

    </<span class="hljs-type">class</span>>
</hibernate-mapping></code>

Address.hbm.xml

<code class=" hljs applescript"><hibernate-mapping>
    <<span class="hljs-type">class</span> <span class="hljs-property">name</span>=<span class="hljs-string">"com.entity.Address"</span> table=<span class="hljs-string">"address"</span>>
        <<span class="hljs-property">id</span> <span class="hljs-property">name</span>=<span class="hljs-string">"addressid"</span> type=<span class="hljs-string">"java.lang.Integer"</span> column=<span class="hljs-string">"addressid"</span>>
            <generator <span class="hljs-type">class</span>=<span class="hljs-string">"identity"</span>/>
        </<span class="hljs-property">id</span>>

        <<span class="hljs-keyword">property</span> <span class="hljs-property">name</span>=<span class="hljs-string">"addressinfo"</span> type=<span class="hljs-string">"java.lang.String"</span> column=<span class="hljs-string">"addressinfo"</span>>
        </<span class="hljs-keyword">property</span>>


    </<span class="hljs-type">class</span>>
</hibernate-mapping></code>

测试代码Test.java

<code class=" hljs avrasm">public class Test {
    public static void main(String[] args){
        Session session = HibernateUtil<span class="hljs-preprocessor">.getSession</span>()<span class="hljs-comment">;</span>
        Transaction tx = session<span class="hljs-preprocessor">.beginTransaction</span>()<span class="hljs-comment">;</span>

        User u = new User()<span class="hljs-comment">;</span>
        u<span class="hljs-preprocessor">.setName</span>(<span class="hljs-string">"sbw"</span>)<span class="hljs-comment">;</span>
        u<span class="hljs-preprocessor">.setPassword</span>(<span class="hljs-string">"123"</span>)<span class="hljs-comment">;       </span>
        User u1 = new User()<span class="hljs-comment">;</span>
        u1<span class="hljs-preprocessor">.setName</span>(<span class="hljs-string">"gaoya"</span>)<span class="hljs-comment">;</span>
        u1<span class="hljs-preprocessor">.setPassword</span>(<span class="hljs-string">"456"</span>)<span class="hljs-comment">;</span>

        Address a = new Address()<span class="hljs-comment">;</span>
        a<span class="hljs-preprocessor">.setAddressinfo</span>(<span class="hljs-string">"heu"</span>)<span class="hljs-comment">;</span>
        Address a1 = new Address()<span class="hljs-comment">;</span>
        a1<span class="hljs-preprocessor">.setAddressinfo</span>(<span class="hljs-string">"hrb"</span>)<span class="hljs-comment">;</span>

        u<span class="hljs-preprocessor">.getAddress</span>()<span class="hljs-preprocessor">.add</span>(a)<span class="hljs-comment">;</span>
        u<span class="hljs-preprocessor">.getAddress</span>()<span class="hljs-preprocessor">.add</span>(a1)<span class="hljs-comment">;</span>
        u1<span class="hljs-preprocessor">.getAddress</span>()<span class="hljs-preprocessor">.add</span>(a)<span class="hljs-comment">;</span>
        u1<span class="hljs-preprocessor">.getAddress</span>()<span class="hljs-preprocessor">.add</span>(a1)<span class="hljs-comment">;</span>

        session<span class="hljs-preprocessor">.save</span>(a)<span class="hljs-comment">;</span>
        session<span class="hljs-preprocessor">.save</span>(a1)<span class="hljs-comment">;</span>
        session<span class="hljs-preprocessor">.save</span>(u)<span class="hljs-comment">;</span>
        session<span class="hljs-preprocessor">.save</span>(u1)<span class="hljs-comment">;</span>
        tx<span class="hljs-preprocessor">.commit</span>()<span class="hljs-comment">;</span>

        HibernateUtil<span class="hljs-preprocessor">.closeSession</span>()<span class="hljs-comment">;</span>

    }
}</code>

测试结果:

这里写图片描述

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn