onetomany 使用

OnetoMany 使用:

 

实体类:

package com.sshcp.model;

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

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

@Entity
public class Classes {

	private int id ;
	private String name ;
	private Set stus = new HashSet();
	
	@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;
	}
	
	@OneToMany(mappedBy="classes")
	public Set getStus() {
		return stus;
	}
	public void setStus(Set stus) {
		this.stus = stus;
	}

}

 

StuOnetoMany.java

package com.sshcp.model;

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.ManyToOne;

@Entity
public class StuOnetoMany {
	
	private int id ;
	private String name ;
	private Classes classes ;
	
	@ManyToOne(cascade=CascadeType.ALL)
	public Classes getClasses() {
		return classes;
	}
	
	public void setClasses(Classes classes) {
		this.classes = classes;
	}
	
	@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;
	}

}

 

 

 

你可能感兴趣的:(hibernate)