我知道这个问题存在。但是,我读过很多帖子,寻找此错误,但无法解决我的问题。
我尝试为“多对多”关系执行内部联接,中间表必须像promotion_code_product下划线,但是我在@Query
上写了这样的东西,但失败了,错误是类似{{1 }}
这是我的查询:
Path expected for join 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")
答案 0 :(得分:1)
请记住,hql连接与常规sql连接不同。
使用hql,您可以对实体(而不是表)执行查询。
因此,将对实体中包含的字段执行联接。
假设您的产品类别包含一个称为“类别”的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")
希望这会有所帮助