hibernate一对一主键单向关联

关联是类(类的实例)之间的关系,表示有意义和值得关注的连接。

本系列将介绍Hibernate中主要的几种关联映射

Hibernate一对一主键单向关联
Hibernate一对一主键双向关联
Hibernate一对一外键单向关联
Hibernate一对一外键双向关联
Hibernate多对一单向关联
Hibernate多对一双向关联

Hibernate多对多关联

代码都写有注释,主要包括(核心配置文件,实体映射文件,实体类,测试类,数据库)主要操作有增删改查。

本篇主要介绍Hibernate一对一主键单向关联:

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!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.driver_class">com.mysql.jdbc.Driver</property>
        <!-- 配置数据库连接字符串 -->
        <property name="connection.url"><![CDATA[jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf8]]></property>
        <!-- 配置数据库连接用户名 -->
        <property name="connection.username">root</property>
        <!-- 配置数据库连接密码 -->
        <property name="connection.password">123456</property>
        <!-- 配置数据库方言 -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <!-- 配置数据库表生成策略 -->
        <!--<property name="hibernate.hbm2ddl.auto">update</property> -->
        <!-- 配置是否打印显示SQL语句 -->
        <property name="show_sql">true</property>
        <!-- 配置是否格式化显示SQL语句 -->
        <property name="format_sql">true</property>
        <!-- 配置hibernate是否自动提交事务 -->
        <!--<property name="hibernate.connection.autocommit">true</property> -->
        <!-- 配置实体类对应的映射文件 -->

        <!-- 一对一主键单向关联 -->
        <mapping resource="com/great/entity3/People.hbm.xml"></mapping>
        <mapping resource="com/great/entity3/IdCard.hbm.xml"></mapping>

    </session-factory>
</hibernate-configuration>

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 package="com.great.entity3">
    <!-- 配置实体类与数据库表的映射关系 -->
    <class name="com.great.entity3.People" table="people">
        <!-- 配置主键映射关系 -->
        <id name="id" column="id" type="int">
            <!-- 配置主键生成策略 -->
            <generator class="foreign">
                <param name="property">idCard</param>
            </generator>
        </id>
        <!-- 配置属性和表字段映射关系 -->
        <property name="pName" column="pName" type="string"></property>
        <one-to-one name="idCard" constrained="true" cascade="all" />
    </class>
</hibernate-mapping>

IdCard.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">
<hibernate-mapping package="com.great.entity3">
    <class name="IdCard" table="idcard">
        <id name="id" column="id" type="int">
            <generator class="native">

            </generator>
        </id>
        <!-- 配置属性和表字段映射关系 -->
        <property name="CardCode" column="cardCode" type="string"></property>

    </class>
</hibernate-mapping>

People.java

package com.great.entity3;

public class People {
    // 一对一主键单向关联
    private int id;
    private String pName;
    private IdCard idCard;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getpName() {
        return pName;
    }

    public void setpName(String pName) {
        this.pName = pName;
    }

    public IdCard getIdCard() {
        return idCard;
    }

    public void setIdCard(IdCard idCard) {
        this.idCard = idCard;
    }

}

IdCard.java

package com.great.entity3;

public class IdCard {
    private int id;
    private String cardCode;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getCardCode() {
        return CardCode;
    }

    public void setCardCode(String cardCode) {
        CardCode = cardCode;
    }

} 

TestOneToOneUniqueUn.java(测试类)

package com.great.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import com.great.entity3.IdCard;
import com.great.entity3.People;

public class TestOneToOneUniqueUnidirectional {
    static Session session;

    // 一对一单向主键关联
    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        // 加载hibernate主配置文件
        Configuration cfg = new Configuration().configure();
        // 构建session工厂
        SessionFactory sf = cfg.buildSessionFactory();
        // 打开session
        session = sf.openSession();
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        // 关闭session,释放资源
        session.close();
    }

    // select1, 通过idCard查询people
    @Test
    public void testOneToOneSelect1() {
        People people = (People) session.get(People.class, 1);
        System.out.println("身份证号码:" + people.getIdCard().getCardCode());
        System.out.println("姓名:" + people.getpName());
    }

    // insert1,通过保存people保存数据 两条insert,先保存inCard,再保存people
    @Test
    public void testOneToOneSave1() {
        Transaction ts = session.beginTransaction();
        IdCard idCard = new IdCard();
        idCard.setCardCode("311487199505051234");
        People people = new People();
        people.setpName("墨樱22");
        people.setIdCard(idCard);
        session.save(people);
        ts.commit();
    }

    // delete1, 通过people删除数据 两条select,两条delete,先delete people,再delete idCard
    @Test
    public void testOneToOneDelete1() {
        Transaction ts = session.beginTransaction();
        People people = (People) session.get(People.class, 15);
        session.delete(people);
        ts.commit();
    }

    // update1,通过people更新数据 两条select,两条update,先update people,再update inCard
    @Test
    public void testOneToOneUpdate1() {
        Transaction ts = session.beginTransaction();
        People people = (People) session.get(People.class, 4);
        people.setpName("张起灵");
        people.getIdCard().setCardCode("888888");
        session.update(people);
        ts.commit();
    }
}

数据库:

people表

两个字段id pName

idcard表

两个字段id cardCode

注意:在people表上建立外键关系。

上一篇:POJ 2893 M × N Puzzle(树状数组求逆序对)


下一篇:HDU 2147kiki's game(巴什博弈变形)