search
HomeDatabaseMysql TutorialHibernate关联关系映射(双向篇)
Hibernate关联关系映射(双向篇)Jun 07, 2016 pm 02:51 PM
hibernaterelationassociationmapping

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)通过主键关联

通过主键关联的双向一对一映射,在需要一方的配置文件中将主键生成策略配置成foreign,即表示需要根据另一方的主键来生成自己的主键,而该实体本身不具有自己的主键生成策略。

示例:

实体类:

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">private</span> User user;

    <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;
    }
    <span class="hljs-keyword">public</span> User <span class="hljs-title">getUser</span>() {
        <span class="hljs-keyword">return</span> user;
    }
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setUser</span>(User user) {
        <span class="hljs-keyword">this</span>.user = user;
    }   

}</code>

表结构:

User表

这里写图片描述

Address表

这里写图片描述

配置文件:

Use.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>>

        <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">"com.entity.Address"</span> cascade=<span class="hljs-string">"all"</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">"foreign"</span>>
                <param <span class="hljs-property">name</span>=<span class="hljs-string">"property"</span>>user</param>
            </generator>
        </<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>>

        <one-<span class="hljs-keyword">to</span>-one <span class="hljs-property">name</span>=<span class="hljs-string">"user"</span> <span class="hljs-type">class</span>=<span class="hljs-string">"com.entity.User"</span> constrained=<span class="hljs-string">"true"</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>);

        a.setUser(u);
        u.setAddress(a);


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

        HibernateUtil.closeSession();

    }
}</code>

测试结果:

这里写图片描述

(2)通过外键关联

通过外键关联的双向一对一映射,外键可以放在任意一方。在存放外键一方的映射文件中,需要添加many-to-one元素,并为该元素添加unique=“true”属性。而另一方的配置文件中要添加one-to-one元素,并使用其name属性来指定关联属性名。此时,存放外键的一方对应的数据表为从表,而另一方对应的数据表变为主表。

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

表结构:

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>>

        <one-<span class="hljs-keyword">to</span>-one <span class="hljs-property">name</span>=<span class="hljs-string">"address"</span> cascade=<span class="hljs-string">"all"</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>>
            </generator>
        </<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>>

        <many-<span class="hljs-keyword">to</span>-one <span class="hljs-property">name</span>=<span class="hljs-string">"user"</span> <span class="hljs-type">class</span>=<span class="hljs-string">"com.entity.User"</span> unique=<span class="hljs-string">"true"</span>>
            <column <span class="hljs-property">name</span>=<span class="hljs-string">"userid"</span>/>
        </many-<span class="hljs-keyword">to</span>-one>

    </<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">private</span> User user;

    <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;
    }
    <span class="hljs-keyword">public</span> User <span class="hljs-title">getUser</span>() {
        <span class="hljs-keyword">return</span> user;
    }
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setUser</span>(User user) {
        <span class="hljs-keyword">this</span>.user = user;
    }   

}</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">"true"</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.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>>
            </generator>
        </<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>>

        <many-<span class="hljs-keyword">to</span>-one <span class="hljs-property">name</span>=<span class="hljs-string">"user"</span> <span class="hljs-type">class</span>=<span class="hljs-string">"com.entity.User"</span> unique=<span class="hljs-string">"true"</span>>
            <column <span class="hljs-property">name</span>=<span class="hljs-string">"userid"</span>/>
        </many-<span class="hljs-keyword">to</span>-one>

    </<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>
        a<span class="hljs-preprocessor">.setAddressinfo</span>(<span class="hljs-string">"hrb"</span>)<span class="hljs-comment">;</span>

        a<span class="hljs-preprocessor">.setUser</span>(u)<span class="hljs-comment">;</span>
        a1<span class="hljs-preprocessor">.setUser</span>(u)<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.双向的多对多关联

