hibernate一对一主键关联

一对一主键关联指的是两个表通过主键形成的一对一映射。

数据表要求:A表的主键也是B表的主键同时B表的主键也是A表的外键

sql:

create table people(
id int primary key auto_increment,
name varchar(100) not null,
sex varchar(100) not null,
age int
)
create table idcard(
id int primary key,
idcard_code varchar(50) not null,
FOREIGN KEY(id) REFERENCES people(id)
)

单向主键关联映射:

package com.demo.hibernate.beans;

public class IDcard {
private int id;
private String idcard_code;
private People people;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getIdcard_code() {
return idcard_code;
}
public void setIdcard_code(String idcardCode) {
idcard_code = idcardCode;
}
public People getPeople() {
return people;
}
public void setPeople(People people) {
this.people = people;
}
public IDcard(int id, String idcardCode, People people) {
super();
this.id = id;
idcard_code = idcardCode;
this.people = people;
}
public IDcard(){} }
package com.demo.hibernate.beans;

public class People {
private int id;
private String name;
private String sex;
private int age;
//private IDcard idcard;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
/*public IDcard getIdcard() {
return idcard;
}
public void setIdcard(IDcard idcard) {
this.idcard = idcard;
}*/
public People(int id, String name, String sex, int age) {
super();
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
}
public People(){} }
<?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.demo.hibernate.beans.User" table="USER">
<id name="id" type="int">
<column name="id"></column><!-- 注意column要放在前面 -->
<generator class="foreign">
<param name="property">people</param>
</generator>
</id> <property name="idcard_code" type="string" column="IDCARD_CODE"/> <one-to-one name="prople" class="com.demo.hibernate.beans.People" property-ref="IDcard" cascade="all"></one-to-one><!--这里要选定好级联操作cascade属性不然可能不会级联持久化类的另一个表--> </class>
</hibernate-mapping>

test:

import com.demo.hibernate.beans.IDcard;
import com.demo.hibernate.beans.People;
import com.demo.hibernate.dao.IDcardDAO;
import com.demo.hibernate.dao.PeopleDAO; public class IDcardService {
public static void main(String[] args){
PeopleDAO pdao=new PeopleDAO();
IDcardDAO iddao=new IDcardDAO();
IDcard idcard=new IDcard();
idcard.setIdcard_code("362421199610126814");
People people=new People();
people.setName("guo");
people.setAge(21);
people.setSex("男");
idcard.setPeople(people);
iddao.addIDcard(idcard);
} }

结果:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAzQAAAC4CAIAAAC6ij4eAAAgAElEQVR4nO2d7bXrKq9GUxfV3F+uxyWcKujirSBN7A64PxxjAZIMjrPiJHOOM85YmzggPgyPhYNu//3337+V/HfKzCHN9ySZQpqnNMWUUrrPKczbR2Gqv1JdkFKabinmf8R0u6XblHqYw/bFuBpwID2ntPXKCfd5u340/az8rXxSGmi3NkNJuKXbrchqDul2S9PUm74UcVvzafs6p4eQ7uXFy3/ZuqWI202pb7+dXk1DmvP1os3DWujDgKn8Vpm/k4/VDmr+B9rBaedsZwjlRyP3Vy5dlrsYs9g2NVap5Q61Q66s/G8pYqhfrHSnnYdw+j1OteV++6jpS8o2J5Stp9bXah9Z5RgfmTjtLO236vVM+oKcjQGgg9sRcXZf/1+Ls2aSar9eqI1BcdbOLAfSFTNEvRZkvUbTz8rfyielgXbTv55SSimINonTVu4cCg1UtKeWHoT9s1iEplshlAs9ZFveDphROy1CtYAJG5Z1Zck5To9VxCrXysdqByv/NNoORv7yyul2sjhLzUOOemvLcg+0g8Vov6jpyW3nfqx+j7G4RopstX3U9PYhKk8LTr2U9oniRouKjldZuiloiuqU9IQ4AxjmqDhbZMSu52xHnI3gLBJD6VbKJ4mzbqyvO4vBUHu2nonHohW9VaFflByw0yy09Jr4X/cXyzYfsx1c84bawcw/6oUeoNZ2qyV6Q2nljraD49FRr7f6xemvdJ44U8dPVeV6W6D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HOWMZAoAaxBkAABzkjGUIAGrGxNn/rbQfvXuKAACAv+bkFQkA/v37NyTOHGX2D3EGAPB7nLwiAcC/f//6xZmvzP4hzgAAfo+TVyQA+PfvX6c421Vm/xBnAAC/x8krEgD8+/cPzxkAABzm5BUJAP79+8c7ZwAAcJiTVyQA+PfvH7/WBACAw5y8IgHAv3//OOcMAAAOc8YyBAA1iDMAADjIGcsQANQgzgAA4CBnLEMAUIM4AwCAg5yxDAFAzWniDAAAAACeB3EGAAAAcCEQZwAAAAAXohZnAAAAAPBGbm8UhgAAAABQ8f8HG04RESaMCAAAAABJRU5ErkJggg==" alt="" />

