I am not so into Hibernate and Spring Data JPA and I have the following doubt.
I have this method signature that correctly perform a query:
@Repository
public interface AccomodationMediaDAO extends JpaRepository<AccomodationMedia, Long> {
AccomodationMedia findByIdAccomodationAndIsMaster(Long accomodationId, boolean isMaster);
}
It finds (on the table mapped by the AccomodationMedia entity class) a single record having the field named idAccomodation setted with the Long value represented by the accomodationId method parameter and the field isMaster reppresented by the isMaster boolean.
It works fine but my "problem" is that doing in this way I have always to explicitly pass the value of the boolean isMaster parameter.
This parameter has to be always set as true
, so I tried to change the previous method signature in this way:
AccomodationMedia findByIdAccomodationAndIsMaster(Long accomodationId, true);
but IntelliJ displays an error: Identifier or type expected.
Why? Can I set that the value of this parameter have to be explicitly set to the true boolean value?
As described in the reference documentation you can just use the IsTrue
keyword:
AccomodationMedia findByIdAccomodationAndIsMasterIsTrue(Long accomodationId);
If you're using Java 8, you can overload it with a default
implementation:
public interface AccomodationMediaDAO extends JpaRepository<AccomodationMedia, Long> {
AccomodationMedia findByIdAccomodationAndIsMaster(Long accomodationId, boolean isMaster);
default AccomodationMedia findByIdAccomodationAndIsMaster(Long accomodationId) {
return findByIdAccomodationAndIsMaster(accomodationId, true);
}
}
But you can't provide a default value to a method parameter. E.g. see this question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With