[Hibernate]注解/关系映射以及关系测试用例

基本注解

package OneToOneSingle;

import java.io.Serializable;

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

/*#################################################################
 ##   类级别注解:                                                                                                #####
 ##  @Entity:映射实体类,使用此属性必须指定主键属性                                           #####
 ##  @Entity(name="tableName"),name可选,可以指定对应数据库里对应表的名称  #####
 ##   属性级别注解:                                                                                             #####
 ##  @Id 指定主键,在主键的get方法上使用                                                           #####
 ##   一个实体类可以有一个或者多个属性被映射成主键,                                         #####
 ##   但是如果有多个主键属性,该实体类必须实现serializble接口                             #####
 ##   @GeneratedValue(strategy=Type,generator="")主键生成策略                      #####
 ##   Type:生成策略                                                                                           #####
 ##    默认为GenerationType.AUTO根据底层数据库自动选择,String类型不能使用此类型#      
 ##    GenerationType.IDENTITY根据数据库Identity字段生成                                  #####
 ##    GenerationType.SEQUENCE使用sequence来决定主键取值                             #####
 ##    GenerationType.TABLE使用指定表类决定主键取值,结合@TableGenerator使用 #####
 ##   @Column 描述数据库表中该字段的详细定义                                                    #####
 ##       name:不一致就手工设置                                                                             #####
  ##      nullable:是否为空,unique是否唯一,length长度,仅对String有效               #####
  ##    @EmbeddedId复合主键 也是要实现serializble接口                                       #####
 ##################################################################*/

@Entity
// #@Entity(name="tableName")也可
public class User implements Serializable{

	private int userid;
	@Id
	@GeneratedValue//mysql底层是自动增长
	private int userUUID;
	private String username;
	private String password;
	// private UserInfo userinfo;

	public User() {// 符合规范
		// 无参构造方法
	}

	@Id
	@GeneratedValue//mysql底层是自动增长
	public int getUserid() {
		return userid;
	}

	public void setUserid(int userid) {
		this.userid = userid;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getUserUUID() {
		return userUUID;
	}

	public void setUserUUID(int userUUID) {
		this.userUUID = userUUID;
	}

	// public UserInfo getUserinfo() {
	// return userinfo;
	// }
	//
	// public void setUserinfo(UserInfo userinfo) {
	// this.userinfo = userinfo;
	// }

}

package OneToOneSingle;

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

/*#################################################################
##   类级别注解:(此处有jar包冲突……无法测试OTZ)                                             #####
##   @Table表示实体对应数据库表的信息                                                               #####
##   @Table(name="",catalog="",scheme=""                                                       #####
##   name:表名 只有和实体名不一致的时候才指定                                                   #####
##   catalog目录:  mysql不支持                                                                          #####
##   scheme格式:写数据库名称                                                                          #####
##################################################################*/
@Entity(name = "t_UserInfo")
public class UserInfo {
	private int id;
	//嵌入类
	private Address address;
	private String tel;
	private String mail;

	public UserInfo() {
	}

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

	public void setId(int id) {
		this.id = id;
	}



	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public String getMail() {
		return mail;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}

}
package OneToOneSingle;

import javax.persistence.Embeddable;

//是一个嵌入类,并不需要生成表结构

/*##############################################
 ##   @Embeddable是类级别注解也是属性级别注解   ####
##     它会把这个类的字段嵌入到指定实体类里     #####
#################################################


import javax.persistence.Embeddable;


//是一个嵌入类,并不需要生成表结构


/*#########################################
 ##   @Embeddable是类级别注解也是属性级别注解     ####
##     它会把这个类的字段嵌入到指定实体类里             #####
############################################
 */
@Embeddable
public class Address {
private String AddPost;
private String AddForhead;


public Address(){}


public String getAddPost() {
	return AddPost;
}


public void setAddPost(String addPost) {
	AddPost = addPost;
}


public String getAddForhead() {
	return AddForhead;
}


public void setAddForhead(String addForhead) {
	AddForhead = addForhead;
}
}
 */
@Embeddable
public class Address {
private String AddPost;
private String AddForhead;

public Address(){}

public String getAddPost() {
	return AddPost;
}

public void setAddPost(String addPost) {
	AddPost = addPost;
}

public String getAddForhead() {
	return AddForhead;
}

public void setAddForhead(String addForhead) {
	AddForhead = addForhead;
}
}

一对一单向外键关联

@OneToOne(cascade=CascadeType.ALL)

【CascadeType】级联关系

@JoinColumn(name="pid",unique=true)

package OneToOneSingle;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;

//主控类
@Entity
public class Student {
	private int sid;
	private String major;
	private StuIdCard idcard;

