Hibernate的Annotation

三、Hibernate的Annotationjava

特別注意:Hibernate的Annotation要依賴於JPA包。數組

在Hibernate的3.5以前都須要下載Hibernate的Annotation的jar包,在3.5以後Hibernate的Annotation jar包已
經集成到Hibernate包中了。app

3.一、基本映射fetch

//注意是javax.persistence.Entity
@Entity
//設置表名
@Table(name="t_user")
public class User {
	private int id;
	private String username;
	private String password;
	private String nickname;
	private Date born;
	private Date createDate;
	
	//@Column能夠爲這個字段進行定義
	@Column(name="create_date")
	public Date getCreateDate() {
		return createDate;
	}
	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}
	
	//標誌爲id屬性
	@Id
	//GeneratedValue表示自動遞增
	@GeneratedValue
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}

3.二、一對一this

@Entity
@Table(name="t_id_card")
public class IDCard {
	private int id;
	private String no;
	private Person person;
	
	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getNo() {
		return no;
	}
	public void setNo(String no) {
		this.no = no;
	}
	
	@OneToOne
	//@JoinColumn能夠設置外鍵的名稱。
	//只要使用了OneToOne就會在本身的表中增長外鍵。
	@JoinColumn(name="pid")
	public Person getPerson() {
		return person;
	}
	public void setPerson(Person person) {
		this.person = person;
	}
@Entity
@Table(name="t_person")
public class Person {
	private int id;
	private String name;
	private IDCard idCard;
	
	//只要設置了@OneToOne就會在本身的表中增長外鍵,因此只有設置mappedBy="本身在對端的屬性名稱"
	//才能說明關係由對方維護
	@OneToOne(mappedBy="person") //對端的屬性名稱,說明關係由對方來維護
	public IDCard getIdCard() {
		return idCard;
	}
	public void setIdCard(IDCard idCard) {
		this.idCard = idCard;
	}
	
	@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;
	}

3.三、一對多ssr

m:1單向:code

@ManyToOne(fetch=FetchType.LAZY) //延遲加載
	//外鍵名稱
	@JoinColumn(name="cid")
	public Classroom getClassroom() {
		return classroom;
	}
	public void setClassroom(Classroom classroom) {
		this.classroom = classroom;
	}

1:m單向:xml

@OneToMany
    //加入到對方表中的外鍵
    @JoinColumn(name="cid")
    //等於xml中的lazy="extra"
	@LazyCollection(LazyCollectionOption.EXTRA)
	public Set<Student> getStudents() {
		return students;
	}
	public void setStudents(Set<Student> students) {
		this.students = students;
	}

雙向:ci

@ManyToOne(fetch=FetchType.LAZY) //延遲加載
	//外鍵名稱
	@JoinColumn(name="cid")
	public Classroom getClassroom() {
		return classroom;
	}
	public void setClassroom(Classroom classroom) {
		this.classroom = classroom;
	}


    @OneToMany(mappedBy="classroom")
    //@JoinColumn(name="cid")
    //設置了mappedBy就已經等於說明由對方維護關係,因此不一樣在設置JoinColumn
	@LazyCollection(LazyCollectionOption.EXTRA)
	public Set<Student> getStudents() {
		return students;
	}
	public void setStudents(Set<Student> students) {
		this.students = students;
	}

3.四、多對多get

多對多雙向:

@ManyToMany
	//name="t_role_admin"表示關聯表的名稱,
	//joinColumns={@JoinColumn(name="rid")}表示本身在關聯表中的外鍵名稱,是數組
	//inverseJoinColumns={@JoinColumn(name="aid")}表示對方在關聯表中的外鍵名稱,也是數組
	@JoinTable(name="t_role_admin",joinColumns={@JoinColumn(name="rid")},
				inverseJoinColumns={@JoinColumn(name="aid")})
	public Set<Admin> getAdmins() {
		return admins;
	}
	public void setAdmins(Set<Admin> admins) {
		this.admins = admins;
	}


    @ManyToMany(mappedBy="admins") //由對方維護關係
	public Set<Role> getRoles() {
		return roles;
	}
	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

兩個一對多:

@OneToMany(mappedBy="course")
	@LazyCollection(LazyCollectionOption.EXTRA)
	public Set<TeacherCourse> getTcs() {
		return tcs;
	}
	public void setTcs(Set<TeacherCourse> tcs) {
		this.tcs = tcs;
	}


    @OneToMany(mappedBy="teacher")
	@LazyCollection(LazyCollectionOption.EXTRA)
	public Set<TeacherCourse> getTcs() {
		return tcs;
	}
	public void setTcs(Set<TeacherCourse> tcs) {
		this.tcs = tcs;
	}


    @ManyToOne
	@JoinColumn(name="tid")
	public Teacher getTeacher() {
		return teacher;
	}
	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}
	
	@ManyToOne
	@JoinColumn(name="cid")
	public Course getCourse() {
		return course;
	}
	public void setCourse(Course course) {
		this.course = course;
	}
相關文章
相關標籤/搜索