Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

spring - Custom update in CrudRepository

is that possible to returns updated entity by custom update method instead of numbers of affected rows? How can I achieve this?

I would like to have sth like this:

 public interface DataRepository extends CrudRepository<Data, Long> {

 @Modifying
 @Query(value="UPDATE data SET max_version = max_version + 1 WHERE id = 'A'", nativeQuery=true)
 Data updateDataByType();
}

instead of this

 public interface DataRepository extends CrudRepository<Data, Long> {

 @Modifying
 @Query(value="UPDATE data SET max_version = max_version + 1 WHERE id = 'A'", nativeQuery=true)
 Integer updateDataByType();

}

question from:https://stackoverflow.com/questions/65857953/custom-update-in-crudrepository

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You cannot do this with the @Modifying annotation. because these methods can only be void and int. Otherwise you will get the error Modifying queries can only use void or int / Integer as return type.

But can be you can implement custom repository Implementation and return your updated entity after done with the query execution.

Reference: Custom Implementations for Spring Data Repositories


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...