Home Database Mysql Tutorial Hibernate关联关系映射(双向篇)

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

Jun 07, 2016 pm 02:51 PM
hibernate relation association mapping

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>
Copy after login

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>
Copy after login

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>
Copy after login
Copy after login

表结构:

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>
Copy after login

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>
Copy after login

测试类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>
Copy after login

测试结果:

这里写图片描述

(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>
Copy after login

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>
Copy after login
Copy after login

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

测试结果:

这里写图片描述

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

表结构:

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>
Copy after login

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>
Copy after login
Copy after login

测试类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>
Copy after login

测试结果:

这里写图片描述

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>
Copy after login
Copy after login

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>
Copy after login

表结构:

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>
Copy after login

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>
Copy after login

测试类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>
Copy after login

测试结果:

这里写图片描述

Statement of this Website
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

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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to integrate Hibernate in SpringBoot project How to integrate Hibernate in SpringBoot project May 18, 2023 am 09:49 AM

Integrating Hibernate in SpringBoot Project Preface Hibernate is a popular ORM (Object Relational Mapping) framework that can map Java objects to database tables to facilitate persistence operations. In the SpringBoot project, integrating Hibernate can help us perform database operations more easily. This article will introduce how to integrate Hibernate in the SpringBoot project and provide corresponding examples. 1.Introduce dependenciesIntroduce the following dependencies in the pom.xml file: org.springframework.bootspring-boot-starter-data-jpam

Selected Java JPA interview questions: Test your mastery of the persistence framework Selected Java JPA interview questions: Test your mastery of the persistence framework Feb 19, 2024 pm 09:12 PM

What is JPA? How is it different from JDBC? JPA (JavaPersistence API) is a standard interface for object-relational mapping (ORM), which allows Java developers to use familiar Java objects to operate databases without writing SQL queries directly against the database. JDBC (JavaDatabaseConnectivity) is Java's standard API for connecting to databases. It requires developers to use SQL statements to operate the database. JPA encapsulates JDBC, provides a more convenient and higher-level API for object-relational mapping, and simplifies data access operations. In JPA, what is an entity? entity

How to automatically associate MySQL foreign keys and primary keys? How to automatically associate MySQL foreign keys and primary keys? Mar 15, 2024 pm 12:54 PM

How to automatically associate MySQL foreign keys and primary keys? In the MySQL database, foreign keys and primary keys are very important concepts. They can help us establish relationships between different tables and ensure the integrity and consistency of the data. In actual application processes, it is often necessary to automatically associate foreign keys to the corresponding primary keys to avoid data inconsistencies. The following will introduce how to implement this function through specific code examples. First, we need to create two tables, one as the master table and the other as the slave table. Create in main table

Java Errors: Hibernate Errors, How to Handle and Avoid Java Errors: Hibernate Errors, How to Handle and Avoid Jun 25, 2023 am 09:09 AM

Java is an object-oriented programming language that is widely used in the field of software development. Hibernate is a popular Java persistence framework that provides a simple and efficient way to manage the persistence of Java objects. However, Hibernate errors are often encountered during the development process, and these errors may cause the program to terminate abnormally or become unstable. How to handle and avoid Hibernate errors has become a skill that Java developers must master. This article will introduce some common Hib

Explore the relationship between the Android system and the Linux kernel Explore the relationship between the Android system and the Linux kernel Mar 14, 2024 pm 12:48 PM

The Android system and the Linux kernel are two closely related entities, and the relationship between them is close and complex. In the Android system, the Linux kernel plays an important role, providing underlying hardware drivers and system call support for the Android system. This article will explore the relationship between the Android system and the Linux kernel, how they interact and work together, and provide some specific code examples. Android is a mobile operating system developed based on the Linux kernel and is mainly used for mobile devices such as smartphones and tablets. L

What are the differences between hibernate and mybatis What are the differences between hibernate and mybatis Jan 03, 2024 pm 03:35 PM

The differences between hibernate and mybatis: 1. Implementation method; 2. Performance; 3. Comparison of object management; 4. Caching mechanism. Detailed introduction: 1. Implementation method, Hibernate is a complete object/relational mapping solution that maps objects to database tables, while MyBatis requires developers to manually write SQL statements and ResultMap; 2. Performance, Hibernate is possible in terms of development speed Faster than MyBatis because Hibernate simplifies the DAO layer and so on.

Research on the relationship between Huawei Hongmeng system and Android Research on the relationship between Huawei Hongmeng system and Android Mar 23, 2024 am 11:54 AM

Research on the relationship between Huawei Hongmeng system and Android With the continuous development of technology, smartphones have become an indispensable part of people's lives. As one of the world's leading mobile phone manufacturers, Huawei has been constantly innovating and is committed to providing better mobile operating systems and user experiences. In recent years, with the United States' suppression of Huawei, Huawei has begun to accelerate the development of its own operating system, and HarmonyOS came into being. In this context, people have begun to pay attention to the relationship between Hongmeng System and Android. First, we need to understand

In C language, what are Evaluation, Precedence and Association? In C language, what are Evaluation, Precedence and Association? Sep 03, 2023 pm 09:49 PM

The "C" compiler evaluates expressions according to precedence and associativity rules. If an expression contains operators of different precedence, precedence rules are taken into account. Here, 10*2 is evaluated first because '*' has higher precedence than '-' and '='. If the expressions contain the same precedence, the associativity rule is considered, i.e. from left to right (or from right to the left).

See all articles