1对1
Hibernate的1对1关联关系映射有两种
1 主表主键与从表主键,形成主外键关系(从表的主键又是外键)
2 主表主键与从表外键(必须唯一unique),形成主外键关系
1对1中PO使用的一个,只是hbm.xml映射存在两次写法
1对1分析:
实例:公司Company : 地址Address
jvava bean
Address.java:
package com.my.bean;
public class Address {
private Integer id;
private String addr;
//第一个地址 属于一个公司
private Company company;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public Company getCompany() {
return company;
}
public void setCompany(Company company) {
this.company = company;
}
}
Company.java:
package com.my.bean;
public class Company {
private Integer id;
private String name;
//第一个公司只有一个地址
private Address address;
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 Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
主表主键与从表主键,形成主外键关系(从表的主键又是外键)
Address.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!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.my.bean.Address" table="t_address_pk">
<id name="id">
<!-- 从表主键 又是主表的外键 -->
<generator class="foreign">
<param name="property">company</param>
</generator>
</id>
<property name="addr"></property>
<!-- 1对1 ,此项确定提供给约束使用的 -->
<one-to-one name="company" class="com.my.bean.Company" constrained="true"></one-to-one>
</class>
</hibernate-mapping>
Company.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!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.my.bean.Company" table="t_company_pk">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<!-- 1对1 -->
<one-to-one name="address" class="com.my.bean.Address"></one-to-one>
</class>
</hibernate-mapping>
主表主键与从表外键(必须唯一unique),形成主外键关系
Address.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!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.my.bean.Address" table="t_address">
<id name="id">
<generator class="native"></generator>
</id>
<property name="addr"></property>
<!-- 特例:本身多对1,但此时要是地址i必须唯一 , column指定就是外键的名称-->
<many-to-one name="company" class="com.my.bean.Company" column="company_id" unique="true"></many-to-one>
</class>
</hibernate-mapping>
Company.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!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.my.bean.Company" table="t_company">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<!-- foreign-key 直接指定外键的值,property-ref通过属性获得对应的表中外键名称 -->
<one-to-one name="address" class="com.my.bean.Address" foreign-key="company_id"></one-to-one>
</class>
</hibernate-mapping>
测试类:
package com.my.bean;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
public class TestOneToOne {
private SessionFactory sessionFactory = new Configuration()
.configure()
.addClass(Company.class)
.addClass(Address.class)
.buildSessionFactory();
@Test
public void demo01(){
Session session = sessionFactory.openSession();
session.close();
}
}
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/12085.html