当前位置: 代码迷 >> XML/SOAP >> 一对多(多对一)双向联系关系(annotation&xml)
  详细解决方案

一对多(多对一)双向联系关系(annotation&xml)

热度:912   发布时间:2014-03-01 00:37:44.0
一对多(多对一)双向关联(annotation&xml)
annotation方式:
一、编写一方类
@Entity
public class Group {
	private int id;
	private String name;
	private Set<User> users =new HashSet<User>();
	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}
	@OneToMany(mappedBy="group")
	public Set<User> getUsers() {
		return users;
	}
	public void setUsers(Set<User> users) {
		this.users = users;
	}
	public String getName() {
		return name;
	}
	public void setId(int id) {
		this.id = id;
	}
	public void setName(String name) {
		this.name = name;
	}
}

针对本类来讲@OneToMany(mappedBy="group"),mappedBy是设置对方即多方的属性(不是自己的属性)为主导

二、编写多方的类
@Entity
public class User {
	private int id;
	private String name;
	private Group group;

	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}
	@ManyToOne
	public Group getGroup() {
		return group;
	}
	public String getName() {
		return name;
	}
	public void setGroup(Group group) {
		this.group = group;
	}
	public void setId(int id) {
		this.id = id;
	}
	public void setName(String name) {
		this.name = name;
	}
}

针对本类来讲@ManyToOne

三、设置hibernate.cfg.xml
<mapping class="org.hibernate.tutorial.domain.Group"/>
		<mapping class="org.hibernate.tutorial.domain.User"/>


四、测试结果
    create table Group (
        group_Id integer not null auto_increment,
        name varchar(255),
        primary key (group_Id)
    )

    create table User (
        User_ID integer not null auto_increment,
        name varchar(255),
        groupID integer,
        primary key (User_ID)
    )

    alter table User 
        add index FK_k6brud1jwhinw4g4f6yafcqjm (groupID), 
        add constraint FK_k6brud1jwhinw4g4f6yafcqjm 
        foreign key (groupID) 
        references Group (group_Id)

注意:双向必设置mappedby,减少冗余字段,且以对方属性为主导


xml方式:
一、编写如上两个实体类
二、配置一方Group.hbm.xml
<?xml version="1.0"?> 
<!DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping package="org.hibernate.tutorial.domain"> 

    <class name="Group" table="Group"> 
        <id name="id" column="group_Id"> 
            <generator class="native"/> 
        </id> 
        <property name="name"></property>
        <set name="users">
        	<key column="groupID"></key>
        	<one-to-many class="org.hibernate.tutorial.domain.User" />
        </set>
    </class> 

</hibernate-mapping>

配置本类集合属性users,以groupID为多方外键列名(双向:必与一方配置列名一致),配置多方类

三、配置多方User.hbm.xml
<?xml version="1.0"?> 
<!DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping package="org.hibernate.tutorial.domain"> 

    <class name="User" table="User"> 
        <id name="id" column="User_ID"> 
            <generator class="native"/> 
        </id> 
        <property name="name"></property>
        <many-to-one name="group" column="groupID"></many-to-one>
    </class> 

</hibernate-mapping>

配置本类属性group,以groupID为本类(多方)外键列名(必必一方配置列名一致)

四、设置hibernate.cfg.xml
<mapping resource="org/hibernate/tutorial/domain/Group.hbm.xml"/>
		<mapping resource="org/hibernate/tutorial/domain/User.hbm.xml"/>


五、测试如上
  相关解决方案