本章主要是描述几种经典映射关系,顺带比较Hibernate4.x和Hibernate5.x之间的区别。

一、建立测试工程目录

Hibernate 基础配置及常用功能(二)

有关实体类之间的相互映射关系,Hibernate官方文档其实描述的非常详细,这里只提供几种常见映射。(推荐4.3.11版本的 hibernate-release-4.3.11.Final\documentation\manual)

二、编写映射关系

(1)one2one单表内嵌映射:

package model.family;

import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Husband {
    private int id;
    private String husbandName;
    private Wife wife;

    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }

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

    public String getHusbandName() {
        return husbandName;
    }

    public void setHusbandName(String husbandName) {
        this.husbandName = husbandName;
    }

    // 两个实体对象共用一张数据表,提高查询速度
    @Embedded
    public Wife getWife() {
        return wife;
    }

    public void setWife(Wife wife) {
        this.wife = wife;
    }

}
Husband.java

相关文章:

  • 2022-12-23
  • 2021-12-10
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-16
猜你喜欢
  • 2021-06-27
  • 2021-11-15
  • 2021-08-25
  • 2022-12-23
  • 2021-12-20
  • 2021-05-16
  • 2021-11-11
相关资源
相似解决方案