我有两个表(product,cart),我想做一个查询,得到一个符合我需要的产品。
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;
}
}
目前我得到了两个具有相同hashcode的产品,每个产品都有两个cart,相反,我想得到一个产品 Cart
table。
有人能帮我做正确的查询吗?
1条答案
按热度按时间f45qwnt81#
乍一看,我可以看到产品实体没有实现
hashCode()
方法,我假设这应该在BaseEntity
. 如果你能确认我是对的,也许hashCode()
在那个类是错误的或是一个常数。请把你用来获取购物车相关产品的代码也放进去好吗?也许是出了什么问题。