hibernate之關於使用串連表實現多對一關聯映射

來源:互聯網
上載者:User

標籤:many-to-one使用串連表實現關聯映射   採用串連表實現多對一關聯映射   


【Hibernate】之關於使用串連表實現多對一關聯映射


在我們項目使用中採用中間表最多的一般就是多對一,或者是多對多,當然一對一使用中間表也是可以的,但是這種幾率通常少之又少!所以這裡重點介紹多對一和一對多的採用中間表進行關聯映射!


依然採用Group和Person來描述這個邏輯!


Annotations配置


@Entity@Table(name="t_group")publicclass Group {    private Integer id;    private String name;    @Id    @GeneratedValue    public Integer getId() {       returnid;    }    publicvoid setId(Integer id) {       this.id = id;    }    @Column(name="g_name")    public String getName() {       returnname;    }    publicvoid setName(String name) {       this.name = name;    }}
@Entity@Table(name="p_person")publicclass Person {    private Integer id;    private String name;    private Integer age;    private Group group;    @ManyToOne    @JoinTable(name="g_p",joinColumns={@JoinColumn(name="p_id")},           inverseJoinColumns={@JoinColumn(name="g_id")})    public Group getGroup() {       returngroup;    }    publicvoid setGroup(Group group) {       this.group = group;    }    @Id    @GeneratedValue    public Integer getId() {       returnid;    }    publicvoid setId(Integer id) {       this.id = id;    }    @Column(name="p_name")    public String getName() {       returnname;    }    publicvoid setName(String name) {       this.name = name;    }    @Column(name="p_age")    public Integer getAge() {       returnage;    }    publicvoid setAge(Integer age) {       this.age = age;    }}


XML配置


<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC     "-//Hibernate/HibernateMapping DTD 3.0//EN"    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping package="csg.hibernate.entity">    <class name="Person" table="t_person">       <id name="id">           <column name="id"/>           <generator class="native" />       </id>       <property name="name" />       <property name="age" />       <join table="t_p" optional="true">           <key column="p_id"></key>           <many-to-one name="group" column="g_id" class="Group" unique="true"/>       </join>    </class></hibernate-mapping>
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC     "-//Hibernate/HibernateMapping 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">           <column name="id"/>           <generator class="native" />       </id>       <property name="name" />    </class></hibernate-mapping>


寫這篇文章,我特意查詢了一下網上的文章,發現大家都是採用XML配置的,所以我這裡也寫了Annotations配置,因為JPA中的Annotations使用起來遠比XML要方便!OK?



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

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.