6

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?

Oliver Drotbohm
  • 80,157
  • 18
  • 225
  • 211
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596

3 Answers3

11

As described in the reference documentation you can just use the IsTrue keyword:

AccomodationMedia findByIdAccomodationAndIsMasterIsTrue(Long accomodationId);
Oliver Drotbohm
  • 80,157
  • 18
  • 225
  • 211
5

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.

Community
  • 1
  • 1
radoh
  • 4,554
  • 5
  • 30
  • 45
0

There's no way you can set a default value into a method signature definition, you must tell Spring which is your parameter value explicitly. You could use a different approach in this case if you want to avoid setting your "isMaster" to true in your repository implementation. You could use the @Query annotation to define your own HQL query and provide the default value in you query String, this approach can be used whenever you want to create your own custom respository method using a named query instead of the named method definitions provided by Spring Data architecture.

In your particular case you can try something like this:

@Repository
public interface AccomodationMediaDAO extends JpaRepository<AccomodationMedia, Long> {

    @Query("SELECT a FROM AccomodationMedia a WHERE a.accomodationId = :accomodationId AND a.isMaster = true")
    AccomodationMedia findByIdAccomodationAndIsMaster(@Param(value = "accomodationId") Long accomodationId);

}

Please note that the properties mapped after the dot notation (a.accomodationId) name must match the entity local column names declared into your entity POJO, meaning that "accomodationId" must match the name of the variable that represents your ID column, same as applies for the "isMaster" property.

Daniel G.
  • 126
  • 1
  • 12