public class Wife {
private Long id;
private String name;
private Husband husband;
public class Husband {
private Long id;
private String name;
private Wife wife;
外键保存在husband的表中
<?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="hiber.Wife" table="wife">
<id name="id" type="java.lang.Long">
<column name="ID" scale="0" />
<!-- The generator-class attribute of @hibernate.id is deprecated, use the @hibernate.generator tag instead -->
<generator class="increment">
</generator>
</id>
<property name="name" type="java.lang.String" >
<column name="name" length="255"></column>
</property>
<one-to-one name="husband" property-ref="wife"></one-to-one>
</class>
<class name="hiber.Husband" table="Husband">
<id name="id" type="java.lang.Long">
<column name="ID" scale="0" />
<!-- The generator-class attribute of @hibernate.id is deprecated, use the @hibernate.generator tag instead -->
<generator class="increment">
</generator>
</id>
<property name="name" type="java.lang.String" >
<column name="name" length="255"></column>
</property>
<many-to-one name="wife" class="hiber.Wife" lazy="false" unique="true" not-null="true" cascade="all" >
<column name="wife_id" scale="0" />
</many-to-one>
</class></hibernate-mapping>
测试:
Wife wife = new Wife();
Husband husband = new Husband();
wife.setName("wife777");
husband.setName("husband777");
husband.setWife(wife);
// wife.setHusband(husband);
sph.save(wife);
sph.save(husband);
//进行访问
Wife wife1 = sph.get(4l);
System.out.println(wife1.getName()+":"+wife1.getHusband().getName());
Husband husband1 = sph.getHusband(3l);
System.out.println(husband1.getName()+":"+husband1.getWife().getName());
hibernate 基于外键的1-1
原创mb636514f3a19f5 ©著作权
©著作权归作者所有:来自51CTO博客作者mb636514f3a19f5的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
1-1 SQL概述
SQL概述 通过SQL管理数据库文件 SQL与建立关系型数据表
SQL 数据库 语言 休闲 关系数据库 -
1-1中台
中台(不一定一模一样)为了快速部署----》使用Docker或者K8s
html5 java html Docker 快速部署 -
1-1 系统相关命令
查看系统状态的一些命令
文件系统 磁盘分区 centos -
1-1 课程概览
这次不会涉及到前端开发,但是前端的代码会提供给大家。 所有的请求通过网关来做转发。 之前的课程。 结束
spring cloud 前端开发 其他