在双向的多对多关联中,两端都要添加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">private</span> Set<User> user = <span class="hljs-keyword">new</span> HashSet<User>();

    <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;
    }
    <span class="hljs-keyword">public</span> Set<User> <span class="hljs-title">getUser</span>() {
        <span class="hljs-keyword">return</span> user;
    }
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setUser</span>(Set<User> user) {
        <span class="hljs-keyword">this</span>.user = user;
    }   

}</code>

表结构:

user表

这里写图片描述

address表

这里写图片描述

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">"user_address"</span> inverse=<span class="hljs-string">"true"</span>>
            <key>
                <column <span class="hljs-property">name</span>=<span class="hljs-string">"userid"</span>/>
            </key>

            <many-<span class="hljs-keyword">to</span>-many <span class="hljs-type">class</span>=<span class="hljs-string">"com.entity.Address"</span> column=<span class="hljs-string">"addressid"</span>/>
        </<span class="hljs-keyword">set</span>>

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

Address.hbm.xml

<code class=" hljs xml"><span class="hljs-tag"><<span class="hljs-title">hibernate-mapping</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">class</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"com.entity.Address"</span> <span class="hljs-attribute">table</span>=<span class="hljs-value">"address"</span>></span>
        <span class="hljs-tag"><<span class="hljs-title">id</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"addressid"</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"java.lang.Integer"</span> <span class="hljs-attribute">column</span>=<span class="hljs-value">"addressid"</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">generator</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"identity"</span>></span>
            <span class="hljs-tag"></<span class="hljs-title">generator</span>></span>
        <span class="hljs-tag"></<span class="hljs-title">id</span>></span>

        <span class="hljs-tag"><<span class="hljs-title">property</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"addressinfo"</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"java.lang.String"</span> <span class="hljs-attribute">column</span>=<span class="hljs-value">"addressinfo"</span>></span>
        <span class="hljs-tag"></<span class="hljs-title">property</span>></span>

        <span class="hljs-tag"><<span class="hljs-title">set</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"user"</span> <span class="hljs-attribute">table</span>=<span class="hljs-value">"user_address"</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">key</span>></span>
                <span class="hljs-tag"><<span class="hljs-title">column</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"addressid"</span>/></span>
            <span class="hljs-tag"></<span class="hljs-title">key</span>></span>

            <span class="hljs-tag"><<span class="hljs-title">many-to-many</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"com.entity.User"</span> <span class="hljs-attribute">column</span>=<span class="hljs-value">"userid"</span>/></span>
        <span class="hljs-tag"></<span class="hljs-title">set</span>></span>

    <span class="hljs-tag"></<span class="hljs-title">class</span>></span>
<span class="hljs-tag"></<span class="hljs-title">hibernate-mapping</span>></span></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>

        a<span class="hljs-preprocessor">.getUser</span>()<span class="hljs-preprocessor">.add</span>(u)<span class="hljs-comment">;</span>
        a<span class="hljs-preprocessor">.getUser</span>()<span class="hljs-preprocessor">.add</span>(u1)<span class="hljs-comment">;</span>
        a1<span class="hljs-preprocessor">.getUser</span>()<span class="hljs-preprocessor">.add</span>(u)<span class="hljs-comment">;</span>
        a1<span class="hljs-preprocessor">.getUser</span>()<span class="hljs-preprocessor">.add</span>(u1)<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
SpringBoot项目里怎么集成HibernateSpringBoot项目里怎么集成HibernateMay 18, 2023 am 09:49 AM

在SpringBoot项目中集成Hibernate前言Hibernate是一个流行的ORM(对象关系映射)框架,它可以将Java对象映射到数据库表,从而方便地进行持久化操作。在SpringBoot项目中,集成Hibernate可以帮助我们更轻松地进行数据库操作,本文将介绍如何在SpringBoot项目中集成Hibernate,并提供相应的示例。1.引入依赖在pom.xml文件中引入以下依赖:org.springframework.bootspring-boot-starter-data-jpam

如何让MySQL外键和主键自动关联起来?如何让MySQL外键和主键自动关联起来?Mar 15, 2024 pm 12:54 PM

