hibernate注解之实体bean OnetoMany注解(四)

一对多注解@OneToMany
我们用人和地址来表述一对多的关系。

package com.unicom.onetomany;

import java.io.Serializable;

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

import org.hibernate.annotations.GenericGenerator;
/*
 * @author huzhen
 */
@Entity
@Table(name="t2_address")
public class Address implements Serializable {
	private static final long serialVersionUID = 1L;
	public String id;
	public String name;
	public String destict;
	@Id
	@GeneratedValue(generator="indent")
	@GenericGenerator(name="indent",strategy="uuid")
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	@Column(length=32)
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Column(length=32)
	public String getDestict() {
		return destict;
	}
	public void setDestict(String destict) {
		this.destict = destict;
	}
	}

package com.unicom.onetomany;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;
/*
 * @author huzhen
 */
@Entity
@Table(name="t2_people")
public class People implements Serializable {
	public String id;
	public int age;
	public String des;
	public Set
address; @Id @GeneratedValue(generator="indent") @GenericGenerator(name="indent",strategy="uuid") public String getId() { return id; } public void setId(String id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Column(length=32) public String getDes() { return des; } public void setDes(String des) { this.des = des; } @OneToMany(cascade=CascadeType.ALL) @JoinColumn(name="people_id") public Set
getAddress() { return address; } public void setAddress(Set
address) { this.address = address; } } package com.unicom.onetomany; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.JoinColumn; import org.hibernate.Criteria; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.cfg.Configuration; import org.junit.After; import org.junit.Before; /* * @author huzhen */ public class Test { public static SessionFactory sessionFactory; public static Session session; @Before public void opensession(){ Configuration config = new AnnotationConfiguration(); config.configure(); sessionFactory = config.buildSessionFactory(); session = sessionFactory.getCurrentSession(); session.beginTransaction(); } @After public void closesession() { session.getTransaction().commit(); sessionFactory.close(); } /* * 进行着个插入的时候,一定要在注解上加上cascade=CascadeType.ALL,否则会报 * TransientObjectException异常。单向一对多注解,hibernate会冗余一个中间表, * 我们可以在@onetomany处添加注解@JoinColumn(name="people_id"),将会在地址 * 数据库中添加一个外键列 */ @org.junit.Test public void inserttest(){ People ple=new People(); ple.setAge(17); ple.setDes("hehehheheheh"); Address add=new Address(); add.setName("公司"); add.setDestict("dizhi"); Address add1=new Address(); add1.setName("hoime"); add1.setDestict("dizhi"); Address add2=new Address(); add2.setName("home"); add2.setDestict("dizhi"); Set
set=new HashSet
(); set.add(add); set.add(add1); set.add(add2); ple.setAddress(set); try { session.save(ple); } catch (HibernateException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @org.junit.Test public void select() { } @org.junit.Test public void delete(){ People ple=(People) session.get(People.class, "8ab8dab249407fa00149407fa1b70001"); session.delete(ple); } }

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值