Null check on input object in spring boot jpa query

Viewed 632

I've got this object:

@Getter
@Setter
public class PhaseBean {
   private Long id;
   private String name;
   private PhaseBean subPhase;
}

that is passed as input to this repo method (simplified):

@Query(value= 
  " ... 
    and ( :#{#phaseBean.id} is null or :#{#phaseBean.name} is null ....                  //row1
    and ( :#{#phaseBean.subPhase} is null or :#{#phaseBean.subPhase.id} is null or ...   //row2
    ... "
)
List<Phase> load(@Param("phaseBean") PhaseBean phaseBean);

In the test that I've done, the phaseBean is valorized in this way:

id = 3 
name = null
subPhase = null

and when the repo method is executed, I get this error:

SpelEvaluationException: EL1007E: Property or field 'id' cannot be found on null

I did 2 different tests:

  1. comment "row1" of the query and execute the method -> same error
  2. comment "row2" of the query and execute the method -> method works!

So I'm sure that the problem is that the check :#{#phaseBean.subPhase} is null is not working for the inner object.
Any suggestion? Thanks a lot.

1 Answers

At the end I have to change the method signature to use String, Long ecc. as input instead of my custom object ( PhaseBean ); in this way I can use the "normal" sintax ":param" and everything works (I move the null controls of the PhaseBean objects in the caller method):

@Query(value= 
" ... 
and ( :subPhaseId is null or ...   //row2
... "
)
List<Phase> load(
...
@Param("phaseId") Long phaseId, @Param("subPhaseId") Long subPhaseId
...
);
Related