hibernate一对一共享主键映射是如何配置?一对一关系外键映射如何配置?
下面做个简单测试。
导入jar包
配置hibernate.cfg.xml文件
写相关实体
数据库建表
映射文件配置
测试
第一个测试是一对一共享主键
hibernate.cfg.xml配置
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 添加配置信息:数据库连接参数 -->
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="hibernate.connection.username">zt</property>
<property name="hibernate.connection.password">zt</property>
<!-- 添加配置信息:hibernate 自身属性
dialect :方言,指定数据类型,hibernate可以针对不同的数据库做出相应的优化
-->
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<!-- 为getCurrentSession增加配置 -->
<property name="current_session_context_class">thread</property>
<mapping resource="person.cfg.xml"/>
</session-factory>
</hibernate-configuration>
2.person 实体,在这里粗略表达个人
passport实体,粗略表示护照。
person 实体代码
package com.hibernate.entity;
public class Person {
private Integer id;
private String name;
private String address;
// 护照信息
private Passport passport;
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 String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Passport getPassport() {
return passport;
}
public void setPassport(Passport passport) {
this.passport = passport;
}
public Person(Integer id, String name, String address) {
super();
this.id = id;
this.name = name;
this.address = address;
}
public Person() {
super();
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", address=" + address + "]";
}
}
passport实体代码
package com.hibernate.entity;
public class Passport {
private Integer id;
private Integer expire;
private String note;
// 个人信息
private Person person;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getExpire() {
return expire;
}
public void setExpire(Integer expire) {
this.expire = expire;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public Passport(Integer id, Integer expire, String note) {
super();
this.id = id;
this.expire = expire;
this.note = note;
}
public Passport() {
super();
}
@Override
public String toString() {
return "Passport [id=" + id + ", expire=" + expire + ", note=" + note + "]";
}
}
3.数据库建表
CREATE TABLE PERSON(
ID NUMBER(2) PRIMARY KEY,
NAME VARCHAR2(20),
address VARCHAR2(20)
)
CREATE TABLE PASSPORT(
ID NUMBER(2) PRIMARY KEY references PERSON(ID),
expire number(1),
note VARCHAR2(20)
)
4.映射文件配置
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.hibernate.entity.Person" table="person">
<id name="id" column="id" type="java.lang.Integer">
<generator class="increment"></generator>
</id>
<property name="name" column="name" type="java.lang.String"></property>
<property name="address" column="address" type="java.lang.String"></property>
<!--
关系属性:passport
表关联配置要点:
1.关系属性名
2.关系对方
3.关系中的外键
one-to-one 默认行为:在进行表连接时,将双方的主键相连
4.级联,对关联表进行同级操作(查询操作不受级联限制)
默认:none 不级联
save-update :当更新当前方时,级联更新对方
delete :当删除当前方时,级联删除对方
all : 所有操作都级联
-->
<one-to-one name="passport" class="com.hibernate.entity.Passport" cascade="save-update"></one-to-one>
</class>
<class name="com.hibernate.entity.Passport" table="passport">
<id name="id" column="id" type="java.lang.Integer">
<!--
当前的主键是外键,要从关系对方中来
-->
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
<property name="expire"></property>
<property name="note"></property>
<!-- constrained: true 表示为从表方 -->
<one-to-one name="person" class="com.hibernate.entity.Person" cascade="none" constrained="true"></one-to-one>
</class>
</hibernate-mapping>
5.HibUtil session工具类
package com.hibernate.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibUtil {
private static Configuration cfg = new Configuration().configure();
private static SessionFactory factory = cfg.buildSessionFactory();
public static Session getSession(){
return factory.getCurrentSession();
}
public static Session openSession(){
return factory.openSession();
}
}
6.测试插入数据
@Test
public void insert(){
Session session = HibUtil.getSession();
Transaction tx = session.beginTransaction();
Person person = new Person(null, "小明", "河南");
Passport passport = new Passport(null, 3, "美国");
person.setPassport(passport);
passport.setPerson(person);
session.save(person);
tx.commit();
}
7.测试查询
@Test
public void query(){
Session session = HibUtil.getSession();
Transaction tx = session.beginTransaction();
Person person = (Person)session.get(Person.class, 1);
System.out.println(person.getPassport());
System.out.println(person);
tx.commit();
}
8测试更新
@Test
public void update(){
Session session = HibUtil.getSession();
Transaction tx = session.beginTransaction();
Person person = (Person)session.get(Person.class, 1);
Passport passport = person.getPassport();
// 修改数据
person.setName("小强");
person.setAddress("china");
passport.setExpire(1);
passport.setNote("中国");
session.update(person);
tx.commit();
}
9.删除
@Test
public void delete(){
Session session = HibUtil.getSession();
Transaction tx = session.beginTransaction();
/* 1.单独删除从表 ,直接查询出来,删除就可以了。
* Passport passport = (Passport)session.get(Passport.class, 1);
session.delete(passport);
*
* 2.删除主表,级联删除从表
* 注意:要在关系映射中 添加级联配置
* <one-to-one name="passport" class="com.hibernate.entity.Passport" cascade="save-update,delete"></one-to-one>
*/
Person person = (Person)session.get(Person.class, 1);
session.delete(person);
tx.commit();
}
第二个是一对一外键关联关系
建立二个表
create table STUDENT(
id NUMBER(2) primary key,
age number(2),
name nvarchar2(20)
)
create table COMPLATE(
id NUMBER(2) primary key,
price number(5),
brand nvarchar2(20),
stu_id references STUDENT(id) unique
)
2。student 实体
package com.hibernate.entity;
public class Student {
private Integer id;
private Integer age;
private String name;
private Compoter compoter;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Compoter getCompoter() {
return compoter;
}
public void setCompoter(Compoter compoter) {
this.compoter = compoter;
}
@Override
public String toString() {
return "Student [id=" + id + ", age=" + age + ", name=" + name + ", compoter=" + compoter + "]";
}
public Student(Integer id, Integer age, String name) {
super();
this.id = id;
this.age = age;
this.name = name;
}
public Student() {
super();
}
}
3. compoters实体
package com.hibernate.entity;
public class Compoter {
private Integer id;
private Integer price;
private String brand;
private Student student;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
@Override
public String toString() {
return "Compoter [id=" + id + ", price=" + price + ", brand=" + brand + "]";
}
public Compoter(Integer id, Integer price, String brand) {
super();
this.id = id;
this.price = price;
this.brand = brand;
}
public Compoter() {
super();
}
}
4.最重要的配置文件来了 student.cfg.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hibernate.entity">
<class name="Student" table="student">
<id name="id">
<generator class="increment"></generator>
</id>
<property name="age"/>
<property name="name"/>
//
<one-to-one name="compoter" class="Compoter" property-ref="student"
cascade="all"></one-to-one>
</class>
<class name="Compoter" table="complate">
<id name="id">
<generator class="increment"></generator>
</id>
<property name="brand"/>
<property name="price"/>
<many-to-one unique="true" name="student" class="Student"
column="stu_id" cascade="none"></many-to-one>
</class>
</hibernate-mapping>
5测试,这里只测试一个插入,查询,删除,更新都和上面一样用法。
/**
* 1.插入从表
* 2.插入主表
* 3.级联插入
*/
@Test
public void inset(){
Session session = HibUtil.getSession();
Transaction tx = session.beginTransaction();
/* 插入主表 */
/*Student stu = new Student(null, 20, "zs");
session.save(stu);*/
/* 插入从表,数据库要是没有数据,自己先先添加一条 */
/*
Student student = (Student)session.get(Student.class, 1);
Compoter compoter = new Compoter(null, 99, "LX");
compoter.setStudent(student);
session.save(compoter);
*/
/* 级联插入 */
Student student = new Student(null, 19, "刷刷");
Compoter compoter = new Compoter(null, 99, "联想");
student.setCompoter(compoter);
compoter.setStudent(student);
session.save(student);
tx.commit();
}
转载于:https://blog.51cto.com/zhuws/1936902