双向多对一:

前面单向的配置不变,在原来的People基础上加上对应的IDcard对象以及修改People.hbm.xml

People.java---->把前面的注释去掉

People.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.demo.hibernate.beans.People" table="PEOPLE">
<id name="id" type="int">
<column name="id"></column><!-- 注意column要放在前面 -->
<generator class="native"></generator>
</id> <property name="name" type="string" column="NAME"/> <property name="sex" type="string" column="SEX"/> <property name="age" type="int" column="AGE"/> <one-to-one name="idcard" class="com.demo.hibernate.beans.IDcard" property-ref="people" cascade="all"></one-to-one> </class>
</hibernate-mapping>

Test:

public static void main(String[] args){
PeopleDAO dao=new PeopleDAO();
People people=new People();
people.setName("guon");
people.setAge(28);
people.setSex("女");
IDcard card=new IDcard();
card.setIdcard_code("362421199610116734");
people.setIdcard(card);
card.setPeople(people);
dao.addPeople(people);
}

注意双向与单向的test页面有区别的,在于People和IDcard对象里面都含有对方的对象,必须全部添加进入,否则会报错误,错误是关于主键的类型与策略不符合。

结果:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAr4AAADICAIAAACBAXXXAAAgAElEQVR4nO2dy7XrOK5AlVT3gNHckULpniqEntdcWb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AAEwHAAAACIDpAAAAAAEwHQAAACAApgMAAAAEwHQAAACAAJgOAAAAEADTAQAAAAJgOgAAAEAATAcAAAAIgOkAAAAAATAdAAAAIMDwTzf/BwAAP0b/HAG/A6YDAACYPG76gc8F0wEAAEweN/3A54LpAAAAJo+bfuBzqU2HPzfaV1/dgAEA4Nk8ZSaCD6MwHRy74R9MBwCA3+MpMxF8GJvp4NsN/2A6AAD8Hk+ZieDDuJoOu3bDP5gOAAC/x1NmIvgw8DoAAIDJU2Yi+DA46wAAACZPmYngw+AXFgAAYPKUmQg+DO51AAAAk8dNP/C5YDoAAIDJ46Yf+FwwHQAAwORx0w98LpgOAABg8rjpBz6XgOkAAAAAgOkAAAAAATAdAAAAIMDwPwAAAIBu8DoAAABAAEwHAAAACIDpAADw1vzvf/9bluXVUnwe6K2HY1rCdAAAeGuYAo+B3nrAdAAA+EKYAo+B3nrAdAAA+EKYAo+B3nrAdAAA+EKYAo+B3nrAdAAA+EKYAo+B3nrAdAAA+EKYAo+B3nrAdAAA+ELOnAL//u+/h3//9+/eV//81T7/64/6+P3AdOgB0wEA4AuxBve//gxyEq+sAt1IwHS48tef4coTCvP3f//9tLyieFralFS3mf8H8PEyBkPxvjQAAAAASUVORK5CYII=" alt="" />

一对一外键关联:

单向(特殊的many-to-one,在many-to-one上加上unique=“true”):

create table person(
id int primary key auto_increment,
name varchar(100),
age int,
addressid int unique
)
create table address(
id int primary key auto_increment,
detail varchar(100)
) alter table person
add constraint fk_per_add foreign key (addressid) references address (id);

SQL

package com.entity;

public class Person {
private int id;
private String name;
private int age;
private Address address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
} public Person(){} }

Person.java

<?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.entity.Person" table="PERSON">
<id name="id" type="int">
<column name="id"></column>
<generator class="identity"></generator>
</id>
<property name="name" type="string">
<column name="NAME"></column>
</property>
<property name="age" type="int">
<column name="AGE"></column>
</property>
<many-to-one name="address" class="com.entity.Address" unique="true" cascade="all">
<column name="addressid"></column>
</many-to-one>
</class> </hibernate-mapping>

注意在多对一里面也要加上cascade="all"这种条件,当然可能值不是all

关于address的话就是普通的持久化类和映射文件

双向(前面与单向相同,附表采用one-to-one配置):

<?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.entity.Address" table="ADDRESS">
<id name="id" type="int">
<column name="id"></column>
<generator class="identity"></generator>
</id> <property name="detail" type="string">
<column name="detail"></column>
</property> <one-to-one name="person" class="com.entity.Person" cascade="all" property-ref="address"> </one-to-one>
</class> </hibernate-mapping>

Address.hbm.xml

package com.entity;

public class Address {
private int id;
private String detail;
private Person person; public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
} public Address(){}
public void setPerson(Person person) {
this.person = person;
}
public Person getPerson() {
return person;
} }
package com.server;

import com.dao.AddressDAO;
import com.entity.Address;
import com.entity.Person; public class Test {
public static void main(String[] args){
AddressDAO addressdao=new AddressDAO(); Address address=new Address();
address.setDetail("江西生物科技职业学院!");
Person p=new Person();
p.setName("guozhen");
p.setAge(21);
p.setAddress(address);
address.setPerson(p);
addressdao.addAddress(address);
} }

