我想知道是否可以为Spring的CrudRepository
保存方法定义自定义返回类型,如以下示例中的find查询方法所示:
<T> Optional<T> findById(Long id, Class<T> type);
In the documentation我仅找到带有查询方法的动态投影示例。
我试过了
<T> T save(Foo entity, Class<T> type);
但出现以下错误:
java.lang.IllegalArgumentException: Failed to create query for method public abstract java.lang.Object com.xyz.myproject.persistence.dao.FooDAO.save(com.xyz.myproject.persistence.model.Foo,java.lang.Class)! No property save found for type Foo!
任何想法如何使它工作?
最佳答案
我认为不是,应该是Foo save(Foo entity);
或来自文档
Example 30. Fragments overriding save(…)
interface CustomizedSave<T> {
<S extends T> S save(S entity);
}
class CustomizedSaveImpl<T> implements CustomizedSave<T> {
public <S extends T> S save(S entity) {
// Your custom implementation
}
}
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.custom-implementations
关于java - Spring CrudRepository保存方法的自定义返回类型(动态投影),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55853729/