Hibernate之關於一對多,多對一雙向關聯映射

來源:互聯網
上載者:User

標籤:多對一雙向關聯映射   一對多雙向關聯映射   



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


由於一對多,和多對一的雙向關聯映射基本上一樣,所以這裡就一起寫下來!

Annotations配置

@Entity@Table(name="t_group")publicclass Group {    private Integer id;    private String name;    private Set<Person> persons=newHashSet<Person>();//set不允許重複,最適合資料庫模型    @Id    @GeneratedValue    public Integer getId() {       returnid;    }    publicvoid setId(Integerid) {       this.id = id;    }    @Column(name="t_name")    public String getName() {       returnname;    }    publicvoid setName(Stringname) {       this.name = name;    }    @OneToMany(mappedBy="group")//意識是告訴Hibernate的關聯關係應該設在Person,多的一方//  只有OneToOne,OneToMany,ManyToMany上才有mappedBy屬性,ManyToOne不存在該屬性;    public Set<Person>getPersons() {       returnpersons;    }    publicvoidsetPersons(Set<Person> persons) {       this.persons = persons;    }}

@Entity@Table(name="t_person")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="p_name")    public String getName() {       returnname;    }    publicvoid setName(Stringname) {       this.name = name;    }    @Column(name="p_age")    public Integer getAge() {       returnage;    }    publicvoid setAge(Integerage) {       this.age = age;    }}

XML配置


<?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">           <column name="id"/>           <generator class="native" />       </id>       <property name="name" />       <set name="persons">           <key column="groupId"/>           <one-to-many class="csg.hibernate.entity.Person"/>       </set>    </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="Person" table="t_person">       <id name="id">           <column name="id"/>           <generator class="native" />       </id>       <property name="name" />       <property name="age" />       <many-to-one name="group" column="groupId"/>    </class></hibernate-mapping>

由上大家都可以得出疑問?

為什麼雙向關聯在Group中key需要指定column而在Person中也需要指定column呢?

如果其中一個不指定會有什麼後果?

如果指定的column不一致又會有什麼後果?

大家可以試一下!





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.