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

jps 实体关联 OneToMany 双向关联 (ManyToOne)

程序员文章站 2022-03-02 15:25:42
...

jps 实体关联 OneToMany 双向关联 (ManyToOne) 例子(可以对一的 一方操作)

多的一方
/**
 * @Author: sunyanhui
 * @Date: 2021/3/2 14:25
 */
@Entity
@Table(name = "shop_pos_screen_setting")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class ShopPOSScreenSettingEntity extends BaseBusinessEntity {

    @Column(name = "shop_id")
    private Long shopId;

    @Column(name = "switching_time")
    private Integer switchingTime;

    @OneToMany(mappedBy = "shopPOSScreenSettingEntity", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @OrderBy("sort")
    private List<ShopPOSScreenItemEntity> shopPOSScreenItemEntityList;


    public ShopPOSScreenSettingEntity( Long shopId) {
        this.shopId = shopId;
    }
}

一的一方
/**
 * @Author: sunyanhui
 * @Date: 2021/3/2 17:33
 */
@Entity
@Table(name = "shop_pos_screen_item")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class ShopPOSScreenItemEntity {

    @Id
    @Column(name = "id")
    private Long id;

    @Column(name = "shop_id")
    private Long shopId;

    @Column(name = "type")
    private Type type;

    @Column(name = "url")
    private String url;

    @Column(name = "sort")
    private Integer sort;

    @ManyToOne
    @JoinColumn(name = "shop_id", referencedColumnName = "shop_id", insertable = false, updatable = false)
    @NotFound(action = NotFoundAction.IGNORE)
    @JsonIgnore
    private ShopPOSScreenSettingEntity shopPOSScreenSettingEntity;

    public ShopPOSScreenItemEntity(Long id, Long shopId, Type type, String url, Integer sort) {
        this.id = id;
        this.shopId = shopId;
        this.type = type;
        this.url = url;
        this.sort = sort;
    }

    /*0:图片, 1:视频*/
    public enum Type {
        PICTURE,
        VIDEO
    }
}


相关标签: jpa java