Hibernate 再接觸 一對多單向關聯

來源:互聯網
上載者:User

標籤:ash   enc   source   hibernate   ber   sts   sch   tab   pack   

在1的方向加多的集合

Group.java

package com.bjsxt.hibernate;import java.util.HashSet;import java.util.Set;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.OneToMany;import javax.persistence.Table;@Entity@Table(name="t_group")public class Group {    private int id;    private String name;    private Set<User> users = new HashSet<User>();    @Id    @GeneratedValue    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    @OneToMany    @JoinColumn(name="groupId")    //不寫這句話會產生中間表    public Set<User> getUsers() {        return users;    }    public void setUsers(Set<User> users) {        this.users = users;    }}
package com.bjsxt.hibernate;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.ManyToOne;import javax.persistence.Table;@Entity@Table(name="t_user")public class User {    private int id;    private String name;            @Id    @GeneratedValue    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }}

User.hbm.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>    <class name="com.bjsxt.hibernate.User" table="t_user">        <id name="id">            <generator class="native"></generator>        </id>                <property name="name"></property>            </class>    </hibernate-mapping>

Group.hbm.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>    <class name="com.bjsxt.hibernate.Group" table="t_group">        <id name="id">            <generator class="native"></generator>        </id>                <property name="name"></property>        <set name="users">            <key column="groupId"></key>            <one-to-many class="com.bjsxt.hibernate.User"/>        </set>    </class>    </hibernate-mapping>
@Test    public void testSchemaExport() {        new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);    }

 

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.