Hibernate学习之----------多对一与一对多

 

在hibernate中经常遇到用户组与用户关系的映射,在这里其实是一对多或多对一的关系,这个用户组与用户可以用下面语句描述:

一个用户组可包含很多用户,但一个用户只能属于一个用户组

 

先来讨论在用户表里嵌入用户组id的情况:

上代码(采用注解,后面有xml配置说明):

package suse.ly.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * 用户组类
 * @author ly-suse
 *
 */
@Entity
@Table(name="t_group")
public class Group {

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

 用户类;

package suse.ly.domain;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/**
 * 用户类
 * @author ly-suse
 *
 */
@Entity
@Table(name="t_user")
public class User {

	private int id;
	
	private String name;

	private Group group;
	
	@ManyToOne(fetch=FetchType.LAZY)  //(多对一,每个用户组可以包含多个用户)
	public Group getGroup() {
		return group;
	}

	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}

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

 

 

然后运行测试:

new SchemaExport(new AnnotationConfiguration().configure()).create(true, true);

 

运行结果:

alter table t_user drop foreign key FKCB63CCB6C41AE897
drop table if exists t_group
drop table if exists t_user
create table t_group (id integer not null auto_increment, name varchar(255), primary key (id))
create table t_user (id integer not null auto_increment, name varchar(255), group_id integer, primary key (id))
alter table t_user add index FKCB63CCB6C41AE897 (group_id), add constraint FKCB63CCB6C41AE897 foreign key (group_id) references t_group (id)

从结果可以看出t_user表里含有group_id这个外键

 

xml配置如下:

Group.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
        <hibernate-mapping>
        	<class name="suse.ly.domain.Group" >
        		<id>
        			<generator class="native"></generator>
        		</id>
        		
        		<property name="name"></property>
        	</class>
        </hibernate-mapping>

 User.hbm.xml

 

 

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
        <hibernate-mapping>
        	<class name="suse.ly.domain.User" >
        		<id>
        			<generator class="native"></generator>
        		</id>
        		
        		<many-to-one name="group" column="groupId"></many-to-one>
        	</class>
        </hibernate-mapping>

 运行结果如上一样。

 

 

你可能感兴趣的:(Hibernate)