简体   繁体   中英

How can I set that a Spring Data JPA query method have to be a parameter value always set as true?

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?

参考文档中所述,您只需使用IsTrue关键字:

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. Eg see this question .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM