hibernate 关联关系双向 中包含一对一关系,一对多关系,多对多关系,其实关联关系就是两个表,一个主建表,一个外键表,然后外键表对应着主键表的其中一个id,它们之间有着关联的关系,那么一个外键表中一个列对应一个主键表的一个列,这个我们所说一对一的关系,那么要是一个外键表中有两个列对应一个主键表的列,那么这个就是一对多,以此类推,多对多也是如此。
给大家介绍一下一对一关系一个列子,环境:Maven+Hibernate中的一个一对一关系的列子
前提:一个主键表:Person(人表),一个外键表:Crad(卡表)
package com.zking.entity;
//Person 表
public class Person {
private String pid;
private String pname;
//外键表
private Crad card;
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(String pid, String pname, Crad card) {
super();
this.pid = pid;
this.pname = pname;
this.card = card;
}
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public Crad getCard() {
return card;
}
public void setCard(Crad card) {
this.card = card;
}
}
package com.zking.entity;
import java.util.UUID;
//卡表
public class Crad {
//对于主键表中的pid
private String pid;
private String cno;
//设置UUID
private String cid=String.valueOf(UUID.randomUUID());
//主建表
private Person person;
public Crad() {
super();
// TODO Auto-generated constructor stub
}
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public String getCno() {
return cno;
}
public void setCno(String cno) {
this.cno = cno;
}
public String getCid() {
return cid;
}
public void setCid(String cid) {
this.cid = cid;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
@Override
public String toString() {
return "Card [pid=" + pid + ", cno=" + cno + ", cid=" + cid + ", person=" + person + "]";
}
}
然后设置hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">sasa</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<!-- 显示Sql语句 -->
<property name="show_sql">true</property>
<!-- 格式化Sql语句 -->
<property name="format_sql">true</property>
<!-- 映射关系 -->
<mapping resource="com/zking/entity/Crad.hbm.xml"/>
<mapping resource="com/zking/entity/Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
<mapping resource="com/zking/entity/Crad.hbm.xml"/>映射到Crad表中
<mapping resource="com/zking/entity/Person.hbm.xml"/>映射到Person中
生成实体类的hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2018-9-9 9:00:34 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.zking.entity.Crad" table="CRAD">
<id name="pid" type="java.lang.String">
<column name="pid" />
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
<property name="cid" type="java.lang.String">
<column name="CID" />
</property>
<property name="cno" type="java.lang.String">
<column name="CNO" />
</property>
<!--对应着主键表-->
<one-to-one name="person" class="com.zking.entity.Person"></one-to-one>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2018-9-9 9:00:34 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.zking.entity.Person" table="PERSON">
<id name="pid" type="java.lang.String">
<column name="PID" />
<generator class="guid" />
</id>
<property name="pname" type="java.lang.String">
<column name="PNAME" />
</property>
<!--对应着外键表-->
<one-to-one name="card" class="com.zking.entity.Crad" cascade="delete"></one-to-one>
</class>
</hibernate-mapping>
主表中:<one-to-noe name="主表中定义从表的对象名" class="从表类全限定名">
从表中:<one-to-noe name="从表中定义主表的对象名" class="主表类全限定名">
实现类:
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import com.zking.entity.Crad;
import com.zking.entity.Person;
public class Mytest {
@Test
public void OneToOne() {
Configuration configuration=new Configuration().configure();
SessionFactory sessionFactory=configuration.buildSessionFactory();
Session session=sessionFactory.openSession();
Transaction transaction=session.beginTransaction();
//人的表
Person p1=new Person();
p1.setPid("999fec40-b3cd-11e8-b394-54e1ada2ecc3");
p1.setPname("jackon");
//卡的表
Crad c1=new Crad();
c1.setPid("999fec40-b3cd-11e8-b394-54e1ada2ecc3");
c1.setCno("192454");
c1.setPerson(p1);
p1.setCard(c1);
System.out.println(c1);
//session.save(p1);
session.delete(p1);
transaction.commit();
sessionFactory.close();
}
}
在这里我使用到cascade(级联关系),我在这里就使用到一个增加,一个删除,效果是:主建表增加时,从表中也会随之增加,
删除时主表删除一条数据时,从表中对应主键表中的id也会删除。
增加:cascade="save-update" 删除:cascade="delete"
//主表Person
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2018-9-9 9:00:34 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.zking.entity.Person" table="PERSON">
<id name="pid" type="java.lang.String">
<column name="PID" />
<generator class="guid" />
</id>
<property name="pname" type="java.lang.String">
<column name="PNAME" />
</property>
<one-to-one name="card" class="com.zking.entity.Crad" cascade="save-update"></one-to-one>
</class>
</hibernate-mapping>
注意删除时候:
(Crad中要把pid设为主键)
以上就是简单的一对一关系:
注意:别忘记给从表中的id 设置UUID,在创建实体类中要与数据库中的一致,在实现类方法中要互设,在删除级联时候:在外键中的数据库改一下地方(在外键表把主键给与主键表相关联的列),关键点:删除时一定要set.跟主表关联的列。
这是我简单总结的一对一关系,有不足的地方请多多指教!