一对一映射:
1)主键关联。一对一默认使用的是立即加载,如果需要使用延迟加载,那么需要在one-to-one元素中将constrained属性设置为true,并且将待加载一方的class元素中lazy设置为true(或者不去设置,因为该属性的默认值是true)。一对一加载默认使用左外连接,可以通过修改fetch属性为select修改成迷城发送一条select语句的形式。
实例:
Student.java
package com.hibernate.one2one;
public class Student {
private int stuId;
private String stuName;
private CardId cardId;
public int getStuId() {
return stuId;
}
public void setStuId(int stuId) {
this.stuId = stuId;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public CardId getCardId() {
return cardId;
}
public void setCardId(CardId cardId) {
this.cardId = cardId;
}
}
CardId.java
package com.hibernate.one2one;
public class CardId {
private int cardId;
private String carNumber;
private Student student;
public int getCardId() {
return cardId;
}
public void setCardId(int cardId) {
this.cardId = cardId;
}
public String getCarNumber() {
return carNumber;
}
public void setCarNumber(String carNumber) {
this.carNumber = carNumber;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
}
Student.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.hibernate.one2one.Student">
<id name="stuId" column="stuid">
<generator class="increment"></generator>
</id>
<property name="stuName" column="stuName" type="string"></property>
<one-to-one name="cardId" class="com.hibernate.one2one.CardId" cascade="all" fetch="select"></one-to-one>
</class>
</hibernate-mapping>
CardId.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.hibernate.one2one.CardId" table="tb_card">
<id name="cardId" column="cardId">
<generator class="foreign">
<param name="property">student</param>
</generator>
</id>
<property name="carNumber" type="string"></property>
<one-to-one name="student" class="com.hibernate.one2one.Student" fetch="select"></one-to-one>
</class>
</hibernate-mapping>
hibernae.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">253503125</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<mapping resource="com/hibernate/one2one/CardId.hbm.xml"/>
<mapping resource="com/hibernate/one2one/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
2)外键关联。本质上是一对多的退化形式。在many-to-one元素中增加unique=“true”属性就变成一对一。