本篇介绍一对多单向关联,实例还是和Hibernate关系映射(三)多对一单向关联中的例子一样,一个车主(Owner)可以有多辆车,一辆车(Car)只能有一个车主,下面是表结构:
表结构和多对一的表结构是一样的,都是在多方(car方)有一个字段作为外键指向owner的Id。
下面是实体类的代码:
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "t_owner")
public class Owner {
private Integer id;
private String name;
private Set cars = new HashSet();
public Owner() {
super();
}
public Owner(Integer id, String name, Set cars) {
super();
this.id = id;
this.name = name;
this.cars = cars;
}
@Id
@GeneratedValue
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;
}
@OneToMany
@JoinColumn(name="ownerId")
public Set getCars() {
return cars;
}
public void setCars(Set cars) {
this.cars = cars;
}
}
在一这方,我们设置了一个Set集合,cars,表示这个车主拥有的车辆列表,在get上设置@OneToMany标签,表示一个一对多的关系,@JoinColumn(name=”ownerId”),表示外键设置在多的一方,也就是t_car表中,外键名称为ownerId,如果不设置该属性,那么会增加一个中间表,用来保存关联关系,类似于多对多的处理方法。
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "t_car")
public class Car {
private Integer id;
private String name;
public Car() {
super();
}
public Car(Integer id, String name) {
super();
this.id = id;
this.name = name;
}
@Id
@GeneratedValue
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;
}
}
上一篇的单向多对一,只能从汽车找到车主信息,而本篇的单向一对多,则只能从车主找到拥有的汽车列表,虽然在数据库表结构上都是一样的,但是在程序的应用中却大有不同,而且都存在一定的局限性。
写好配置文件后,接下来开始测试CRUD操作:
首先进行数据插入操作,testCreate()
@Test
public void testCreate(){
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Owner owner = new Owner();
owner.setName("Annan_Wang");
Set carSet = new HashSet();
Car car1 = new Car();
car1.setName("BMW M3");
carSet.add(car1);
Car car2 = new Car();
car2.setName("Porsche Cayman");
carSet.add(car2);
owner.setCars(carSet);
session.save(owner);
session.save(car1);
session.save(car2);
session.getTransaction().commit();
}
接下来测试读取操作,testRead()
@Test
public void testRead(){
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Owner owner = (Owner) session.load(Owner.class, 1);
Set carSet = owner.getCars();
System.out.println("车主姓名:"+owner.getName());
Iterator iterator = carSet.iterator();
while(iterator.hasNext()){
System.out.println("拥有车辆,品牌是:"+iterator.next().getName());
}
Car car1 = (Car) session.load(Car.class, 1);
System.out.println(car1.getName());
session.getTransaction().commit();
}
结果:
车主姓名:Annan_Wang
拥有车辆,品牌是:BMW M3
拥有车辆,品牌是:Porsche Cayman
车辆信息:BMW M3
下面是修改操作的测试,testUpdate()
@Test
public void testUpdate(){
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Car car1 = (Car) session.load(Car.class, 1);
car1.setName("宝马M3");
Car car2 = (Car) session.load(Car.class, 2);
car2.setName("保时捷Cayman");
session.saveOrUpdate(car1);
session.saveOrUpdate(car2);
session.getTransaction().commit();
}
结果:
最后是删除操作,testDelete()
@Test
public void testDelete(){
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Owner owner = (Owner) session.load(Owner.class, 1);
Car car1 = (Car) session.load(Car.class, 1);
Car car2 = (Car) session.load(Car.class, 2);
session.delete(owner);
session.delete(car1);
session.delete(car2);
session.getTransaction().commit();
}
希望能对你有所帮助,下一篇介绍一对多和多对一的双向关联