java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Hibernate映射关系

Hibernate双向一对一映射关系配置代码实例

作者:Y_wee

这篇文章主要介绍了Hibernate双向一对一映射关系配置代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1、外键映射

1.1、实体类

package com.yl.bean;

import java.io.Serializable;

/**
 * 公民实体类
 */
public class Person implements Serializable {
  private Integer id;
  private String name;
  private PersonCard card;

  public Person() {
  }

  public Person(Integer id, String name, PersonCard card) {
    this.id = id;
    this.name = name;
    this.card = card;
  }

  public Integer getId() {
    return id;
  }

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

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public PersonCard getCard() {
    return card;
  }

  public void setCard(PersonCard card) {
    this.card = card;
  }

  @Override
  public String toString() {
    return "Person{" +
        "id=" + id +
        ", name='" + name + '\'' +
        ", card=" + card +
        '}';
  }
}
package com.yl.bean;

import java.io.Serializable;

/**
 * 身份证实体类
 */
public class PersonCard implements Serializable {
  private Integer id;
  private String cardNo;
  private Person person;

  public PersonCard() {
  }

  public PersonCard(Integer id, String cardNo, Person person) {
    this.id = id;
    this.cardNo = cardNo;
    this.person = person;
  }

  public Integer getId() {
    return id;
  }

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

  public String getCardNo() {
    return cardNo;
  }

  public void setCardNo(String cardNo) {
    this.cardNo = cardNo;
  }

  public Person getPerson() {
    return person;
  }

  public void setPerson(Person person) {
    this.person = person;
  }

  @Override
  public String toString() {
    return "PersonCard{" +
        "id=" + id +
        ", cardNo='" + cardNo + '\'' +
        ", person=" + person +
        '}';
  }
}

1.2、全局配置文件(hibernate.cfg.xml)

<!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="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate?characterEncoding=utf8&amp;serverTimezone=GMT%2B8</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">123456</property>
    <!--显示sql-->
    <property name="hibernate.show_sql">true</property>
    <!--自动创建表-->
    <property name="hibernate.hbm2ddl.auto">update</property>
    <!--指定映射配置文件的位置-->
    <mapping resource="com/yl/bean/Person.hbm.xml"></mapping>
    <mapping resource="com/yl/bean/PersonCard.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>

1.3、公民映射配置文件(Person.hbm.xml)

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <!--配置User类-->
  <class name="com.yl.bean.Person" table="t_person">
    <!--主键-->
    <id name="id" column="id" type="java.lang.Integer">
      <!--将主键交给本地数据库管理-->
      <generator class="native"></generator>
    </id>

    <property name="name" column="name" type="java.lang.String"></property>

    <one-to-one name="card" class="com.yl.bean.PersonCard"></one-to-one>
  </class>
</hibernate-mapping>

1.4、身份证映射配置文件(PersonCard.hbm.xml)

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <!--配置User类-->
  <class name="com.yl.bean.PersonCard" table="t_personcard">
    <!--主键-->
    <id name="id" column="id" type="java.lang.Integer">
      <!--将主键交给本地数据库管理-->
      <generator class="native"></generator>
    </id>

    <property name="cardNo" column="cardNo" type="java.lang.String"></property>

    <many-to-one name="person" column="person_id" unique="true"></many-to-one>
  </class>
</hibernate-mapping>

1.5、测试类

 @Test
  public void test01(){
    Person person=new Person();
    person.setName("yl001");

    PersonCard personCard=new PersonCard();
    personCard.setCardNo("001");

    person.setCard(personCard);
    personCard.setPerson(person);

    Session session= HibernateUtils.getSession();
    Transaction transaction=session.beginTransaction();
    session.save(person);
    session.save(personCard);
    transaction.commit();

    session.close();
  }

2、主键映射

表的字段既是主键,又是外键(eg:身份证号码既是主键又是外键)

2.1、映射配置文件实现

主要修改PersonCard.hbm.xml文件

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <!--配置User类-->
  <class name="com.yl.bean.PersonCard" table="t_personcard">
    <!--主键-->
    <id name="id" column="id" type="java.lang.Integer">
      <!--将主键交给本地数据库管理-->
      <generator class="native"></generator>
    </id>

    <property name="cardNo" column="cardNo" type="java.lang.String"></property>

    <!--<many-to-one name="person" column="person_id" unique="true"></many-to-one>-->
    <one-to-one name="person" class="Person" constrained="true"/>
  </class>
</hibernate-mapping>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文