Hibernate学习笔记 3 - 关系映射配置(基于注解)

 

Hibernate学习笔记 3 - 关系映射配置(基于注解)
为了讲解本功能,创建了以下五张表

--角色
create table Roles
(
 id int identity(1,1) primary key,
    name varchar(20)    
)
go
insert into Roles values ('管理员')
insert into Roles values ('普通用户')
go
select * from Roles
go

--用户
create table Users
(
 id int identity primary key,
 name varchar(20),
 roleid int references roles(id)
)
go
insert into Users values ('haha',1)
go
select * from Users

--学生
create table Stud(
 id int identity primary key,
 name varchar(20)
)
go
insert into Stud values ('张三')
insert into Stud values ('李四')
insert into Stud values ('王五')
select * from stud
--课程
create table Course(
 id int identity primary key,
 name varchar(20) 
)
go
insert into course values ('JAVA')
insert into Course values ('C#')
insert into Course values ('JSP')
select * from Course

--选课
create table CourseSel(
 id int identity primary key,
 studid int references Stud(id),
 courseid int references Course(id)
)
go

insert into CourseSel values (1,1)
insert into CourseSel values (1,2)
insert into CourseSel values (2,2)
insert into CourseSel values (2,3)
insert into CourseSel values (3,1)
insert into CourseSel values (3,3)
go


 

多对一关系(在用户对象中保存一个角色对象)
Roles.java

@Entity(name="Roles")//注解表名
public class Roles implements Serializable {
	@Id //注解主键
	@GeneratedValue(strategy=GenerationType.IDENTITY) //注解主键生成方式
	private int id;
	private String name;
	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;
	}
}

Users.java

@Entity(name="Users")//注解表名
public class Users implements Serializable {
	@Id//注解主键
	@GeneratedValue(strategy=GenerationType.IDENTITY)//注解主键生成方式
	private int id;
	private String name;
	@ManyToOne(targetEntity=Roles.class)//注解多对一	
	@JoinColumn(name="roleid")//注解外键
	private Roles role;
	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 Roles getRole() {
		return role;
	}
	public void setRole(Roles role) {
		this.role = role;
	}
}

@ManyToOne用于配置多对一设置说明
@ManyToOne(targetEntity=目标类)
@JoinColumn(name="外键")


@ManyToOne(fetch=FetchType,cascade=CascadeType)
可选
@ManyToOne表示一个多对一的映射,该注解标注的属性通常是数据库表的外键
optional:是否允许该字段为null,该属性应该根据数据库表的外键约束来确定,默认为true
fetch:表示抓取策略,默认为FetchType.EAGER
cascade:表示默认的级联操作策略,可以指定为ALL,PERSIST,MERGE,REFRESH和REMOVE中的若干组合,默认为无级联操作

@JoinColumn
可选
@JoinColumn和@Column类似,介量描述的不是一个简单字段,而一一个关联字段,例如.描述一个@ManyToOne的字段.
name:该字段的名称.由于@JoinColumn描述的是一个关联字段,如ManyToOne,则默认的名称由其关联的实体决定.


一对多关系(一个角色有多个用户)
Roles.java

@Entity(name="Roles")//注解表名
public class Roles implements Serializable {
	@Id //注解主键
	@GeneratedValue(strategy=GenerationType.IDENTITY) //注解主键生成方式
	private int id;
	private String name;
	
	@OneToMany(targetEntity=Users.class,mappedBy="id")	
	@ForeignKey(name="roleid")
	private List<Users> users=new ArrayList<Users>();
	
	
	public List<Users> getUsers() {
		return users;
	}
	
	
	public void setUsers(List<Users> users) {
		this.users = users;
	}
	
	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;
	}
}


 Users.java

@Entity(name="Users")//注解表名
public class Users implements Serializable {
	@Id //注解主键
	@GeneratedValue(strategy=GenerationType.IDENTITY) //注解主键生成方式
	private int id;
	private String name;
	private int roleid;
	public int getRoleid() {
		return roleid;
	}
	public void setRoleid(int roleid) {
		this.roleid = roleid;
	}
	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用于设置一对多
配置说明

@OneToMany(targetEntity=目标类,mappedBy="目标类的主键") 
@ForeignKey(name="外键")


多对多关系(学生和课程之间有多对多关系)
Course.java

@Entity(name="Course")
public class Course implements Serializable {
	@Id //注解主键
	@GeneratedValue(strategy=GenerationType.IDENTITY) //注解主键生成方式
	private int id;
	private String name;
	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;
	}	
}	


CourseSel.java

public class CourseSel implements Serializable {
	@Id //注解主键
	@GeneratedValue(strategy=GenerationType.IDENTITY) //注解主键生成方式
	private int id;
	private int studid;
	private int courseid;
	public int getStudid() {
		return studid;
	}
	public void setStudid(int studid) {
		this.studid = studid;
	}
	public int getCourseid() {
		return courseid;
	}
	public void setCourseid(int courseid) {
		this.courseid = courseid;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}	
}

Stud.java

@Entity(name="Stud")
public class Stud implements Serializable {
	@Id //注解主键
	@GeneratedValue(strategy=GenerationType.IDENTITY) //注解主键生成方式
	private int id;
	private String name;
	@ManyToMany(targetEntity=Course.class)
	@JoinTable(
			name="dbo.CourseSel",
			joinColumns=@JoinColumn(name="courseid"),
			inverseJoinColumns=@JoinColumn(name="studid")
	)	
	private Set<Course> courses=new HashSet<Course>();
	public Set<Course> getCourses() {
		return courses;
	}
	public void setCourses(Set<Course> courses) {
		this.courses = courses;
	}
	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;
	}	
}

 

@ManyToMany用于配置多对多关系
说明:
@ManyToMany(targetEntity=目标类)
@JoinTable(
name="关系表",
joinColumns=@JoinColumn(name="从表外键"),
inverseJoinColumns=@JoinColumn(name="主表外键")
)

你可能感兴趣的:(Hibernate,数据库,String,table,Class,insert)