hibernate一对多(OneToMany)注解开发

hibernate基于注解开发的一对多关系小Demo。不多说,上代码

Boss.java(老板类)

package com.it.hibernate.domain;

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

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.Type;

/**
 * 老板
 * @author 侯青华
 *
 */
@Entity
@Table(name="b_boss")
public class Boss {
	@Id
	@Column(name="b_id",nullable=true)
	@GeneratedValue(strategy=GenerationType.IDENTITY) //主键生成策略
	private Integer id;
	@Column(name="b_name",length=50)
	@Type(type = "string")
	private String name;
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name="b_date")
	private Date date;
	@OneToMany(targetEntity=Employee.class,mappedBy="b")	//一对多,让Employee维护外键
	@Cascade(CascadeType.SAVE_UPDATE) //级联
	private Set<Employee> employees = new HashSet<Employee>();
	public Boss() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Boss(Integer id, String name, Date date, Set<Employee> employees) {
		super();
		this.id = id;
		this.name = name;
		this.date = date;
		this.employees = employees;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	public Set<Employee> getEmployees() {
		return employees;
	}
	public void setEmployees(Set<Employee> employees) {
		this.employees = employees;
	}
	
	
 
}

Employee.java(雇员类)

package com.it.hibernate.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;

/**
 * 雇员
 * @author 侯青华
 *
 */
@Entity
@Table(name="e_employee")
public class Employee {
	@Id
	@GenericGenerator(name="myuuid",strategy="uuid")
	@GeneratedValue(generator="myuuid")
	@Column(name="e_id")
	private String id;
	@Column(name="e_name")
	@Type(type="string")
	private String name;
	@Column(name="e_money")
	private Double money;
	@JoinColumn(name="b_Boss_b_id")
	@ManyToOne(targetEntity=Boss.class)
	private Boss b;
	public Employee() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Employee(String id, String name, Double money, Boss b) {
		super();
		this.id = id;
		this.name = name;
		this.money = money;
		this.b = b;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Double getMoney() {
		return money;
	}
	public void setMoney(Double money) {
		this.money = money;
	}
	public Boss getB() {
		return b;
	}
	public void setB(Boss b) {
		this.b = b;
	}
	

}

Test1.java(测试类)

package com.it.hibernate.test;

import java.util.Date;

import org.hibernate.Session;
import org.junit.Test;

import com.it.hibernate.domain.Boss;
import com.it.hibernate.domain.Employee;
import com.it.hibernate.utils.HibernateUtils;

public class Test1 {
	@Test
	public void test1(){
		//创建持久化接口对象,并开启事务
		Session session = HibernateUtils.openSession();
		session.beginTransaction();
		
		//操作
		Boss b = new Boss();
		b.setDate(new Date());
		b.setName("张老板");
		
		Employee e1 = new Employee();
		e1.setMoney(1000d);
		e1.setName("员工二");
		
		Employee e2 = new Employee();
		e2.setMoney(1000d);
		e2.setName("员工三");
		
		//级联
		b.getEmployees().add(e1);
		b.getEmployees().add(e2);
		
		//维护外键
		e1.setB(b);
		e2.setB(b);
		//
		session.save(b);
		//提交事务,关闭持久化接口对象
		session.getTransaction().commit();
		session.close();
	}

}

结果:


    总结: 在一对多关系中,@Cascade属性(级联)只设置“一”的一方即可,外键由“多”的一方进行维护,在实际开发场景中,删除员工老板不会被删除,老板被删除了员工肯定要删除。







  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值