Return 即使外键在 HQL 中为 null 的数据
Return data even if the foreign key is null in HQL
我有一个 Product
Table 连接 ProductCategory
table 与 @ManyToOne
和 @OneToMany
关系。
Product
:
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "category_id")
@NotFound(action=NotFoundAction.IGNORE)
private ProductCategory productCategory;
Category
:
@OneToMany(fetch = FetchType.LAZY, mappedBy="productCategory" ,cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
@JsonBackReference
@OrderBy("id desc")
private Set<Product> products = new HashSet<Product>();
我有这个从 Product
table:
检索数据的存储库方法
@Query("SELECT p from Product p where cast(p.id as string) like :x or p.name like :x or p.description like :x or p.sku like :x or p.productCategory.name like :x")
public Page<Product> search(@Param("x") String keyword, Pageable pageable);
此方法不会 return 类别为空的产品。
即使 productCategory
列为空,如何检索产品。
我通过在查询中添加(实际上)LEFT JOIN
解决了问题:
"SELECT p from Product p left join p.productCategory where cast(p.id as string) like :x or p.name like :x or p.description like :x or p.sku like :x or p.productCategory.name like :x"
我有一个 Product
Table 连接 ProductCategory
table 与 @ManyToOne
和 @OneToMany
关系。
Product
:
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "category_id")
@NotFound(action=NotFoundAction.IGNORE)
private ProductCategory productCategory;
Category
:
@OneToMany(fetch = FetchType.LAZY, mappedBy="productCategory" ,cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
@JsonBackReference
@OrderBy("id desc")
private Set<Product> products = new HashSet<Product>();
我有这个从 Product
table:
@Query("SELECT p from Product p where cast(p.id as string) like :x or p.name like :x or p.description like :x or p.sku like :x or p.productCategory.name like :x")
public Page<Product> search(@Param("x") String keyword, Pageable pageable);
此方法不会 return 类别为空的产品。
即使 productCategory
列为空,如何检索产品。
我通过在查询中添加(实际上)LEFT JOIN
解决了问题:
"SELECT p from Product p left join p.productCategory where cast(p.id as string) like :x or p.name like :x or p.description like :x or p.sku like :x or p.productCategory.name like :x"