关系映射的1to1
1中是基于外键的一对一
配置文件
//配置级联操作
在这两个配置文件中person是主表,address是字表。
package bean;public class Address { private Integer id; private String descs; private int code; private Person person; public Address(){} public Address(String descs,int code) { this.code = code; this.descs = descs; } @Override public String toString() { return "Address [id=" + id + ", descs=" + descs + ", code=" + code + "]"; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getDescs() { return descs; } public void setDescs(String descs) { this.descs = descs; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } }
package bean;import java.util.Date;public class Person { private Integer id; private String name; private int password; private Date birthday; private Address address; public Person(){} public Person(String name,int password,Date birthday) { this.name= name; this.password = password; this.birthday = birthday; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", password=" + password + ", birthday=" + birthday + ", 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 int getPassword() { return password; } public void setPassword(int password) { this.password = password; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } }
测试的方法
package Test;import java.util.Date;import bean.Address;import bean.Person;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.hibernate.service.ServiceRegistry;import org.hibernate.service.ServiceRegistryBuilder;public class HibernateTest { SessionFactory sessionFactory =null; Session session =null; Transaction tx = null; /** * 初始化测试数据 * @throws Exception */ public void setUp() throws Exception { System.out.println("------setUp---初始化测试资源-----"); Configuration config = new Configuration().configure(); ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); sessionFactory = config.buildSessionFactory(sr); session = sessionFactory.openSession(); } public void testAdd(){ Address p = new Address("郑州",456465); tx = session.beginTransaction(); session.persist(p); tx.commit(); } public void testAdd2(){ Address address = new Address("郑州",456465); Person person = new Person("tiger",123,new Date()); address.setPerson(person); person.setAddress(address); tx = session.beginTransaction(); session.persist(person); session.persist(address); tx.commit(); } public void testGet(){ Person p = (Person)session.get(Person.class, 1); System.out.println(p); } /** * 释放测试数据 * @throws Exception */ public void tearDown() throws Exception { System.out.println("------tearDown---释放测试数据---"); session.close(); sessionFactory.close(); } public static void main(String [] args) throws Exception { HibernateTest h = new HibernateTest(); h.setUp(); h.testAdd2(); h.tearDown(); }}
一对一关联查询的时候,如果是根据主表查找字表的数据就立即查询(一条查询语句就搞定根据左外连接),如果从字表查找主表的数据的时候是延迟查询(两条sql语句,也是左外连接实现
public void testGet() { //通过人获得地址 Person p = (Person)session.get(Person.class, 1); System.out.println(p); System.out.println(p.getAddress()); } @Test public void testGet2() { //通过地址获得人 Address addrss = (Address)session.get(Address.class, 1); System.out.println(addrss); System.out.println(addrss.getPerson()); }
public void testAdd2(){ Address address = new Address("郑州",456465); Person person = new Person("tiger",123,new Date()); address.setPerson(person); //设置了级联操作所以可以不需要再 手动保存 address类了 //person.setAddress(address); tx = session.beginTransaction(); session.persist(person); session.persist(address); tx.commit(); }
)。
基于主键的一对一配置文件
-- - -- - person