有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java MySql内部联接结果会复制值

我有两个表(产品,购物车),我想做一个查询,得到一个符合我需要的产品

SELECT * FROM product;
+----+-------------+-------+---------------+----------+-------------+-----------+------------+
| id | description | image | name          | quantity | supplier_id | productid | product_id |
+----+-------------+-------+---------------+----------+-------------+-----------+------------+
| 8  | Desc 1      | NULL  | Product 1     | NULL     | 6           | NULL      | NULL       |
| 15 | asd         | NULL  | asd           | NULL     | 6           | NULL      | NULL       |
| 35 | asdsadsad   | NULL  | test producdt | NULL     | 6           | NULL      | NULL       |
| 45 | NULL        | NULL  | asdas         | NULL     | 6           | 514       | NULL       |
+----+-------------+-------+---------------+----------+-------------+-----------+------------+
SELECT * FROM cart;
+-----+-------+----------+------------+---------+
| id  | price | quantity | product_id | user_id |
+-----+-------+----------+------------+---------+
| 141 | 100   | 1        | 8          | 26      |
| 139 | 100   | 1        | 8          | 26      |
+-----+-------+----------+------------+---------+
SELECT *
FROM product
INNER JOIN cart
    ON cart.user_id = 26
    AND cart.product_id = product.id
    AND product.supplier_id = 6
+----+-------------+-------+---------------+----------+-------------+-----------+------------+-----+-------+----------+------------+---------+
| id | description | image | name          | quantity | supplier_id | productid | product_id | id  | price | quantity | product_id | user_id |
+----+-------------+-------+---------------+----------+-------------+-----------+------------+-----+-------+----------+------------+---------+
| 8  | Desc 1      | NULL  | Product 1     | NULL     | 6           | NULL      | NULL       | 141 | 100   | 1        | 8          | 26      |
| 8  | Desc 1      | NULL  | Product 1     | NULL     | 6           | NULL      | NULL       | 139 | 100   | 1        | 8          | 26      |
+----+-------------+-------+---------------+----------+-------------+-----------+------------+-----+-------+----------+------------+---------+

购物车

@Entity
public class Cart extends BaseEntity implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    @JsonIgnore
    @ManyToOne
    @JoinColumn(name = "user_id")
    private User user;
    @JsonManagedReference
    @ManyToOne
    @JoinColumn(name = "product_id")
    private Product product;
    private Integer quantity;
    private Double price;
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "cart_property_option",
            joinColumns = @JoinColumn(name = "cart_id"),
            inverseJoinColumns = @JoinColumn(name = "property_option_id"))
    private Set<PropertyOption> selectedPropertyOptions;


    public Cart() {
    }

    public Cart(User user, Product product, Integer quantity) {
        this.user = user;
        this.product = product;
        this.quantity = quantity;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public Integer getQuantity() {
        return quantity;
    }

    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }

    public Integer getId() {
        return id;
    }

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

    public Set<PropertyOption> getSelectedPropertyOptions() {
        return selectedPropertyOptions;
    }

    public void setSelectedPropertyOptions(Set<PropertyOption> selectedPropertyOptions) {
        this.selectedPropertyOptions = selectedPropertyOptions;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    @Override
    public boolean equals(Object obj) {
        if(!(obj instanceof Cart)) {
            return false;
        }
        return this.id.equals(((Cart) obj).getId());
    }

    @Override
    public int hashCode() {
        return 31 * 17 + id.hashCode();
    }
}

产品

@Entity
public class Product extends BaseEntity{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    private String name;
    private String description;
    private String productId;
    private String image;
    @JsonBackReference
    @ManyToOne
    @JoinColumn(name = "supplier_id")
    private Supplier supplier;
    @JsonBackReference
    @OneToMany(mappedBy = "product", cascade = CascadeType.REMOVE)
    private Set<Cart> carts;

    @JsonBackReference
    @OneToMany(mappedBy = "product", cascade = CascadeType.REMOVE)
    private Set<Price> prices;
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "product_productCategory",
            joinColumns = @JoinColumn(name = "product_id"),
            inverseJoinColumns = @JoinColumn(name = "category_id"))
    private Set<ProductCategory> productCategories;

    @JsonBackReference
    @OneToMany(fetch = FetchType.EAGER, mappedBy = "product", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<Property> properties;

    public void addProperty(Property property){
        if(properties == null) {
            properties = new HashSet<>();
        }
        properties.add(property);
    }

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Supplier getSupplier() {
        return supplier;
    }

    public void setSupplier(Supplier supplier) {
        this.supplier = supplier;
    }

    public Set<Cart> getCarts() {
        return carts;
    }

    public void setCarts(Set<Cart> carts) {
        this.carts = carts;
    }


    public Set<Price> getPrices() {
        return prices;
    }

    public void setPrices(Set<Price> prices) {
        this.prices = prices;
    }

    public Set<ProductCategory> getProductCategories() {
        return productCategories;
    }

    public void setProductCategories(Set<ProductCategory> productCategories) {
        this.productCategories = productCategories;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public Set<Property> getProperties() {
        return properties;
    }

    public void setProperties(Set<Property> properties) {
        this.properties = properties;
    }

    public String getProductId() {
        return productId;
    }

    public void setProductId(String productId) {
        this.productId = productId;
    }
}

目前,我得到了两个具有相同哈希代码的产品,每个产品都有两个购物车,相反,我想得到一个产品。我添加了具有不同属性的相同产品,构建了Cart

有人能帮我做正确的查询吗


共 (1) 个答案

  1. # 1 楼答案

    乍一看,我可以看到产品实体没有实现hashCode()方法,我假设这应该在超级类BaseEntity中实现。如果你能检查一下我是正确的,也许那个类中hashCode()的实现是错误的,或者是一个常量

    请同时输入您用来获取购物车相关产品的代码?也许是出了什么问题