首页 > 其他分享 >hibernate的OneToMany映射

hibernate的OneToMany映射

时间:2022-10-24 16:56:14浏览次数:51  
标签:hibernate name 映射 OneToMany Column private member value id

hibernate的OneToMany和ManyToOne映射

1、前言

在数据库中,不管一对多或者多对一都统称为:一对多。@OneToMany和@ManyToOne指定了一对多的关系。

2、@OneToMany使用外键映射的单向一对多关联

单向关联

entity:Member

@Entity
@Setter
@Getter
@Table(name = "jei_member")
public class Member implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    @ApiModelProperty(value = "id")
    private Long id;

    @Column(name = "name")
    @ApiModelProperty(value = "姓名")
    private String name;

    @Column(name = "gender")
    @ApiModelProperty(value = "性别")
    private Integer gender;

    @OneToMany(fetch = FetchType.LAZY)
    @JoinColumn(name = "member_id", referencedColumnName = "id", insertable = false, updatable = false)
    private Set<Order> orders;

    public Set<OrderDto> getOrderDtos() {
        if (orders == null) {
            return null;
        }
        return orders.stream().map(x -> new OrderDto(x.getId(), x.getProductName(), x.getProductNum(), x.getMemberId())).collect(Collectors.toSet());
    }

}

注意:

@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id", referencedColumnName = "id", insertable = false, updatable = false)

这里@JoinColumn()中name="member_id"指子表(MANY)中的字段, referencedColumnName = "id"指当前表(ONE)中的字段。

3、@ManyToOne使用外键映射的单向一对多关联

单向关联

entity:Order

@Entity
@Setter
@Getter
@Table(name = "jei_order")
public class Order implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    @ApiModelProperty(value = "id")
    private Long id;

    @Column(name = "product_name")
    @ApiModelProperty(value = "商品名称")
    private String productName;

    @Column(name = "product_num")
    @ApiModelProperty(value = "数量")
    private Integer productNum;

    @Column(name = "member_id")
    @ApiModelProperty(value = "会员id")
    private Long memberId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "member_id", referencedColumnName = "id", insertable = false, updatable = false)
    private Member member;

}

注意:

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id", referencedColumnName = "id", insertable = false, updatable = false)

这里的name=“member_id”, 指的当前表(MANY)的字段,referencedColumnName = "id"只能是主表(ONE)的主键id。

4、@OneToMany和@ManyToOne使用外键映射的双向一对多关联

entity:Menber

@Entity
@Setter
@Getter
@Table(name = "jei_member")
public class Member implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    @ApiModelProperty(value = "id")
    private Long id;

    @Column(name = "name")
    @ApiModelProperty(value = "姓名")
    private String name;

    @Column(name = "gender")
    @ApiModelProperty(value = "性别")
    private Integer gender;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "member")
    private Set<Order> orders;

    public Set<OrderDto> getOrderDtos() {
        if (orders == null) {
            return null;
        }
        return orders.stream().map(x -> new OrderDto(x.getId(), x.getProductName(), x.getProductNum(), x.getMemberId(), x.getMember().getName(), x.getMember().getGender()))
                .collect(Collectors.toSet());
    }
}

注意:

@OneToMany(fetch = FetchType.LAZY, mappedBy = "member")

这里不需要@JoinColumn进行声明关联,需要用mappedBy 来确定关系由谁来维护。mappedBy="member"说明在两个关联的实体Bean中,member这一端是关系的拥有者。

entity:Order

@Entity
@Setter
@Getter
@Table(name = "jei_order")
public class Order implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    @ApiModelProperty(value = "id")
    private Long id;

    @Column(name = "product_name")
    @ApiModelProperty(value = "商品名称")
    private String productName;

    @Column(name = "product_num")
    @ApiModelProperty(value = "数量")
    private Integer productNum;

    @Column(name = "member_id")
    @ApiModelProperty(value = "会员id")
    private Long memberId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "member_id", referencedColumnName = "id",insertable = false,updatable = false)
    private Member member;

}

注意:

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id", referencedColumnName = "id", insertable = false, updatable = false)

这里的name=“member_id”, 指的当前表(MANY)的字段,referencedColumnName = "id"只能是主表(ONE)的主键id。

总结:
1、@JoinColumn(name = "many_id", referencedColumnName = "one_id", insertable = false, updatable = false)
2、在一对多的情况下,一定要注意死循环的问题,实体类中使用@Setter、@Getter。@data双向关联的情况下,会出现栈溢出。

标签:hibernate,name,映射,OneToMany,Column,private,member,value,id
From: https://www.cnblogs.com/jspider/p/16821983.html

相关文章

  • 磁盘映射
    步骤1:打开远程连接(可以点左下角开始>运行>输入mstsc回车)步骤2:点选项在出来的窗口中点“本地连接”,再点详细信息,然后在出来的窗口中的驱动器勾选一个你的映射到服务器上的......
  • 新功能发布 | TSMaster重大更新,一文了解信号映射操作流程
    前言今天介绍TSMaster新功能—信号映射。这个功能在旧版本里,只能实现从CAN信号到系统变量的映射,软件更新以后可以支持系统变量反向映射到信号,甚至可以实现通过表达方式进行......
  • NHibernate 1.0 Released 版本发布了
    NHibernateisaportofHibernatetothe.NETplatform.Hibernateistheleadingopen-sourceobject-relationalpersistencelibraryforJava,usedtopersistpla......
  • Hibernate缓存及核心接口类详解
    Hibernate缓存概述一级缓存(session级别缓存)也叫事务级别的缓存二级缓存(sessionFactory缓存)也叫应用级缓存三级缓存(查询缓存)区别:一级缓存的生命周期和session的生命......
  • 天线阵列fft角度映射
    相位差:Δφ=2πdsin(θ)/λ=2π/N其中,d是天线间距,θ是入射角,λ是载频波长λ=f0/c,N是沿着天线阵列维度(例如:86根天线)的fft采样点数。将角度维fft映射到[0,2π]的相位,得到......
  • hibernate的OneToOne映射
    hibernate的OneToOne映射1、前言@OneToOne注解可以建立实体bean之间的一对一的关联。一对一关联有四种情况:一是关联的实体都共享同样的主键;二是其中一个实体通过外键......
  • ASP.net EF动态映射实体
    1、配置EF与建立实体模型这里不做过多介绍、主要介绍如何动态映射实体模型1.1、实现过程有很多种方式我们这里使用接口、然后扫描所有继承了该接口的实体类然后映射(也可......
  • 静态资源映射
    web开发中,不可避免地是对静态资源的访问,SpringBoot默认的静态资源映射为三样:classpath:/META-INF/resources/classpath:/resources/classpath:/static/classpath:/pu......
  • springMvc路径参数映射原理
     一、springMvc 执行流程图 二、springMvc中url映射接口关键点2.1:AbstractHandlerMappingorg.springframework.web.servlet.handler.AbstractHandlerMapping#getHa......
  • Hibernate的关键API详解
    在Java中使用Hibernate的步骤​开始Hibernate开发​​......