@OneToMany单向一对多

Hibernate单项一对多详细配置:

数据模型:

@OneToMany单向一对多_第1张图片

一个年级有多个学生,每个学生只属于一个年级。

用hibernate实现一对多关系有xml配置和注解两种方式,下面逐一介绍:

第一种:注解方式

Grade.java

package cn.siggy.pojo;

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.MappedSuperclass;
import javax.persistence.OneToMany;
import javax.persistence.Table;
/**
 * AbstractGrade entity provides the base persistence definition of the Grade
 * entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "GRADE")
public class Grade implements java.io.Serializable {

	// Fields

	private Integer id;
	private String name;
	private Set students = new HashSet(0);

	// Constructors
	public Grade() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Grade(Integer id, String name, Set students) {
		super();
		this.id = id;
		this.name = name;
		this.students = students;
	}

	// Property accessors
	@Id
	@GeneratedValue
	@Column(name = "ID", unique = true, nullable = false, precision = 10, scale = 0)
	public Integer getId() {
		return this.id;
	}

	

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

	@Column(name = "NAME")
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY,targetEntity=Student.class)    
        //@JoinColumn(name="GRADE_ID",nullable=true) //该配置在单独保存Grade是会将student中的外键字段置空,所以采用下面配置
        @JoinColumn(name="PLAN_ID",updatable=false) 
        public Set getStudents() {
		return this.students;
	}
	public void setStudents(Set students) {
	this.students = students;
	}
}


 
  

Student.java

package cn.siggy.pojo;

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.ManyToOne;
import javax.persistence.MappedSuperclass;
import javax.persistence.Table;

/**
 * AbstractStudent entity provides the base persistence definition of the
 * Student entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "STUDENT")
public class Student implements java.io.Serializable {

	// Fields

	private Integer id;
	private String name;
	private Integer age;

	// Constructors

	/** default constructor */
	public Student() {
	}

	// Property accessors
	@Id
	@GeneratedValue
	@Column(name = "ID", unique = true, nullable = false, precision = 10, scale = 0)
	public Integer getId() {
		return this.id;
	}

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

	@Column(name = "NAME", length = 510)
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "AGE", precision = 10, scale = 0)
	public Integer getAge() {
		return this.age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

}


hibernate.cfg.xml




	
		
		oracle.jdbc.driver.OracleDriver
		jdbc:oracle:thin:@10.18.100.126:1521:gsdb
		db_lzsmw
		pass_lzsmw
		
		org.hibernate.dialect.Oracle10gDialect
		true
		true
		update
		
		
		
		
	



 

第二种:xml配置方式实现一对多 

Grade.java

package cn.siggy.pojo;

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

public class Grade {
	private int id;
	private String name;
	private Set students = new HashSet(0);
	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 Set getStudents() {
		return students;
	}
	public void setStudents(Set students) {
		this.students = students;
	}
}

Grade.hbm.xml




	
		
			
		
		
		
		
			
			
			
			
		
	

Student.java

package cn.siggy.pojo;

public class Student {
	private int id;
	private String name;
	private int age;
	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 int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

Student.hbm.xml




	
		
			
		
		
		
	
hibernate.cfg.xml




	
		
		oracle.jdbc.driver.OracleDriver
		jdbc:oracle:thin:@10.18.100.126:1521:gsdb
		db_lzsmw
		pass_lzsmw
		
		org.hibernate.dialect.Oracle10gDialect
		true
		true
		update
		
		
		

		
		
	

工具类:HibernateUtil.java

package cn.siggy.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
	private static Configuration cfg=null;
	private static SessionFactory factory=null;
	private static Session session = null;
	static{
		cfg = new Configuration().configure();
		factory = cfg.buildSessionFactory(new StandardServiceRegistryBuilder()
		.applySettings(cfg.getProperties()).build());
	}
	public static Session getSession(){
		if(factory!=null)
			return factory.openSession();
		factory=cfg.buildSessionFactory(new StandardServiceRegistryBuilder()
		.applySettings(cfg.getProperties()).build());
		return factory.openSession();
	}
	public static void closeSession(){
		if(session!=null&&session.isOpen())
			session.close();
	}
}

Junit单元测试类:

package cn.siggy.test;

import java.sql.SQLException;
import java.util.Iterator;
import javax.sql.rowset.serial.SerialException;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;
import cn.siggy.pojo.Grade;
import cn.siggy.pojo.Student;
import cn.siggy.util.HibernateUtil;

public class HibernateTest {
	@Test
	public void testCreateDB(){
		Configuration cfg = new Configuration().configure();
		SchemaExport se = new SchemaExport(cfg);
		//第一个参数 是否生成ddl脚本  第二个参数  是否执行到数据库中
		se.create(true, true);
	}
	@Test
	public void testSave() throws HibernateException, SerialException, SQLException{
		Session session = null;
		Transaction tx = null;
		try{
			session = HibernateUtil.getSession();
			tx = session.beginTransaction();
			Grade grade = new Grade();
			grade.setName("基础");
			
			Student stu = new Student();
			stu.setName("张三疯");
			stu.setAge(22);
			Student stu1 = new Student();
			stu1.setName("老王");
			stu1.setAge(23);
			//关联
			grade.getStudents().add(stu);
			grade.getStudents().add(stu1);
			//保存数据的顺序 是根据外键的配置来决定的
			//如果外键不能为null,那么先保存一的一端
			//如果外键可以为null,则可以随意保存
			session.save(grade);
			session.save(stu);
			session.save(stu1);
			
			tx.commit();
			
		}catch (HibernateException e) {
			if(tx!=null)
				tx.rollback();
			e.printStackTrace();
			throw e;
		}finally{
			HibernateUtil.closeSession();
		}
	}
	@Test
	public void testGet(){
		Session session = null;
		Transaction tx = null;
		try{
			session = HibernateUtil.getSession();
			tx = session.beginTransaction();
			//取数据
			Grade grade = (Grade)session.get(Grade.class, 1);
			System.out.println("gradeName="+grade.getName());
			System.out.println("grade所对应的多的一端的数据");
			Iterator iter = grade.getStudents().iterator();
			for(;iter.hasNext();){
				Student temp = iter.next();
				System.out.println("name="+temp.getName()+"\tage="+temp.getAge());
			}
			tx.commit();
		}catch (HibernateException e) {
			if(tx!=null)
				tx.rollback();
			e.printStackTrace();
			throw e;
		}finally{
			HibernateUtil.closeSession();
		}
	}
}

如下表结构代码可以自动生成,所以不用手动创建

Student表结构:
@OneToMany单向一对多_第2张图片

Grade表结构:

@OneToMany单向一对多_第3张图片



 
 

你可能感兴趣的:(java,web开发)