【hibernate框架】一对多(多对一)双向关联(XML实现)

用户和组的关联,一个用户只能属于一个组,一个组可以拥有多个用户

User.java:

package cn.edu.hpu.one2many;


public class User {
	private int id;
	private String name;
	private Group group;
	
	public Group getGroup() {
		return group;
	}
	public void setGroup(Group group) {
		this.group = group;
	}


	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;
	}
	
}


Group.java:
package cn.edu.hpu.one2many;


import java.util.HashSet;
import java.util.Set;


public class Group {
	private int id;
	private String name;
	private Set<User> users=new HashSet<User>();
	//选择set的原因是因为,set互相之间不会有重复的
	//跟数据库模型比较匹配
	
	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;
	}
	public Set<User> getUsers() {
		return users;
	}
	public void setUsers(Set<User> users) {
		this.users = users;
	}
	
}


配置文件
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="cn.edu.hpu.one2many">
    <class name="User" table="m_user" dynamic-update="true">
    	<!-- 主键 这里的主键名字刚好也是'id'-->
			<id name="id" column="id">
			    <generator class="native"></generator>
			</id>  
		
        <property name="name"></property> 
        <!-- 两边的group配置的colum必须一样 -->
        <many-to-one name="group" column="groupId"></many-to-one>
    </class>
</hibernate-mapping>


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="cn.edu.hpu.many2one">
    <class name="Group" table="x_group" dynamic-update="true">
    	<!-- 主键 这里的主键名字刚好也是'id'-->
			<id name="id" column="id">
			    <generator class="native"></generator>
			</id>  
		
        <property name="name"></property>
        <set name="users">
           <key column="groupId"></key><!-- 指定关联字段的名字叫什么 -->
           <one-to-many class="cn.edu.hpu.one2many.User"/>
        </set>
    </class>
</hibernate-mapping>


在hibernate.cfg.xml中添加:
<mapping resource="cn/edu/hpu/many2many/Group.hbm.xml"/> 
<mapping resource="cn/edu/hpu/many2many/User.hbm.xml"/> 


生成的sql语句:
alter table m_user 
        drop 
        foreign key FKBF71E05DB11F7732


    drop table if exists m_group


    drop table if exists m_user


    create table m_group (
        id integer not null auto_increment,
        name varchar(255),
        primary key (id)
    )


    create table m_user (
        id integer not null auto_increment,
        name varchar(255),
        groupId integer,
        primary key (id)
    )


    alter table m_user 
        add index FKBF71E05DB11F7732 (groupId), 
        add constraint FKBF71E05DB11F7732 
        foreign key (groupId) 

        references m_group (id)


转载请注明出处:http://blog.csdn.net/acmman

你可能感兴趣的:(Hibernate,数据库,一对多多对一双向关联,XML实现)