一对一:唯一外键:为外键加上唯一约束
公司 Company 地址 Address
关系属性 Address Coompany
1.唯一外键建表
create table g_company(
t_id Integer primary key,
t_name varchar2(30),
t_regdate date
)
create table g_address(
t_id Integer primary key,
t_city varchar2(30),
t_zip Integer,
c_id integer references g_company(t_id)
)
2.在com.jsu.hb.pojo包中创建实体类Company和Address
package com.jsu.hb.pojo;
import java.util.Date;
public class Company {
private Integer id;
private String name;
private Date regdate;
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 Date getRegdate() {
return regdate;
}
public void setRegdate(Date regdate) {
this.regdate = regdate;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
在Address.java中
package com.jsu.hb.pojo;
public class Address {
private Integer id;
private String city;
private String zip;
private Company company;
//提供工具方法,在内存中解除与Company的关系
public void remove(Company city){
this.company=null;
city.setAddress(null);
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public Company getCompany() {
return company;
}
public void setCompany(Company company) {
this.company = company;
}
}
3.提供映射文件在o2ofk.hbm.xml文件中
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.jsu.hb.pojo"> <class name="Company" table="g_company"> <!-- id属性配置 --> <id name="id" column="t_id"> <generator class="increment"></generator> </id> <!-- 普通属性配置 --> <property name="name" column="t_name"></property> <property name="regdate" column="t_regdate" type="java.util.Date"></property> <!-- 配置关系属性 cascade="save-update":表示删除公司表时不级联删除地址表 property-ref="c":Address通过关系属性Company的配置间接获得外键 --> <one-to-one name="address" class="Address" cascade="save-update"></one-to-one> </class> <class name="Address" table="g_address"> <id name="id" column="t_id"> <generator class="increment"></generator> </id> <!-- 普通属性配置 --> <property name="city" column="t_city"></property> <property name="zip" column="t_zip"></property> <!-- 配置关系属性 :唯一外键 因为Address有外键的存在,Address在本质上是多对一(many-to-one)Company, 通过unique="true":设置外键的唯一性约束 --> <many-to-one name="company" class="Company" unique="true" column="c_id" cascade="save-update" ></many-to-one> </class> </hibernate-mapping>
4.在hibernate.cfg.cml文件中对映射文件进行注册
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- show_sql:是否显示hibernate执行的SQL语句,默认为false -->
<property name="show_sql">true</property>
<!-- show_sql:是否显示hibernate执行格式化输出的SQL语句,默认为false -->
<property name="format_sql">true</property>
<!-- 配置与数据库连接的参数 -->
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:oracle</property>
<property name="connection.username">scott</property>
<property name="connection.password">tiger</property>
<!-- 2.自身属性相关的配置
dialect:方言
hibernate根据dialect的配置进行特定数据性能方面的调优
-->
<property name="dialect">org.hibernate.dialect.Oracle9iDialect</property>
<mapping resource="com/jsu/hb/pojo/o2ofk.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
5.在com.jsu.hb.util包中提供HibernateUtil.java
package com.jsu.hb.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static SessionFactory sf;
private static ThreadLocal<Session> tl= new ThreadLocal<Session>();
static{
try{
Configuration cfg = new Configuration();
cfg.configure();
sf=cfg.buildSessionFactory();
}catch(Exception e){
e.printStackTrace();
}
}
public static Session openSession(){
return sf.openSession();
}
public static Session getCurrentSession(){
Session session = tl.get();//先从储存的线程中查找
if(session==null){
session=openSession();
tl.set(session);
return session;
}
return session;
}
}
6.在com.jsu.hb.test包中提供测试类TestOnetoOneFK.java
package com.jsu.hb.test;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import com.jsu.hb.pojo.Address;
import com.jsu.hb.pojo.Company;
import com.jsu.hb.util.HibernateUtil;
public class TestOnetoOneFK {
@Test
public void save(){
Company c = new Company();
c.setName("51Gowoo");
c.setRegdate(new Date());
Address a = new Address();
a.setCity("长沙");
a.setZip("410012");
//在内存中设定他们之间的关系
c.setAddress(a);
a.setCompany(c);
//创建对象
Session session =HibernateUtil.getCurrentSession();
Transaction tx = session.getTransaction();
tx.begin();
session.save(c);
tx.commit();
}
//查询
@Test
public void query(){
Session session =HibernateUtil.getCurrentSession();
Transaction tx = session.getTransaction();
tx.begin();
Company c = (Company)session.get(Company.class, 2);
System.out.println(c.getName()+" : "+c.getRegdate());
System.out.println(c.getAddress().getCity()+" : "+c.getAddress().getZip());
tx.commit();
}
//删除
@Test
public void del(){
Session session =HibernateUtil.getCurrentSession();
Transaction tx = session.getTransaction();
tx.begin();
Company c = (Company)session.get(Company.class, 2);
//解除对象在内存中的关系
c.getAddress().remove(c);
session.delete(c);
/*
* 失败:虽然设置了级联为save-update,hibernate不是直接操作数据库,直接操作对象(内存当中)
* 通过session.get(Company.class, 1);获得了Company对象,根据传递性持久化会把Address
* 一起查询,实际操作了两个对象,而且这两个对象在内存中依旧保持关系,传递性持久化会将关联的 对象一起删除,所以报异常 deleted
* object would be re-saved by cascade 解决方式:要在删除前 解除对象在内存中的关系
* 1.为Address提供工具方法,解除内存中的关系
*
*/
tx.commit();
}
}
本文详细介绍了如何在数据库中实现一对一关系,并为外键添加唯一约束,通过创建实体类、映射文件和配置文件,实现了公司和地址表之间的关联,并提供了在内存中解除关系的方法,以及对数据的操作如保存、查询和删除。

1225

被折叠的 条评论
为什么被折叠?



