【问题标题】:Why is my data getting associated with wrong column in mysql db?为什么我的数据与 mysql db 中的错误列相关联?
【发布时间】:2020-06-28 21:46:38
【问题描述】:

我有两个使用复合键以多对多关系连接的表:

表1

    @Entity
    @Table(name = "user")
    public class User {

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private int id;

        @Column(name = "user_name")
        private String userName;

        @Column(name = "first_name")
        private String firstName;

        @Column(name = "last_name")
        private String lastName;

        private String password;

        private String authorization;

        @OneToMany(
                mappedBy = "user",
                cascade = CascadeType.ALL,
                orphanRemoval = true
        )
        @JsonManagedReference
        private List<UserProduct> userProducts = new ArrayList<>();

        @OneToMany(
                mappedBy = "user",
                cascade = CascadeType.ALL,
                orphanRemoval = true
        )
        private List<Orders> orders = new ArrayList<>();

表2

@Entity
@Table(name = "product")
public class Product {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    private String name;

    private int price;

    private double mass;

    private double alcohol;

    private String picture;

    private int amount;

    @OneToMany(
            mappedBy = "user",
            cascade = CascadeType.ALL,
            orphanRemoval = true
    )
    private List<UserProduct> userProducts = new ArrayList<>();

    @OneToMany(
            mappedBy = "orders",
            cascade = CascadeType.ALL,
            orphanRemoval = true
    )
    private List<OrderProduct> orderProducts = new ArrayList<>();

带有复合键的表

@Entity
@Table(name = "user_product")
public class UserProduct {

    @EmbeddedId
    private UserProductId id;

    @ManyToOne(fetch = FetchType.LAZY)
    @MapsId("userId")
    @JsonBackReference
    private User user;

    @ManyToOne(fetch = FetchType.LAZY)
    @MapsId("productId")
    private Product product;

    @Column(name = "amount_of_new_products")
    private int amountOfNewProducts;

当我对 UserProduct 表进行 REST 调用时,我可以使用此有效负载更新产品值:

{
    "user": 4,
    "product": 2,
    "amountOfNewProducts": 32
}

它根据用户 ID 而不是产品 ID 写入信息。对于这个有效载荷,它会这样写:

{
        "id": 3,
        "name": "Grimbergen Blanche",
        "price": 132,
        "mass": 0.33,
        "alcohol": 6.0,
        "picture": "https://i.imgur.com/qIq1OrC.png",
        "amount": 502,
        "userProducts": [],
        "orderProducts": []
    },
    {
        "id": 4,
        "name": "Grimbergen Blonde",
        "price": 132,
        "mass": 0.33,
        "alcohol": 6.7,
        "picture": "https://i.imgur.com/OnioHd5.png",
        "amount": 435,
        "userProducts": [
            {
                "id": {
                    "productId": 2,
                    "userId": 4
                },
                "product": {
                    "id": 2,
                    "name": "Lav Premium",
                    "price": 73,
                    "mass": 0.33,
                    "alcohol": 4.9,
                    "picture": "https://i.imgur.com/T3gCAOE.png",
                    "amount": 1862,
                    "userProducts": [],
                    "orderProducts": []
                },
                "amountOfNewProducts": 32
            }
        ],
        "orderProducts": []
    },

因此,基本上即使我将 2 作为产品 id 传递,信息也会写入 id 为 4 的产品中,因为用户 id 是 4。任何我可能会搞砸的线索将不胜感激。

【问题讨论】:

    标签: mysql hibernate jpa spring-data-jpa many-to-many


    【解决方案1】:

    您需要在 Product 实体中修复 mappedBy = "user"。一定是"product",像这样:

    @OneToMany(
                mappedBy = "product",
                cascade = CascadeType.ALL,
                orphanRemoval = true
        )
        private List<UserProduct> userProducts = new ArrayList<>();
    

    还要检查UserProductId 中的@JoinColumn 是否正确(不幸的是,您没有在问题中输入代码)。

    【讨论】:

      猜你喜欢
      • 2020-12-12
      • 2011-02-26
      • 2015-04-14
      • 2022-11-02
      • 2020-12-17
      • 2015-03-05
      • 1970-01-01
      • 2019-09-21
      • 1970-01-01
      相关资源
      最近更新 更多