Hibernate 一对一外键双向关联
来源:互联网 发布:面试官面试技巧知乎 编辑:程序博客网 时间:2024/05/29 18:38
Hibernate 一对一外键双向关联
一对一外键关联是一对多外键关联的特例,只是在多的一方加了个唯一性约束。
一、模型
一个人对应一个地址。
/*==============================================================*/
/* DBMS name: MySQL 5.0 */
/* Created on: 2008-12-9 0:12:54 */
/*==============================================================*/
/* DBMS name: MySQL 5.0 */
/* Created on: 2008-12-9 0:12:54 */
/*==============================================================*/
drop table if exists address;
drop table if exists person;
/*==============================================================*/
/* Table: address */
/*==============================================================*/
create table address
(
id bigint not null auto_increment comment 'ID',
detail varchar(120) not null comment '详细地址',
personid bigint comment '人的ID',
primary key (id)
)
type = InnoDB;
/* Table: address */
/*==============================================================*/
create table address
(
id bigint not null auto_increment comment 'ID',
detail varchar(120) not null comment '详细地址',
personid bigint comment '人的ID',
primary key (id)
)
type = InnoDB;
alter table address comment '地址';
/*==============================================================*/
/* Table: person */
/*==============================================================*/
create table person
(
id bigint not null auto_increment comment 'ID',
name varchar(24) not null comment '姓名',
primary key (id)
)
type = InnoDB;
/* Table: person */
/*==============================================================*/
create table person
(
id bigint not null auto_increment comment 'ID',
name varchar(24) not null comment '姓名',
primary key (id)
)
type = InnoDB;
alter table person comment '人';
alter table address add constraint FK_Reference_4 foreign key (personid)
references person (id) on delete restrict on update restrict;
references person (id) on delete restrict on update restrict;
二、对象模型
public class Personimplements java.io.Serializable {
private Long id;
private String name;
private Address address;
private Long id;
private String name;
private Address address;
public class Addressimplements java.io.Serializable {
private Long id;
private Person person;
private String detail;
private Long id;
private Person person;
private String detail;
三、映射文件
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="entity.Person" table="person">
<id name="id" type="java.lang.Long">
<columnname="id" />
<generatorclass="identity"/>
</id>
<propertyname="name"type="java.lang.String">
<columnname="name"length="24"not-null="true">
<comment>姓名</comment>
</column>
</property>
<one-to-onename="address"cascade="all"/>
</class>
</hibernate-mapping>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="entity.Person" table="person">
<id name="id" type="java.lang.Long">
<columnname="id" />
<generatorclass="identity"/>
</id>
<propertyname="name"type="java.lang.String">
<columnname="name"length="24"not-null="true">
<comment>姓名</comment>
</column>
</property>
<one-to-onename="address"cascade="all"/>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="entity.Address" table="address" catalog="testdb">
<id name="id" type="java.lang.Long">
<columnname="id" />
<generatorclass="identity"/>
</id>
<propertyname="detail"type="java.lang.String">
<columnname="detail"length="120"not-null="true">
<comment>详细地址</comment>
</column>
</property>
<many-to-onename="person"class="entity.Person"
fetch="select"unique="true">
<columnname="personid">
<comment>人的ID</comment>
</column>
</many-to-one>
</class>
</hibernate-mapping>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="entity.Address" table="address" catalog="testdb">
<id name="id" type="java.lang.Long">
<columnname="id" />
<generatorclass="identity"/>
</id>
<propertyname="detail"type="java.lang.String">
<columnname="detail"length="120"not-null="true">
<comment>详细地址</comment>
</column>
</property>
<many-to-onename="person"class="entity.Person"
fetch="select"unique="true">
<columnname="personid">
<comment>人的ID</comment>
</column>
</many-to-one>
</class>
</hibernate-mapping>
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<propertyname="connection.username">root</property>
<propertyname="connection.url">
jdbc:mysql://localhost:3306/testdb
</property>
<propertyname="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<propertyname="connection.password">xiaohui</property>
<propertyname="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<propertyname="show_sql">true</property>
<propertyname="format_sql">true</property>
<mappingresource="entity/Person.hbm.xml"/>
<mappingresource="entity/Address.hbm.xml"/>
</session-factory>
</hibernate-configuration>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<propertyname="connection.username">root</property>
<propertyname="connection.url">
jdbc:mysql://localhost:3306/testdb
</property>
<propertyname="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<propertyname="connection.password">xiaohui</property>
<propertyname="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<propertyname="show_sql">true</property>
<propertyname="format_sql">true</property>
<mappingresource="entity/Person.hbm.xml"/>
<mappingresource="entity/Address.hbm.xml"/>
</session-factory>
</hibernate-configuration>
四、测试
import org.hibernate.Transaction;
import entity.Address;
import entity.Person;
import utils.HibernateSessionFactory;
public class Test {
public staticvoid main(String[] args) {
savePerson();
}
public staticvoid savePerson() {
Person person = new Person("张三");
Address address = new Address("XX街X号");
person.setAddress(address);
address.setPerson(person);
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
session.save(person);
tx.commit();
}
}
import entity.Address;
import entity.Person;
import utils.HibernateSessionFactory;
public class Test {
public staticvoid main(String[] args) {
savePerson();
}
public staticvoid savePerson() {
Person person = new Person("张三");
Address address = new Address("XX街X号");
person.setAddress(address);
address.setPerson(person);
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
session.save(person);
tx.commit();
}
}
运行日志:
Hibernate:
insert
into
person
(name)
values
(?)
Hibernate:
insert
into
testdb.address
(detail, personid)
values
(?, ?)
insert
into
person
(name)
values
(?)
Hibernate:
insert
into
testdb.address
(detail, personid)
values
(?, ?)
0 0
- Hibernate 一对一外键双向关联
- Hibernate一对一单双向外键关联
- Hibernate 一对一外键双向关联
- Hibernate 一对一外键双向关联
- Hibernate双向一对一外键关联
- Hibernate一对一外键关联映射(双向)
- hibernate一对一外键双向关联
- hibernate一对一外键双向关联
- Hibernate 一对一外键双向关联
- Hibernate一对一外键双向关联
- 13.Hibernate一对一外键&一对一主键双向关联
- 九、Hibernate一对一唯一外键关联(双向关联)
- hibernate一对一唯一外键关联映射(双向关联PersonIdCard)
- Hibernate一对一双向关联(外键关联)用法小结
- Hibernate关联之一对一外键双向关联
- Hibernate一对一双向关联
- hibernate 一对一双向关联
- Hibernate一对一双向关联
- 今日小结
- 给价格加.00
- Java学习笔记--类和对象
- go语言快速入门:使用静态文件(20)
- 内功心法 -- java.util.ArrayList<E> (1)
- Hibernate 一对一外键双向关联
- 国密SKF接口函数介绍之三:应用管理函数
- 搜索框智能提示ajax
- kafka增加topic 的partitions数量
- Java 实例说明 &&和&的区别
- java.lang.ClassCastException: oracle.sql.CLOB cannot be cast to oracle.sql.CLOB
- Lambda表达式
- MySQL数据库多表查询
- 优化UITableViewCell高度计算的那些事