gpt4 book ai didi

java - 加入 hql hibernate 的预期路径

转载 作者:行者123 更新时间:2023-11-30 05:34:54 25 4
gpt4 key购买 nike

我知道这个问题存在于SO中。但是我已经阅读了很多寻找此错误的帖子,但无法解决我的问题。

我尝试为“多对多”关系执行内部联接,中间表必须像promotion_code_product一样下划线,但我在@Query上写了类似这样的内容,但失败了,错误是这样的就像 加入 hql hibernate 的预期路径

这是我的查询:

 @Query("SELECT p.serviceName, p.category, v.code from Product p " 
+ " INNER JOIN PromotionCodeCategory cp ON p.id = cp.productId "
+ " INNER JOIN Voucher v on v.id = cp.promotionCodeId "
+ " WHERE v.code = ?1")

我也尝试过这样的事情:

@Query("SELECT p.serviceName, p.category, v.code from Product p INNER JOIN PromotionCodeProduct cp ON p.id = cp.productId INNER JOIN Voucher v on v.id = cp.promotionCodeId WHERE v.code = ?1")

像这样的实体:

@Table(name = "VOUCHER")
@DynamicUpdate
//@Data
@Setter
@Getter
public class Voucher extends Base {

@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinTable(name = "PROMOTION_CODE_PRODUCT",
joinColumns = @JoinColumn(name = "PROMOTION_CODE_ID"),
inverseJoinColumns = @JoinColumn(name = "PRODUCT_ID"))
private Set<Product> products;

@Column(name = "CODE", unique = true)
private String code;

@Column(name = "TYPE", nullable = false)
private String type;

@Lob
private String descriptions;

public int hasCode() {
return Objects.hashCode(getId());
}

}



@Entity
@Table(name = "PRODUCT")
//@Data
@Setter
@Getter
@DynamicUpdate
public class Product extends Base {

@ManyToMany(mappedBy = "products", fetch = FetchType.EAGER)
private Set<Voucher> promotionCodes;

@Column(name = "SERVICE_NAME", nullable = false)
private String serviceName;

@Column(name = "CATEGORY")
private String category;

@Column(name = "DESCRIPTION")
private String description;

public int hasCode() {
return Objects.hashCode(getId());
}

}

最佳答案

请记住,hql 连接与常规 sql 连接不同。

使用 hql,您可以对实体而不是表执行查询。

因此,对实体中包含的字段执行联接。

假设您的产品类别包含一个名为“categories”的 PromotionCodeCategory 列表或一组,并且您的 PromotionCodeCategory 包含一个名为“vouchers”的优惠券列表或一组,那么您的查询应如下所示:

@Query("SELECT p.serviceName, p.category, v.code from Product p " 
+ " INNER JOIN p.categories AS cp "
+ " INNER JOIN cp.vouchers as v "
+ " WHERE v.code = xxx")
<小时/>

编辑

值得注意的是,只有两个实体:产品和优惠券。因此你的类应该是这样的:

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

@ManyToMany(cascade = { CascadeType.ALL })
@JoinTable(
name = "product_voucher", // insert join table name here
joinColumns = { @JoinColumn(name = "product_id") }, // insert product column name from join table here
inverseJoinColumns = { @JoinColumn(name = "voucher_id") } // // insert voucher column name from join table here
)
Set<Voucher> vouchers = new HashSet<>();

// standard constructor/getters/setters
}


@Entity
@Table(name = "vouchers")
public class Voucher {

@ManyToMany(mappedBy = "vouchers")
private Set<Product> products = new HashSet<>();

// standard constructors/getters/setters
}

那么连接查询应该如下所示:

@Query("SELECT p.serviceName, p.category, v.code from Product p " 
+ " INNER JOIN p.vouchers AS v "
+ " WHERE v.code = xxx")

希望这有帮助

关于java - 加入 hql hibernate 的预期路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56855628/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com