hibernate之關於多對一單向關聯映射

來源:互聯網
上載者:User

標籤:多對一單向關聯映射   many-to-one單向關聯映射   



【Hibernate】之關於多對一單向關聯映射



 在項目的開發中多對一的單向關聯映射是最常見的,關聯映射!這個著重詳細講解一下!

例如,我們現在一個組(Group)和人(Person)


                                       (Person表)

id name
1 張三
2 李四

                                      (Group表)

id name p_id
1
財務組 1
2 財務組 2


由上表我們發現,是不是出現資料的重複?財務組在重複!

所以,在多對一的單向映射中,我們通常是在多的一方加上外鍵來關聯少的一方。

那麼在這個關係中(Group)是少的一方,(Person)是多的一方,一個組裡面可以有多個人!

                                         (Group表)

id name
1 財務組
2 開發組

                                        (Person表)

id name g_id
1
張三 1
2 李四 1


上面這種關係才是正常的商務邏輯!


ok,下面來看我們的Annotations配置!

@Entity@Table(name="t_user")publicclass Person {    private Integer id;      private String name;    private Integer age;    private Group group;        @ManyToOne    public Group getGroup() {       returngroup;    }    publicvoid setGroup(Groupgroup) {       this.group = group;    }    @Id    @GeneratedValue    public Integer getId() {       returnid;    }    publicvoid setId(Integerid) {       this.id = id;    }    @Column(name="name")    public String getName() {       returnname;    }    publicvoid setName(Stringname) {       this.name = name;    }    @Column(name="age")    public Integer getAge() {        returnage;    }    publicvoid setAge(Integerage) {       this.age = age;    }}
@Entity@Table(name="t_group")publicclass Group {    private Integer id;    private String name;        @Id    @GeneratedValue    public Integer getId() {       returnid;    }    publicvoid setId(Integerid) {       this.id = id;    }    @Column(name="name")    public String getName() {       returnname;    }    publicvoid setName(Stringname) {       this.name = name;    }}


XML配置方法


package csg.hibernate.entity; publicclass Person {    private Integer id;    private String name;    private Integer age;    private Group group;        public Group getGroup() {       returngroup;    }    publicvoid setGroup(Groupgroup) {       this.group = group;    }    public Integer getId() {       returnid;    }    publicvoid setId(Integerid) {       this.id = id;    }    public String getName() {       returnname;    }    publicvoid setName(Stringname) {       this.name = name;    }    public Integer getAge() {       returnage;    }    publicvoid setAge(Integerage) {       this.age = age;    }}
package csg.hibernate.entity; publicclass Group {    private Integer id;    private String name;        public Integer getId() {       returnid;    }    publicvoid setId(Integerid) {       this.id = id;    }    public String getName() {       returnname;    }    publicvoid setName(Stringname) {       this.name = name;    }}
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping package="csg.hibernate.entity">    <class name="Person" table="t_user">       <id name="id">           <generator class="native" />       </id>       <property name="name" />       <property name="age"/>       <many-to-one name="Group" column="group_id"/>    </class></hibernate-mapping>
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping package="csg.hibernate.entity">    <class name="Group" table="t_group">       <id name="id">           <generator class="native"/>       </id>       <property name="name" />    </class></hibernate-mapping>


到這裡我們使用Junit測試一下


publicclass JuniTest {privatestatic SessionFactory sessionFactory;        @BeforeClass    publicstaticvoid beforeClass() {       Configuration cfg=new Configuration();       cfg.configure();       sessionFactory=cfg.buildSessionFactory();    }       @Test    public  void add(){       try {           Group group=new Group();           Session session = sessionFactory.getCurrentSession();           session.beginTransaction();           group.setName("部門");           session.save(group);           Person person=new Person();           person.setAge(12);           person.setName("張三");           person.setGroup(group);           session.save(person);           session.getTransaction().commit();       } catch (HibernateException e){           e.printStackTrace();       }           }        @AfterClass    publicstaticvoid afterClass() {       sessionFactory.close();    }}


ok!



本文出自 “諾言永遠依戀小柴、、、” 部落格,請務必保留此出處http://1936625305.blog.51cto.com/6410597/1568855

hibernate之關於多對一單向關聯映射

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.