如何让MySQL外键和主键自动关联起来?在MySQL数据库中,外键和主键是非常重要的概念,它们能够帮助我们在不同表之间建立关联关系,保证数据的完整性和一致性。在实际的应用过程中,经常需要让外键自动关联到对应的主键上,以避免数据不一致的情况发生。下面将介绍如何通过具体的代码示例实现这一功能。首先,我们需要创建两个表,一个表作为主表,另一个表作为从表。在主表中创

Java错误:Hibernate错误,如何处理和避免Java错误:Hibernate错误,如何处理和避免Jun 25, 2023 am 09:09 AM

Java是一种面向对象编程语言,它被广泛地应用于软件开发领域。Hibernate是一种流行的Java持久化框架,它提供了一种简单且高效的方式来管理Java对象的持久化。然而,开发过程中经常会遇到Hibernate错误,这些错误可能会导致程序的异常终止或者不稳定。如何处理和避免Hibernate错误成为了Java开发者必须掌握的能力。本文将介绍一些常见的Hib

探讨安卓系统与Linux内核之间的关系探讨安卓系统与Linux内核之间的关系Mar 14, 2024 pm 12:48 PM

安卓系统与Linux内核是息息相关的两个实体,它们之间的关系紧密而又复杂。在安卓系统中,Linux内核充当着重要的角色,为安卓系统提供了底层的硬件驱动和系统调用支持。本文将探讨安卓系统与Linux内核之间的关系,以及它们是如何交互、协同工作的,同时提供一些具体的代码示例。安卓系统是基于Linux内核开发的移动操作系统,主要用于智能手机、平板电脑等移动设备。L

hibernate和mybatis有哪些区别hibernate和mybatis有哪些区别Jan 03, 2024 pm 03:35 PM

hibernate和mybatis的区别:1、实现方式;2、性能;3、对象管理的对比;4、缓存机制。详细介绍:1、实现方式,Hibernate是一个完整的对象/关系映射解决方案,将对象与数据库表进行映射,MyBatis则需要开发者手动编写SQL语句以及ResultMap;2、性能,Hibernate在开发速度上可能比MyBatis快,因为Hibernate简化了DAO层等等。

华为鸿蒙系统与安卓的关系研究华为鸿蒙系统与安卓的关系研究Mar 23, 2024 am 11:54 AM

华为鸿蒙系统与安卓的关系研究随着科技的不断发展,智能手机已经成为人们生活中不可或缺的一部分。而作为全球领先的手机制造商之一,华为一直在不断创新,致力于提供更好的手机操作系统和用户体验。近年来,随着美国对华为的打压,华为开始加速研发自主操作系统,于是鸿蒙系统(HarmonyOS)应运而生。在此背景下,人们开始关注起了鸿蒙系统与安卓之间的关系。首先,我们需要了解

在C语言中,评估(Evaluation)、优先级(Precedence)和关联(Association)是什么?在C语言中,评估(Evaluation)、优先级(Precedence)和关联(Association)是什么?Sep 03, 2023 pm 09:49 PM

“C”编译器根据优先级和关联性规则对表达式进行求值。如果表达式包含不同优先级运算符,则会考虑优先级规则。这里,首先评估10*2,因为'*'比'-'和'='具有更高的优先级如果表达式包含相同的优先级,则考虑关联性规则,即从左到右(或从右到左)。

Java Hibernate中一对多和多对多关系的映射方式是什么Java Hibernate中一对多和多对多关系的映射方式是什么May 27, 2023 pm 05:06 PM

Hibernate的一对多和多对多Hibernate是一个优秀的ORM框架,它简化了Java应用程序与关系型数据库之间的数据访问。在Hibernate中,我们可以使用一对多和多对多的关系来处理复杂的数据模型。Hibernate的一对多在Hibernate中,一对多关系是指一个实体类对应多个另一个实体类。比如,一个订单(Order)可以对应多个订单项(OrderItem),一个用户(User)可以对应多个订单(Order)。要在Hibernate中实现一对多关系,需要在实体类中定义一个集合属性来存

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.