如:person与idCard
idcard中的id作为主键又作为一个引向person的外键。person作为主表,idcard作为从表。
Person:
public class Person implements Serializable{ private Integer id; private String name; private IdCard idCard; public Person() { } 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 IdCard getIdCard() { return idCard; } public void setIdCard(IdCard idCard) { this.idCard = idCard; } }
IdCard:
public class IdCard { private Integer id; private String cardNo; private Person person; public IdCard(){ } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Person getPerson() { return person; } public String getCardNo() { return cardNo; } public void setCardNo(String cardNo) { this.cardNo = cardNo; } public void setPerson(Person person) { this.person = person; } }
Person.hbm.xml:
<hibernate-mapping> <class name="cn.framelife.hibernate.entity.Person" table="person" catalog="hibernate"> <id name="id" type="java.lang.Integer"> <column name="id" /> <generator class="native" /> </id> <one-to-one name="idCard" class="cn.framelife.hibernate.entity.IdCard"></one-to-one> <property name="name" type="java.lang.String"> <column name="name" length="45" not-null="true" /> </property> </class> </hibernate-mapping>
IdCard.hbm.xml:
<hibernate-mapping> <class name="cn.framelife.hibernate.entity.IdCard" table="id_card" catalog="hibernate"> <id name="id" type="java.lang.Integer"> <column name="id" /> <generator class="foreign"> <param name="property">person</param> </generator> </id> <property name="cardNo" type="java.lang.String"> <column name="card_no" length="45" not-null="true" /> </property> <!-- constrained="true", 表明当前主键上存在一个约束--> <one-to-one name="person" constrained="true" class="cn.framelife.hibernate.entity.Person"></one-to-one> </class> </hibernate-mapping>
增加操作:
Person person = new Person(); person.setName("zhang"); IdCard idCard = new IdCard(); idCard.setCardNo("11111111"); idCard.setPerson(person); //在保存idCard的时候会保存person session.save(idCard); //2
查询操作:
根据主表得到从表信息。查询主表的时候,通过一次连接查询查出两张表中所需要的数据。
Person person = (Person) session.get(Person.class, 1); System.out.println("-----------"); System.out.println(person.getIdCard().getCardNo());
控制台信息:
Hibernate: select person0_.id as id2_1_, person0_.name as name2_1_, idcard1_.id as id1_0_, idcard1_.card_no as card2_1_0_ from hibernate.person person0_ left outer join hibernate.id_card idcard1_ on person0_.id=idcard1_.id where person0_.id=? -------------- 11111111
根据从表得到主表信息。查询从表的时候,是先查从表数据,在使用到主表的对象的时候,再查询主表。
IdCard idCard = (IdCard) session.get(IdCard.class, 1); System.out.println("-----------"); System.out.println(idCard.getPerson().getName());
控制台信息:
Hibernate: select idcard0_.id as id1_0_, idcard0_.card_no as card2_1_0_ from hibernate.id_card idcard0_ where idcard0_.id=? ----------- Hibernate: select person0_.id as id2_1_, person0_.name as name2_1_, idcard1_.id as id1_0_, idcard1_.card_no as card2_1_0_ from hibernate.person person0_ left outer join hibernate.id_card idcard1_ on person0_.id=idcard1_.id where person0_.id=? zhang
one-to-one(元素)懒加载分析:
必须同时满足下面的三个条件时才能实现懒散加载:
1).lazy!=false (lazy缺省方式就!=false,lazy是=proxy)
2).constrained=true
3).fetch=select(fetch缺省方式即为select)
因为主表不能有constrained=true,所以主表没有懒加载功能。能够懒加载的对象都是被改写过的代理对象,当相关联的session没有关闭时,访问这些懒加载对象(代理对象)的属性(getId和getClass除外)时,hibernate会初始化这些代理,当相关联的session关闭后,再访问懒加载的对象将会出现异常。
在根据从表得到主表信息的查询中,查询从对象IdCard时实现了懒加载功能,因为它只查询了IdCard对象,而关联的Person对象它没有进行查询。在使用到Person的时候,将IdCard关联的Person对象也进行了查询。因为访问这些懒加载对象(代理对象)的属性(getId和getClass除外)时,hibernate会初始化这些代理.
在数据量过多过大的时候,不适合使用缓存时,应该使用懒加载。