本文主要是介绍8.2 Hibernate:多对一单向关联(unidirectional),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1 建立多对一应用场景
以人与手机的关系为例,同一时间点上,一部手机只能由一个人使用,而一个人可以同时使用一部或多部手机。手机与人之间是多对一的关系。
使用 MySQL 数据库:
CREATE TABLE `test`.`person` (`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,`name` VARCHAR(16) NOT NULL,`birth` TIMESTAMP NOT NULL,PRIMARY KEY (`id`),UNIQUE INDEX `id_UNIQUE` (`id` ASC))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COMMENT = '人';
CREATE TABLE `test`.`phone` (`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,`imei` VARCHAR(15) NOT NULL,`number` VARCHAR(11) NOT NULL,`owner_id` INT UNSIGNED NULL,PRIMARY KEY (`id`),UNIQUE INDEX `id_UNIQUE` (`id` ASC),UNIQUE INDEX `imei_UNIQUE` (`imei` ASC),UNIQUE INDEX `number_UNIQUE` (`number` ASC),INDEX `FK_PHONE_idx` (`owner_id` ASC),CONSTRAINT `FK_PHONE`FOREIGN KEY (`owner_id`)REFERENCES `test`.`person` (`id`)ON DELETE NO ACTIONON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COMMENT = '手机';
2 定义映射类
2.1 表 person
的映射类定义:
package hibernate;import java.util.Date;import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;@Entity
@Table(name = "person")
public class Person {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)@Column(name = "id")private int id;@Column(name = "name")private String name;@Column(name = "birth")private Date birth;public Person() {}public Person(String name, Date birth) {this.name = name;this.birth = birth;}// 省略 Getters 和 Setters ...}
2.2 表 phone
的映射类定义:
package hibernate;import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ForeignKey;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;@Entity
@Table(name = "phone")
public class Phone {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)@Column(name = "id")private int id;@Column(name = "imei")private String imei;@Column(name = "number")private String number;@ManyToOne@JoinColumn(name = "owner_id", foreignKey = @ForeignKey(name = "FK_PHONE"))private Person person;public Phone() {}public Phone(String imei, String number) {this.imei = imei;this.number = number;}// 省略 Getters 和 Setters ...}
3 测试
3.1 新增
@Test
public void test() {Configuration configuration = new Configuration().configure("hibernate.cfg.xml");SessionFactory sessionFactory = configuration.buildSessionFactory();Session session = sessionFactory.openSession();Transaction transaction = session.beginTransaction();Person person = new Person("Jack", new Date());session.save(person);Phone phone1 = new Phone("866661234567890", "18012345678");phone1.setPerson(person);session.save(phone1);Phone phone2 = new Phone("866660987654321", "18087654321");phone2.setPerson(person);session.save(phone2);transaction.commit();session.close();sessionFactory.close();
}
运行测试,对象 person
,phone1
和 phone2
都被持久化存储到对应的 person
和 phone
数据表中,且 phone1
和 phone2
对应记录的外键都是新插入的 person
对应记录的主键(日志省略)。
如果将单元测试代码中 session.save(phone1);
和 session.save(phone2);
屏蔽掉,则只会持久化存储 person
对象。
如果将单元测试代码中 session.save(person);
屏蔽掉,默认情况下单元测试报异常,不能成功持久化存储任一对象数据。
java.lang.IllegalStateException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance beforeQuery flushing
由此可见:新增多对一关系数据时,默认首先需要保存“一”方的数据,只能成功保存了一”方的数据后才有可能保存“多”方的数据,除非修改 @ManyToOne
的 cascade
属性,才能实现级联插入和更新(后续会详细阐述 cascade
属性的用途)
3.2 删除
基于以上测试新增的数据进行删除测试。
@Test
public void test() {Configuration configuration = new Configuration().configure("hibernate.cfg.xml");SessionFactory sessionFactory = configuration.buildSessionFactory();Session session = sessionFactory.openSession();Transaction transaction = session.beginTransaction();List<Phone> phones = session.createQuery(" FROM Phone").list();for (Phone phone : phones) {session.delete(phone);}Person person = (Person) session.createQuery(" FROM Person").list().get(0);session.delete(person);transaction.commit();session.close();sessionFactory.close();
}
注意:
(1) 可以只删除 phone
,默认不会对其关联的 person
产生影响,除非修改 @ManyToOne
的 cascade
属性(后续会详细阐述 cascade
属性的用途);
(2) 删除 person
前必须首先删除外键关联的 phone
,否则出现以下异常。
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails
3.3 更新
@Test
public void test() {Configuration configuration = new Configuration().configure("hibernate.cfg.xml");SessionFactory sessionFactory = configuration.buildSessionFactory();Session session = sessionFactory.openSession();Transaction transaction = session.beginTransaction();Phone phone = (Phone) session.createQuery(" FROM Phone").list().get(0);phone.setImei("123456789012345");phone.setNumber("15912345678");phone.getPerson().setName("Bruce");phone.getPerson().setBirth(new Date());transaction.commit();session.close();sessionFactory.close();
}
运行测试,phone
关联的 person
级联更新。
3.4 查询
@Test
public void test() {Configuration configuration = new Configuration().configure("hibernate.cfg.xml");SessionFactory sessionFactory = configuration.buildSessionFactory();Session session = sessionFactory.openSession();Transaction transaction = session.beginTransaction();List<Phone> phones = session.createQuery(" FROM Phone").list();for (int i = 1; i <= phones.size(); i++) {System.out.println("---------- Phone " + i + "----------");System.out.println("IMEI : " + phones.get(i - 1).getImei());System.out.println("Number : " + phones.get(i - 1).getNumber());System.out.println("Owner Name : " + phones.get(i - 1).getPerson().getName());System.out.println("Owner Birth : " + phones.get(i - 1).getPerson().getBirth());}transaction.commit();session.close();sessionFactory.close();
}
运行测试,打印结果:
---------- Phone 1----------
IMEI : 866661234567890
Number : 18012345678
Owner Name : Jack
Owner Birth : 2017-07-09 21:07:49.0
---------- Phone 2----------
IMEI : 866660987654321
Number : 18087654321
Owner Name : Jack
Owner Birth : 2017-07-09 21:07:49.0
这篇关于8.2 Hibernate:多对一单向关联(unidirectional)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!