博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hibernateday02表的唯一外键
阅读量:4247 次
发布时间:2019-05-26

本文共 4470 字,大约阅读时间需要 14 分钟。

一对一:唯一外键:为外键加上唯一约束

    公司  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文件中

 4.在hibernate.cfg.cml文件中对映射文件进行注册

true
true
oracle.jdbc.OracleDriver
jdbc:oracle:thin:@127.0.0.1:1521:oracle
scott
tiger
org.hibernate.dialect.Oracle9iDialect

 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
tl= new ThreadLocal
(); 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();	}}

转载地址:http://zvhhi.baihongyu.com/

你可能感兴趣的文章
【数据库】sqlite3数据库备份、导出方法汇总
查看>>
【数据库】适用于SQLite的SQL语句(一)
查看>>
【数据库】适用于SQLite的SQL语句(二)
查看>>
【数据库】适用于SQLite的SQL语句(三)
查看>>
【C++】error: passing ‘const xxx’ as ‘this’ argument discards qualifiers [-fpermissive]
查看>>
【C++】C++11 STL算法(九):番外篇
查看>>
【C++】C++11 STL算法(十):使用STL实现排序算法
查看>>
【网络编程】同步IO、异步IO、阻塞IO、非阻塞IO
查看>>
【网络编程】epoll 笔记
查看>>
【视频】视频传输协议:RTSP、RTP、RTCP、RTMP、HTTP
查看>>
【经验】向word中插入格式化的代码块
查看>>
【经验】配置Anaconda源
查看>>
【AI】在win10上安装TensorFlow2,安装成功,但是import tensorflow时报错:pywrap_tensorflow.py", line 58
查看>>
【Qt】Qt编码风格、命名约定
查看>>
【C++】重载、重写、隐藏
查看>>
【Qt】重新认识QObject
查看>>
【Qt】Qt源码中涉及到的设计模式
查看>>
【视频】海康威视摄像头RTSP协议格式
查看>>
【Qt】监视文件和目录的修改:QFileSystemWatcher
查看>>
【ffmpeg】编译时报错:error: undefined reference to `av...
查看>>