亲宝软件园·资讯

展开

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

Y_wee 人气:0
想了解Hibernate双向一对一映射关系配置代码实例的相关内容吗,Y_wee在本文为您仔细讲解Hibernate映射关系的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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;

/**
 * SFZ实体类
 */
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&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、SFZ映射配置文件(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:SFZ号码既是主键又是外键)

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>

加载全部内容

相关教程
猜你喜欢
用户评论