У меня есть две таблицы (продукт, корзина), и я хочу сделать запрос, чтобы получить продукт, который соответствует моим потребностям.
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;
}
}
В настоящее время я получаю 2 продукта с одинаковым хэш-кодом, и в каждом из них есть 2 корзины, вместо этого я хочу получить один продукт. Я добавил один и тот же продукт с разными свойствами, который составил таблицу Cart
.
Может ли кто-нибудь помочь мне сделать правильный запрос?