Test

注意:在one-to-one中写上property-ref=“” 属性为另一持久化了类的相关联的属性名

hibernate一对一关联的更多相关文章

  1. Hibernate一对一关联映射配置

    一.一对一关联 Hibernate提供了两种映射一对一关联关系的方式:按照外键映射和按照主键映射.下面以员工账号和员工档案表为例,介绍这两种映射方式,并使用这两种映射方式分别完成以下持久化操作: (1 ...

  2. 04.Hibernate一对一关联

        前言:本文主要介绍使用Hibernate映射一对一的关联关系的两种方式:使用外键映射.使用主键映射. 1.数据库表的一对一关联关系     本文根据客户信息表(tb_customer)和地址信 ...

  3. 【学习笔记】Hibernate 一对一关联映射 组件映射 二级缓存 集合缓存

    啊讲道理放假这十天不到啊 感觉生活中充满了绝望 这就又开学了 好吧好吧继续学习笔记?还是什么的 一对一关联映射 这次我们仍然准备了两个表 一个是用户表Users 一个是档案表Resume 他们的关系是 ...

  4. Hibernate 一对一关联映射,mappedBy参数解析

    在最近java,SSH框架的学习中遇到了这样的一个问题,在Hibernate的开发中一对一关联映射的单向关联,主表会在次表新增一列次表的主键如下图,但是在双向关联中次表不会在表中创建主表的主键一列,这 ...

  5. Hibernate 一对一关联查询

    版权声明:本文为博主原创文章,如需转载请标注转载地址. 博客地址:http://www.cnblogs.com/caoyc/p/5602418.html  一对一关联,可以分为两种.一种是基于外键的关 ...

  6. Hibernate 一对一关联映射

    package com.entity; import javax.persistence.Entity; import javax.persistence.OneToOne; @Entity publ ...

  7. hibernate一对一关联手动改表后No row with the given identifier exists:

    articleId手动改了一个并不存在的值 把被控端的id改成存在的就好了

  8. hibernate一对一外键双向关联

    关联是类(类的实例)之间的关系,表示有意义和值得关注的连接. 本系列将介绍Hibernate中主要的几种关联映射 Hibernate一对一主键单向关联Hibernate一对一主键双向关联Hiberna ...

  9. hibernate一对一外键单向关联

    关联是类(类的实例)之间的关系,表示有意义和值得关注的连接. 本系列将介绍Hibernate中主要的几种关联映射 Hibernate一对一主键单向关联Hibernate一对一主键双向关联Hiberna ...

随机推荐

  1. 【传输对象】kafka传递实体类消息

    工具类 负责对象字节数组的相互转换,传输数据用 package com.yq.utils; import java.io.ByteArrayInputStream; import java.io.By ...

  2. 【第一章】 第一个spring boot程序

    环境: jdk:1.8.0_73 maven:3.3.9 spring-boot:1.2.5.RELEASE(在pom.xml中指定了) 注意:关于spring-boot的支持, 最少使用jdk7(j ...

  3. each遍历小结

    JQ中的遍历函数 (逐个加工函数) 格式: $(‘.box p’).each(function(index,element){ })也可以写成 $.each(‘.box p’,function(ind ...

  4. VS2010下创建WEBSERVICE,第二天 ----你会在C#的类库中添加web service引用吗?

    本文并不是什么高深的文章,只是VS2008应用中的一小部分,但小部分你不一定会,要不你试试: 本人对于分布式开发应用的并不多,这次正好有一个项目要应用web service,我的开发环境是vs2008 ...

  5. [Err] 1111 - Invalid use of group function

    本文为博主原创,未经允许不得转载: 初衷,本想通过group by sql语句查询出不同id下总数在一定范围内的数据,所以产生如下的sql,及错误sql AND STATDATE < ' GRO ...

  6. 改变checkbox样式问题

    选择1   选择2 选择3 选择4 选择5 <form action=""> <label for="test">选择1 <inp ...

  7. 【转】DrawDibDraw

    http://blog.csdn.net/normallife/article/details/53177315 BMP位图文件结构及平滑缩放 用普通方法显示BMP位图,占内存大,速度慢,在图形缩小时 ...

  8. STL_算法_06_遍历算法

    ◆ 常用的遍历算法: 1.1.用指定函数依次对指定范围内所有元素进行迭代访问.该函数不得修改序列中的元素 functor for_each(iteratorBegin, iteratorEnd, fu ...

  9. MySQL函数GROUP_CONCAT() 实现多条数据合并

    group_concat()会计算哪些行属于同一组,将属于同一组的列显示出来,group by指定的列进行分组. 例如: -- 根据物流订单id查询所有物流订单,车源订单,车辆信息(多条数据合并为一条 ...

  10. Unity中使用的一套敏感词过滤方式

    当项目中的敏感词数量不是很多的时候,直接用数组来遍历过滤其实也可以,但是具体的数量有多大,这个肯定不好说,因此,对.txt中的敏感词合理组织后再进行过滤就显得非常有必要了. 如上图,左边是txt中配置 ...