欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springboot jpa 非主键连接

程序员文章站 2022-04-25 07:56:14
...

springboot jpa 非主键连接

1. 问题

springboot jpa连接mysql,两个表通过主键管理则不需要额外做一些配置,但是遇到非主键关联时则需要修改

2. 具体代码

@Entity
@Data
@Table
@AllArgsConstructor
@NoArgsConstructor
public class Friend implements Serializable {
    /**
     * 主键
     */
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    private Long id;

    /**
     * 聊天号
     */
    @ManyToOne(cascade=CascadeType.PERSIST,fetch=FetchType.EAGER)
    @JoinColumn(name="selfMessageNumber",referencedColumnName = "messageNumber")
    private Member selfMessageNumber;

    /**
     * 聊天号
     */
    @ManyToOne(cascade=CascadeType.PERSIST,fetch=FetchType.EAGER)
    @JoinColumn(name="partnerMessageNumber",referencedColumnName = "messageNumber")
    private Member partnerMessageNumber;

    /**
     * 群组号
     */
    @ManyToOne
    @JoinColumn(name="group_id",referencedColumnName = "id")
    private FriendGroup friendGroup;

    /**
     * 0自增加1非自增加
     */
    @Enumerated(EnumType.ORDINAL)
    private AddFlagEnum addFlag;

    /**
     * 0未删除1已删除
     */
    private Integer deleteFlag;

    /**
     * 添加时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime insertTime;

    /**
     * 修改时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime updateTime;

    /**
     * 备注
     */
    private String remark;

}
@Entity
@Data
@Table
@AllArgsConstructor
@NoArgsConstructor
public class Friend implements Serializable {
    /**
     * 主键
     */
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    private Long id;

    /**
     * 聊天号
     */
    @ManyToOne(cascade=CascadeType.PERSIST,fetch=FetchType.EAGER)
    @JoinColumn(name="selfMessageNumber",referencedColumnName = "messageNumber")
    private Member selfMessageNumber;

    /**
     * 聊天号
     */
    @ManyToOne(cascade=CascadeType.PERSIST,fetch=FetchType.EAGER)
    @JoinColumn(name="partnerMessageNumber",referencedColumnName = "messageNumber")
    private Member partnerMessageNumber;

    /**
     * 群组号
     */
    @ManyToOne
    @JoinColumn(name="group_id",referencedColumnName = "id")
    private FriendGroup friendGroup;

    /**
     * 0自增加1非自增加
     */
    @Enumerated(EnumType.ORDINAL)
    private AddFlagEnum addFlag;

    /**
     * 0未删除1已删除
     */
    private Integer deleteFlag;

    /**
     * 添加时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime insertTime;

    /**
     * 修改时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime updateTime;

    /**
     * 备注
     */
    private String remark;

}

3.解决方式

  1. 请注意Friend实体类的selfMessageNumber、partnerMessageNumber属性,@JoinColumn(name="selfMessageNumber",referencedColumnName = "messageNumber")这个注解就是Friend的selfMessageNumber->Member的messageNumber,以此指定关联
  2. 实体类必须implements Serializable ,实体类必须implements Serializable ,实体类必须implements Serializable 重要的事情说三遍