本篇介绍多对一的单向关联,应用场景是这样的,实体类汽车(Car),具有以下属性:Id,铭牌(name),车主(owner),实体类车主(Owner),具有以下属性:Id,姓名(name)。那么一辆车只能有一个车主,但是一个土豪车主可以拥有多辆汽车,这是一个多对一的关系。下面是表结构:
t_car表中有一个owner_id字段作为外键指向t_owner表,参照的是t_owner表的id
重点内容
在多对一或者一对多关系中,要将外键设置在多的一方,避免冗余
下面是实体类的代码:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="t_owner")
public class Owner {
private Integer id;
private String name;
public Owner() {
super();
}
public Owner(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;
}
}
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="t_car")
public class Car {
private Integer id;
private String name;
private Owner owner;
public Car() {
super();
}
public Car(Integer id, String name, Owner owner) {
super();
this.id = id;
this.name = name;
this.owner = owner;
}
@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;
}
@ManyToOne
public Owner getOwner() {
return owner;
}
public void setOwner(Owner owner) {
this.owner = owner;
}
}
下面进行CRUD的测试:
首先进行增加数据的测试,testCreate()
@Test
public void testCreate(){
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Owner owner = new Owner();
owner.setName("Annan_Wang");
Car car1 = new Car();
car1.setName("Ford Mustang 5.0L V8");
car1.setOwner(owner);
Car car2 = new Car();
car2.setName("Benz G63 AMG");
car2.setOwner(owner);
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);
System.out.println(owner.getName());
Car car1 = (Car) session.load(Car.class, 1);
System.out.println(car1.getName()+" "+car1.getOwner().getName());
Car car2 = (Car) session.load(Car.class, 2);
System.out.println(car2.getName()+" "+car2.getOwner().getName());
session.getTransaction().commit();
}
单向关联就不能从车主找到拥有的车辆,只能从车辆找到所属的车主。
结果:
Annan_Wang
Ford Mustang 5.0L V8 Annan_Wang
Benz G63 AMG Annan_Wang
更新测试,testUpdate()
@Test
public void testUpdate(){
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Owner owner = (Owner) session.load(Owner.class, 1);
owner.setName("王XX");
Car car1 = (Car) session.load(Car.class, 1);
car1.setName("福特-野马 5.0L V8");
Car car2 = (Car) session.load(Car.class, 2);
car2.setName("梅赛德斯奔驰 G63 AMG");
session.saveOrUpdate(owner);
session.saveOrUpdate(car1);
session.saveOrUpdate(car2);
session.getTransaction().commit();
}
最后是删除测试,testDelete(),实际情况是该车主出售了自己的奔驰硬派越野车。
@Test
public void testDelete(){
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Car car = (Car) session.load(Car.class, 2);
session.delete(car);
session.getTransaction().commit();
}
现在实际情况是,车主移民了,需要删除车主信息,但是还有一辆福特野马应该存在数据库中,做法是,先读取车辆信息,断开与车主的关联,再删除车主信息。
@Test
public void testDelete(){
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Car car = (Car) session.load(Car.class, 1);
car.setOwner(null);
Owner owner = (Owner) session.load(Owner.class, 1);
session.delete(owner);
session.getTransaction().commit();
}
希望能对你有所帮助,下一篇介绍一对多单向关联。