本文共 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 ThreadLocaltl= 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/