1. 程式人生 > >[Hibernate]註解/關係對映以及關係測試用例

[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);

	}

}

相關推薦

[Hibernate]註解/關係對映以及關係試用

基本註解package OneToOneSingle; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.GeneratedValue; impor

Hibernate註解方式對映

JPA:Java persistence APIJPA註解是JavaEE的標準和規範JPA是標準介面,Hibernate是實現,優先使用JPA註解。一:類級別註解1:@Entity對映實體類name屬性對應資料庫中的表(一般不寫通過@Table寫)注意:使用@Entity時必

Hibernate之基本配置試用

hibernate5.2.5入門例項 前幾天不知道怎麼下的一個Hibernate版本是hibernate-search-5.5.4.Final-dist,hibernate核心檔案為hibernate-core-5.0.9.Final.jar,有點坑。今天在公司下載的最新版

試用

功能 正常 配置 編輯 連接 限制 nbsp 我們 pan 日程管理 No. 測試用例 實際結果 期望結果 測試結果 Pass/Failed 備註 1. (1)測試內容: 設置歡迎界面,5秒後自動跳轉 到日程管理的主界面 (2)測試步驟: a.打開Andro

日程管理APP試用

nbsp 圖庫 代碼實現 符號 訪問 攝像 led height 測試用例 No. 測試用例 實際結果 期望結果 測試結果(Pass/Failed) 備註 1 輸入僅有數字或字母的用戶名及密碼,點擊註冊 註冊成功 註冊成功 Pass / 2 輸入含有特殊符

Tcl腳本調用高層API實現儀表使用和主機創建配置的自己主動化試用

sub ret eat ati 包含 lin name ref config #設置Chassis的基本參數,包含IP地址。port的數量等等 set chassisAddr 10.132.238.190 set islot 1 set portList {11 12

因果圖法設計試用

內容 splay 陰影 輸入 .cn 什麽 通過 -- tom 一、使用因果圖的好處 1 考慮了多個輸入之間的相互組合、相互制約關系 2 能夠幫助我們按一定步驟,高效率地選擇測試用例,同時還能為我們指出,程序規格說明描述中存在著什麽問題 二、利用因果圖導出測試用例需要

如何設計試用

交易 vivo 數據庫 tap 單點 個數 切換 統一 miui 測試用例設計方法 一、 Android系統功能測試設計的測試用例: a.對所測APP劃分模塊 b.詳細列出每個模塊的功能點(使用Xmind繪制功能圖) c.使用等價類劃分、邊界值、場景法等對各功能點編寫測試用

如何根據需求分析文檔編寫試用

編寫測試用例從拿到需求文檔不要立馬開始著手寫測試用例,需要仔細推敲整理需求,畫出系統級、模塊內流程圖,並找出各種測試點,等對需求進行了頭腦風暴般的整理之後,此時已對測試系統的功能很清楚了,再著手開始寫測試用例。那麽編寫測試用例的總體思路是什麽呢?1、整理分析需求文檔仔細將需求文檔閱讀一遍,記錄不明白的地方及關

VSCode下調試mocha試用

try 調試 oot 分享 png ima ace get mage 之前使用tape做Node.js的單元測試,最方便一條就是使用它就和自己寫個控制臺應用程序測試一樣,控制起來比較靈活,直接用VSCode進行調試也比較方便。然而tape輸出中文字符總是亂碼,想了很多辦法也

selenium之批量執行試用

span stl def python3 text 指定 unit efault cnblogs 把寫好的測試用例放在指定目錄下,使用discover函數掃描該目錄,並根據關鍵字自動篩選需要執行的用例。本例使用Python3.6版本。 1 # 遍歷指定目錄,批量執行測試

如何設計單元試用

圖例 定義 遍歷 負責 多少 break ima class 格式 如何編寫單元測試用例(白盒測試)。 一、 單元測試的概念 單元通俗的說就是指一個實現簡單功能的函數。單元測試就是只用一組特定的輸入(測試用例)測試函數是否功能正常,並且返回了正確的輸出。

編寫一個簡單的單元試用

ide bsp span log 加減乘除 self teardown __main__ str 開發一個簡單的計算器,用於計算兩個數的加減乘除,示例: 1 class Calculator(): 2 ‘‘‘實現簡單的加減乘除‘‘‘ 3 def _

Java NIO 之 ByteBuffer 試用

bytebuffer java 分享一下,關於ByteBuffer的一些非常基礎的測試用例package xingej.buffer.test001; import java.nio.ByteBuffer; //註意:1、原生JAVA NIO的ByteBuffer的緩沖區是不能添加字符串的,其實,從

測試理論--如何根據需求設計試用

角色 分解 中文 查詢 理論 通過 根據 步驟 正常 從拿到需求文檔不要立馬開始著手寫測試用例,需要仔細推敲整理需求,畫出系統級、模塊內流程圖,並找出各種測試點,等對需求進行了頭腦風暴般的整理之後,此時已對測試系統的功能很清楚了,再著手開始寫測試用例。那麽編寫測試用例的總體

IP地址等價類試用

cells class 3.5 不可用 win blog adding cin 訪問 下面是一個比較完善的設計方案,這個方案中,首先把IP地址分成有效可用的IP地址和有效但不可用的IP地址兩個等價類;其中有效可用的IP地址中包括IP地址的A,B,C三類地址,有效但不可用的I

接口測試基礎五-使用python+requests編寫接口試用

基礎 div 返回結果 pan 返回 結果 strong 什麽 用例 好了,有了前幾章的的基礎,寫下來我把前面的基礎整合一下,來一個實際的接口測試練習吧。 接口測試流程 1.拿到接口的URL地址 2.查看接口是用什麽方式發送 3.添加請求頭、請求體 4.發送查看返回結果,

手機測試用-設置試用

blog 網絡數 自動鎖定 空間 software 非默認 來電界面 操作 時間格式 Software Test Case ID 功能描述 操作步驟 預期結果 備註 設置窗口描述 SET_001 進入設置主窗口 1、從主菜單點擊設置進入

手機測試用-時鐘試用

繼續 文本 div gpo tester 方向 下載 廣泛 提示 ID 功能描述 操作步驟 預期結果 test time P/F comment tester test time P/F comment tester 時鐘設置 鬧鐘

手機測試用-STK試用

適應 轉移 用例 主菜單 包括 拒絕 訂閱 速度 界面 ID 功能描述 操作步驟 預期結果 test time P/F comment tester test time P/F comment tester STK服務 SIM卡適應