	public Student() {
	}


	public Student(String major, StuIdCard idcard) {
		super();
		this.major = major;
		this.idcard = idcard;
	}


	@Id
	@GeneratedValue
	public int getSid() {
		return sid;
	}

	public void setSid(int sid) {
		this.sid = sid;
	}
	
	
   @OneToOne(cascade=CascadeType.ALL)
   @JoinColumn(name="pid",unique=true)//被控类对应的主键
	public StuIdCard getIdcard() {
		return idcard;
	}

	public void setIdcard(StuIdCard idcard) {
		this.idcard = idcard;
	}

	public String getMajor() {
		return major;
	}

	public void setMajor(String major) {
		this.major = major;
	}

}
package OneToOneSingle;

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

import org.hibernate.annotations.GenericGenerator;

//被控类
@Entity
public class StuIdCard {
	private String pid;
	private String sname;

	public StuIdCard() {
	}

	public StuIdCard(String pid, String sname) {
		super();
		this.pid = pid;
		this.sname = sname;
	}

	@Id
	@GeneratedValue(generator = "pid")
	@GenericGenerator(name = "pid", strategy = "assigned")
	@Column(length = 18) // 因为主键是字符串类型
	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	public String getSname() {
		return sname;
	}

	public void setSname(String sname) {
		this.sname = sname;
	}

}

一对一双向外键关联

单向就是主控方里有被控方的类的引用,双向就是双方都存在对方,但是所有关联都只有一个被控方,被控方就必须有

以及如果注解在private上,所有都要保持写在private上,如果你都写在get方法上,同理。

@OneToOne(mappedBy="idcard")//此处的idcard为在主控方里被控方的名字,而不是该类
package OneToOneDouble;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;

//主控类
@Entity(name="t_student")
public class Student {
	private int sid;
	private String major;
	private StuIdCard idcard;

	public Student() {
	}


	public Student(String major, StuIdCard idcard) {
		super();
		this.major = major;
		this.idcard = idcard;
	}


	@Id
	@GeneratedValue
	public int getSid() {
		return sid;
	}

	public void setSid(int sid) {
		this.sid = sid;
	}
	
	
   @OneToOne(cascade=CascadeType.ALL)
   @JoinColumn(name="pid",unique=true)//主控类的外键名,同时是被控类对应的主键名
	public StuIdCard getIdcard() {
		return idcard;
	}

	public void setIdcard(StuIdCard idcard) {
		this.idcard = idcard;
	}

	public String getMajor() {
		return major;
	}

	public void setMajor(String major) {
		this.major = major;
	}

}
package OneToOneDouble;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;

import org.hibernate.annotations.GenericGenerator;

//被控类
@Entity(name="t_cart")
public class StuIdCard {
	private String pid;
	private String sname;
	
	private Student student;

	public StuIdCard() {
	}

	public StuIdCard(String pid, String sname) {
		super();
		this.pid = pid;
		this.sname = sname;
	}

	@Id
	@GeneratedValue(generator = "pid")
	@GenericGenerator(name = "pid", strategy = "assigned")
	@Column(length = 18) // 因为主键是字符串类型
	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	@OneToOne(mappedBy="idcard")
	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	public String getSname() {
		return sname;
	}

	public void setSname(String sname) {
		this.sname = sname;
	}

}

多对一单向外键关联

多方持有一方的引用,比如 多个学生对应一个班级

@ManyToOne(cascade={CascadeType.ALL},fetch=FetchType.EAGER)//集成关系和抓取策略

@JoinColum(name="cid",referencedColumnName="CID")//主控类的外键名,可修改为CID

package ManyToOneSingle;

import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
public class Students {

	private int sid;
	private String major;
	private Date birthday;
	private String gender;
	private Class_ class_num;// 多方持有一方的引用

	public Students() {
	}

