java - Spring CrudRepository保存方法的自定义返回类型(动态投影)

标签 java spring spring-boot spring-data-jpa spring-boot-jpa

我想知道是否可以为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/

相关文章:

java.lang.NullPointerException : Attempt to invoke virtual method on a null object reference Retrofit

java - href 语句中的正斜杠 '/' 保持恢复为反斜杠 '\' .jnlp 文件

java - 使用 Jackson 从嵌套 JSON 字符串分配变量

mysql - ibatis 无法重新返回批量插入的主键

java - Spring Tool Suite 找不到 JDBC 驱动程序类

spring - Grails:将 FixtureLoader 注入(inject)规范

Java 从 dictionary.txt 文件中获取 100 个随机单词

java - 将现有的 WAR 添加到嵌入式 tomcat

java - 如何为基于当前用户查找类的 CRUD 存储库方法编写自定义查询?

java - 如何在 Spring boot 中将带有前导零的整数绑定(bind)到请求负载中的整数字段?