I am having issue this query i am getting error when c doesn’t have parent
@Query("SELECT c FROM Category c WHERE (c.parent = NULL OR c.parent.enable = true) AND c.enable = true AND c.alias = ?1") public Category findByAliasEnabled(String alias);
java.lang.NullPointerException: Cannot invoke “Com.entity.category.Category.getId()” because “category” is null
i am tring to get data when c.parent is enable or c doesn’t have parent
2
The query is trying to access the c.parent.enable property even when c.parent is null, which causes a NullPointerException.
You can modify your query logic to explicitly handle the case where c.parent is null.
@Query("SELECT c FROM Category c WHERE (c.parent IS NULL OR c.parent.enable = true) AND c.enable = true AND c.alias = ?1")
public Category findByAliasEnabled(String alias);
1
Try a JPQL query string of the form:
"SELECT c FROM Category c left join c.parent parent WHERE (parent is null OR parent.enable = true) AND c.enable = true AND c.alias = ?1"
Defining the left join over the c.parent relationship should allow you to return category objects that don’t have parents while still using it in filters in the query.
@Query("SELECT p FROM Product p WHERE p.category.enable= true AND p.category.parent.enable= true AND p.category.parent.enable= true AND p.category.parent.parent.alias LIKE %?1% OR CONCAT(p.id, ' ', p.name, ' ', p.alias, ' ',p.category.alias, ' ',p.category.parent.alias) LIKE %?1%")
Page<Product> findAll(String keyword, Pageable pageable);
retrieve Product entities based on a keyword that can match various fields including the parent and grandparent category aliases, and product fields