	public Students(String major, Date birthday, String gender, Class_ class_) {
		super();
		this.major = major;
		this.birthday = birthday;
		this.gender = gender;
		this.class_num = class_;
	}

	@Id
	@GeneratedValue
	public int getSid() {
		return sid;
	}

	public void setSid(int sid) {
		this.sid = sid;
	}

	public String getMajor() {
		return major;
	}

	public void setMajor(String major) {
		this.major = major;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	@ManyToOne(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
	@JoinColumn(name = "cid", referencedColumnName = "CID") // 给指定外键起个别名
	public Class_ getClass_num() {
		return class_num;
	}

	public void setClass_num(Class_ class_num) {
		this.class_num = class_num;
	}

}
package ManyToOneSingle;

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

import org.hibernate.annotations.GenericGenerator;

@Entity
public class Class_ {

	private String cid;
	private String cname;

	@Id
	@GeneratedValue(generator = "cid")
	@GenericGenerator(name = "cid", strategy = "assigned")
	@Column(length = 4)
	public String getCid() {
		return cid;
	}

	public Class_() {
	}

	public Class_(String cid, String cname) {
		super();
		this.cid = cid;
		this.cname = cname;
	}

	public void setCid(String cid) {
		this.cid = cid;
	}

	public String getCname() {
		return cname;
	}

	public void setCname(String cname) {
		this.cname = cname;
	}

}

一对多单向外键关联

一方持有多方的集合(仅此而已) 如 一个班级有多个学生(一对多)

@OneToMany(cascade={CascadeType.ALL},fetch=FetchType.LAZZ)用于一方,一般是懒加载

@JoinColumn(name="cid")//指定外键名,也是写在一方里,是对一方里对多方集合的注解,这个外键是一方的主键名(可以不是,但是别的成员名也要一致)

package OneToManyDouble;

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

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;

import org.hibernate.annotations.GenericGenerator;

@Entity
public class Class_room {

	private String cid;
	private String cname;
	private Set<Students_D> students;

	@Id
	@GeneratedValue(generator = "cid")
	@GenericGenerator(name = "cid", strategy = "assigned")
	@Column(length = 4)
	public String getCid() {
		return cid;
	}

	@OneToMany(cascade={CascadeType.ALL},fetch=FetchType.LAZY)
	@JoinColumn(name="cid")
	public Set<Students_D> getStudents() {
		return students;
	}

	public void setStudents(Set<Students_D> students) {
		this.students = students;
	}

	public Class_room() {
	}

	public Class_room(String cid, String cname) {
		super();
		this.cid = cid;
		this.cname = cname;
	}

	public void setCid(String cid) {
		this.cid = cid;
	}

	public String getCname() {
		return cname;
	}

	public void setCname(String cname) {
		this.cname = cname;
	}

}
package OneToManyDouble;

import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
public class Students_D {

	private int sid;
	private String major;
	private Date birthday;
	private String gender;

	public Students_D() {
	}

	public Students_D(String major, Date birthday, String gender) {
		super();
		this.major = major;
		this.birthday = birthday;
		this.gender = gender;
	}

	@Id
	@GeneratedValue
	public int getSid() {
		return sid;
	}

	public void setSid(int sid) {
		this.sid = sid;
	}

	public String getMajor() {
		return major;
	}

	public void setMajor(String major) {
		this.major = major;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

}

一对多(多对一)双向外键关联

多方:持有一方的引用

@ManyToOne(cascade={CascadeType.ALL},fetch=FetchType.EAGER)
@JoinColumn(name="cid")

一方:持有多方的集合

@OneToMany(cascade={CascadeType.ALL},fetch=FetchType.LAZY)
@JoinColumn(name="cid")

package OneToManyDouble;

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;

import org.hibernate.annotations.GenericGenerator;

import OneToManySingle.Students_D;

@Entity
public class Class_double {

	private String cid;
	private String cname;
	private Set<Students_double> students;

	@Id
	@GeneratedValue(generator = "cid")
	@GenericGenerator(name = "cid", strategy = "assigned")
	@Column(length = 4)
	public String getCid() {
		return cid;
	}

	public Class_double() {
	}

	@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
	@JoinColumn(name = "cid")
	public Set<Students_double> getStudents() {
		return students;
	}

	public void setStudents(Set<Students_double> students) {
		this.students = students;
	}

	public Class_double(String cid, String cname) {
		super();
		this.cid = cid;
		this.cname = cname;
	}

	public void setCid(String cid) {
		this.cid = cid;
	}

	public String getCname() {
		return cname;
	}

	public void setCname(String cname) {
		this.cname = cname;
	}

}
package OneToManyDouble;

import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
public class Students_double {

	private int sid;
	private String major;
	private Date birthday;
	private String gender;
	private Class_double classes;

	public Students_double() {
	}

	public Students_double(String major, Date birthday, String gender) {
		super();
		this.major = major;
		this.birthday = birthday;
		this.gender = gender;
	}

	@ManyToOne(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
	@JoinColumn(name = "cid")
	public Class_double getClasses() {
		return classes;
	}

	public void setClasses(Class_double classes) {
		this.classes = classes;
	}

	@Id
	@GeneratedValue
	public int getSid() {
		return sid;
	}

	public void setSid(int sid) {
		this.sid = sid;
	}

	public String getMajor() {
		return major;
	}

	public void setMajor(String major) {
		this.major = major;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

}

多对多单向外键关联

学生和教师构成多对多的关联关系

其中一个多方持有另一个多方的集合关系

此关联关系的实质是有一个中间表

@ManyToMany

@JoinTable(//中间表

name="teacher_student",//中间表名

joinColumns={@JoinColumn(name="sid")},//学生表的外键名

inverseJoinColumns={@JoinColumn(name="tid")})//教师表的外键名
package ManyToManySingle;

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

import org.hibernate.annotations.GenericGenerator;

@Entity
public class Teachers {
	private String tid;
	private String tname;

	public Teachers() {
	}

	public Teachers(String tid, String tname) {
		super();
		this.tid = tid;
		this.tname = tname;
	}

	@Id
	@GeneratedValue(generator = "tid")
	@GenericGenerator(name = "tid", strategy = "assigned")
	@Column(length = 4)
	public String getTid() {
		return tid;
	}

	public void setTid(String tid) {
		this.tid = tid;
	}

	public String getTname() {
		return tname;
	}

	public void setTname(String tname) {
		this.tname = tname;
	}

}
package ManyToManySingle;

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;

//主控类
@Entity
public class Students_t {
	private int sid;
	private String sname;
	private String major;
	private Set<Teachers> teachers;

	public Students_t() {
	}

	public Students_t(String sname, String major) {
		super();
		this.sname = sname;
		this.major = major;
	}

	public String getSname() {
		return sname;
	}

	public void setSname(String sname) {
		this.sname = sname;
	}

	@ManyToMany
	@JoinTable(name = "teacher_student", joinColumns = { @JoinColumn(name = "sid") }, inverseJoinColumns = {
			@JoinColumn(name = "tid") })
	public Set<Teachers> getTeachers() {
		return teachers;
	}

	public void setTeachers(Set<Teachers> teachers) {
		this.teachers = teachers;
	}

	@Id
	@GeneratedValue
	public int getSid() {
		return sid;
	}

	public void setSid(int sid) {
		this.sid = sid;
	}

	public String getMajor() {
		return major;
	}

	public void setMajor(String major) {
		this.major = major;
	}

}

多对多双向外键关联

双方都持有对方的集合对象

主控方

@ManyToMany(mappedBy="teachers")

被控方

@ManyToMany

@JoinTable(//中间表

name="teacher_student",//中间表名

joinColumns={@JoinColumn(name="本类里的")},

inverseJoinColumns={@JoinColumn(name="关联类里的")})

package ManyToManyDouble;

import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;

@Entity
public class People {

	private int peopleId;
	private String peopleName;
	private Set<Role> roles;

	public People() {
	}

	public People(String peopleName) {
		super();
		this.peopleName = peopleName;
	}

	@Id
	@GeneratedValue
	public int getPeopleId() {
		return peopleId;
	}

	public void setPeopleId(int peopleId) {
		this.peopleId = peopleId;
	}

	public String getPeopleName() {
		return peopleName;
	}

	public void setPeopleName(String peopleName) {
		this.peopleName = peopleName;
	}

	@ManyToMany(mappedBy="peoples")
	public Set<Role> getRoles() {
		return roles;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

}
package ManyToManyDouble;

import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;

@Entity
public class Role {
	private Integer roleId;
	private String roleName;
	private Set<People> peoples;

	public Role() {
	}

	public Role(String roleName) {
		super();
		this.roleName = roleName;
	}

	@Id
	@GeneratedValue
	public Integer getRoleId() {
		return roleId;
	}

	public void setRoleId(Integer roleId) {
		this.roleId = roleId;
	}

	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	@ManyToMany
	@JoinTable(
			name="role_peo",
			joinColumns={@JoinColumn(name="roleId")},
			inverseJoinColumns={@JoinColumn(name="peopleId")})
	public Set<People> getPeoples() {
		return peoples;
	}

	public void setPeoples(Set<People> peoples) {
		this.peoples = peoples;
	}
	
	
	
	

}

测试用例

package Test;

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

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.yiki.bean.User;
import com.yiki.bean.UserInfo;
import com.yiki.util.Utils;

import ManyToManyDouble.People;
import ManyToManyDouble.Role;
import ManyToManySingle.Students_t;
import ManyToManySingle.Teachers;
import ManyToOneSingle.Class_;
import ManyToOneSingle.Students;
import OneToManyDouble.Class_double;
import OneToManyDouble.Students_double;
import OneToManySingle.Class_room;
import OneToManySingle.Students_D;
import OneToOneSingle.StuIdCard;
import OneToOneSingle.Student;

/*
 ##################################################################
###  @BeforeClass – 表示在类中的任意public static void方法执行之前执行           ####
###  @AfterClass – 表示在类中的任意public static void方法执行之后执行             ####
###  @Before – 表示在任意使用@Test注解标注的public void方法执行之前执行     ####
###  @After – 表示在任意使用@Test注解标注的public void方法执行之后执行       ####
###  @Test – 使用该注解标注的public void方法会表示为一个测试方法                  ####
#################################################################
 */

public class test {

	static Session session;
	static Transaction tr;

	@BeforeClass
	public static void beforeClass() {
		session = Utils.openSession();
		tr = session.beginTransaction();
	}

	@AfterClass
	public static void afterClass() {
		tr.commit();
		session.close();
	}

	public static void main(String[] args) {
		session = Utils.openSession();
		tr = session.beginTransaction();
		People p1 = new People("yiki");
		People p2 = new People("tiffany");
		People p3 = new People("penny");

		Role r1 = new Role("程序员");
		Role r2 = new Role("工程师");
		Role r3 = new Role("学生");

		Set<People> pset1 = new HashSet<People>();
		pset1.add(p1);
		pset1.add(p3);

		Set<People> pset2 = new HashSet<People>();
		pset2.add(p2);
		pset2.add(p3);

		Set<Role> rset1 = new HashSet<Role>();
		rset1.add(r2);
		rset1.add(r3);
		Set<Role> rset2 = new HashSet<Role>();
		rset2.add(r2);
		rset2.add(r1);

		p1.setRoles(rset1);
		p2.setRoles(rset2);
		p3.setRoles(rset1);

		r1.setPeoples(pset2);
		r2.setPeoples(pset1);
		r3.setPeoples(pset1);

		session.save(p1);
		session.save(p2);
		session.save(p3);
		session.save(r1);
		session.save(r2);
		session.save(r3);

		tr.commit();
		session.close();
	}

	@Test
	public void testBean() {
		User user = new User();
		user.setUsername("一对一单向映射");
		session.save(user);

		UserInfo info = new UserInfo();
		info.setMail("嵌入类测试");
		session.save(info);

	}

	@Test
	public void OneToOne() {// 单向
		StuIdCard card = new StuIdCard("kkkkpopoiiutyfcd", "yiki");
		Student student = new Student("计算机", card);

		session.save(card);// 要先保存被控类
		session.save(student);

	}

	@Test
	public void OneToOne_D() {// 双向
		OneToOneDouble.StuIdCard card = new OneToOneDouble.StuIdCard("kkkkkuyiutyfcd", "yiki");
		OneToOneDouble.Student student = new OneToOneDouble.Student("计算机", card);

		session.save(card);// 要先保存被控类
		session.save(student);

	}

	@Test
	public void ManyToOne_S() {// 单向

		Class_ classnum1 = new Class_("C001", "软工班");
		Class_ classnum2 = new Class_("C002", "网络班");
		Students stus1 = new Students("计算机", new Date(), "女", classnum1);
		Students stus2 = new Students("计算机", new Date(), "女", classnum2);
		Students stus3 = new Students("计算机", new Date(), "男", classnum1);

		session.save(classnum1);
		session.save(classnum2);
		session.save(stus1);
		session.save(stus2);
		session.save(stus3);

	}

	@Test
	public void OneToMany() {// 单向

		Class_room classnum1 = new Class_room("C001", "软工班");
		Class_room classnum2 = new Class_room("C002", "网络班");
		Students_D stus1 = new Students_D("计算机", new Date(), "女");
		Students_D stus2 = new Students_D("计算机", new Date(), "女");
		Students_D stus3 = new Students_D("计算机", new Date(), "男");

		Set<Students_D> set1 = new HashSet<Students_D>();
		set1.add(stus1);
		Set<Students_D> set2 = new HashSet<Students_D>();
		set2.add(stus2);
		set2.add(stus3);

		classnum1.setStudents(set1);
		classnum2.setStudents(set2);
		// 先保存多方的集合
		session.save(stus1);
		session.save(stus2);
		session.save(stus3);

		session.save(classnum1);
		session.save(classnum2);

	}

	@Test
	public void OneToMany_D() {// 双向

		Class_double classnum1 = new Class_double("C001", "软工班");
		Class_double classnum2 = new Class_double("C002", "网络班");
		Students_double stus1 = new Students_double("计算机", new Date(), "女");
		Students_double stus2 = new Students_double("计算机", new Date(), "女");
		Students_double stus3 = new Students_double("计算机", new Date(), "男");

		Set<Students_double> set1 = new HashSet<Students_double>();
		set1.add(stus1);
		Set<Students_double> set2 = new HashSet<Students_double>();
		set2.add(stus2);
		set2.add(stus3);

		classnum1.setStudents(set1);
		classnum2.setStudents(set2);
		// 双向保存顺序无关
		session.save(stus1);
		session.save(stus2);
		session.save(stus3);

		session.save(classnum1);
		session.save(classnum2);

	}

	@Test
	public void ManyToMany() {// 单向
		Teachers t1 = new Teachers("t001", "yiki");
		Teachers t2 = new Teachers("t002", "ttttt");
		Teachers t3 = new Teachers("t003", "ttppt");

		Set<Teachers> teaset1 = new HashSet<Teachers>();
		teaset1.add(t1);
		teaset1.add(t2);

		Set<Teachers> teaset2 = new HashSet<Teachers>();
		teaset1.add(t1);
		teaset1.add(t3);

		Set<Teachers> teaset3 = new HashSet<Teachers>();
		teaset1.add(t3);
		teaset1.add(t2);

		Students_t s1 = new Students_t("penny", "计算机");
		Students_t s2 = new Students_t("soso", "计算机");
		Students_t s3 = new Students_t("dongjing", "计算机");

		s1.setTeachers(teaset1);
		s2.setTeachers(teaset3);
		s3.setTeachers(teaset2);

		session.save(t1);
		session.save(t2);
		session.save(t3);
		session.save(s1);
		session.save(s2);
		session.save(s3);


	}

	@Test
	public void ManyToMany_D() {// 双向

		People p1 = new People("yiki");
		People p2 = new People("tiffany");
		People p3 = new People("penny");

		Role r1 = new Role("程序员");
		Role r2 = new Role("工程师");
		Role r3 = new Role("学生");

		Set<People> pset1 = new HashSet<People>();
		pset1.add(p1);
		pset1.add(p3);

		Set<People> pset2 = new HashSet<People>();
		pset2.add(p2);
		pset2.add(p3);

		Set<Role> rset1 = new HashSet<Role>();
		rset1.add(r2);
		rset1.add(r3);
		Set<Role> rset2 = new HashSet<Role>();
		rset2.add(r2);
		rset2.add(r1);

		p1.setRoles(rset1);
		p2.setRoles(rset2);
		p3.setRoles(rset1);

		r1.setPeoples(pset2);
		r2.setPeoples(pset1);
		r3.setPeoples(pset1);

		session.save(p1);
		session.save(p2);
		session.save(p3);
		session.save(r1);
		session.save(r2);
		session.save(r3);

	}

}















评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值