基于主键一对一
1.一端主键生成器使用foreign策略,表明根据对方主键来生成自己的主键,自己不独立生成主键,<param>子元素指定使用当前持久化类那个属性最为对方
2.采用foreign策略的一端增加one-to-one元素映射关联属性,并增加constarined="true"属性;另一端增加one-to-one元素映射关联属性。
3.constarined(约束)指定当前持久化类对应的数据表的主键添加一个外键约束,引用被关联的对象所对应的数据表主键
第一个对象(部门)
//部门
public class Department {
private Integer depId;
private String depName;
private Manager mgr;
public Manager getMgr() {
return mgr;
}
public void setMgr(Manager mgr) {
this.mgr = mgr;
}
public Integer getDepId() {
return depId;
}
public void setDepId(Integer depId) {
this.depId = depId;
}
public String getDepName() {
return depName;
}
public void setDepName(String depName) {
this.depName = depName;
}
第二个对象(经理)
//经理
public class Manager {
private Integer mId;
private String mName;
private Department dep;
public Integer getmId() {
return mId;
}
public void setmId(Integer mId) {
this.mId = mId;
}
public String getmName() {
return mName;
}
public void setmName(String mName) {
this.mName = mName;
}
public Department getDep() {
return dep;
}
public void setDep(Department dep) {
this.dep = dep;
}
}
第一个对象(部门)映射文件
使用外键方式生成当前主键<generator class="foreign">
property指定当前持久化类那个属性的主键作为外键
采用foreign策略的一端增加one-to-one元素映射关联属性,并增加constarined="true"属性以使当前主键使用外键约束
<hibernate-mapping>
<class name="cheniso.Department" table="DEPARTMENTISO">
<id name="depId" type="java.lang.Integer">
<column name="DEPID" />
<!-- 使用外键方式生成当前主键 -->
<generator class="foreign">
<!-- property指定当前持久化类那个属性的主键作为外键 -->
<param name="property">mgr</param>
</generator>
</id>
<property name="depName" type="java.lang.String">
<column name="DEPNAME" />
</property>
<!-- 采用foreign策略的一端增加one-to-one元素映射关联属性,并增加constarined="true"属性以使当前主键使用外键约束
-->
<one-to-one name="mgr" class="cheniso.Manager" constrained="true"></one-to-one>
</class>
</hibernate-mapping>
第二个对象(经理)映射文件
<hibernate-mapping>
<class name="cheniso.Manager" table="MANAGERISO">
<id name="mId" type="java.lang.Integer" access="field">
<column name="MID" />
<generator class="native" />
</id>
<property name="mName" type="java.lang.String" access="field">
<column name="MNAME" />
</property>
<one-to-one name="dep" class="cheniso.Department"></one-to-one>
</class>
</hibernate-mapping>
保存
- 没有先后顺序 都不会有多余SQL
public static void testOneToOneSave() {
StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();// 配置文件configure()
SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
cheniso.Department department = new cheniso.Department();
department.setDepName("研发部");
cheniso.Manager manager = new cheniso.Manager();
manager.setmName("张三");
// 设置关联
department.setMgr(manager);
manager.setDep(department);
// 没有先后顺序 都不会有多余SQL
session.save(manager);
session.save(department);
transaction.commit();
session.close();
sessionFactory.close();
}
查询
/**
* 一对一关联(主键) 查询
*/
public static void testOneToOneGet() {
StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();// 配置文件configure()
SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
// 1.默认对关联属性使用懒加载
// 2.会出现懒加载异常
cheniso.Department department = session.get(cheniso.Department.class, 1);
System.out.println(department.getDepName());
cheniso.Manager manager = department.getMgr();
System.out.println(manager.getmName());
transaction.commit();
session.close();
sessionFactory.close();
}