Heim > Datenbank > MySQL-Tutorial > Hibernate关联关系映射(双向篇)

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

WBOY
Freigeben: 2016-06-07 14:51:18
Original
1024 Leute haben es durchsucht

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>
Nach dem Login kopieren

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>
Nach dem Login kopieren

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>
Nach dem Login kopieren
Nach dem Login kopieren

表结构:

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>
Nach dem Login kopieren

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>
Nach dem Login kopieren

测试类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>
Nach dem Login kopieren

测试结果:

这里写图片描述

(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>
Nach dem Login kopieren

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>
Nach dem Login kopieren
Nach dem Login kopieren

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

测试结果:

这里写图片描述

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>
Nach dem Login kopieren
Nach dem Login kopieren

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>
Nach dem Login kopieren
Nach dem Login kopieren

表结构:

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>
Nach dem Login kopieren

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>
Nach dem Login kopieren
Nach dem Login kopieren

测试类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>
Nach dem Login kopieren

测试结果:

这里写图片描述

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>
Nach dem Login kopieren
Nach dem Login kopieren

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>
Nach dem Login kopieren

表结构:

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>
Nach dem Login kopieren

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>
Nach dem Login kopieren

测试类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>
Nach dem Login kopieren

测试结果:

这里写图片描述

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage