【问题标题】:Missing CrudRepository#findOne method缺少 CrudRepository#findOne 方法
【发布时间】:2017-10-21 09:56:59
【问题描述】:

我在我的项目中使用 Spring 5。直到今天还有可用的方法CrudRepository#findOne

但下载最新的快照后它突然消失了!有没有参考说明该方法现在不可用?

我的依赖列表:

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'


repositories {
    mavenCentral()
    maven { url "https://repo.spring.io/snapshot" }
    maven { url "https://repo.spring.io/milestone" }
}    

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-data-jpa'

    runtime 'com.h2database:h2:1.4.194'
}

更新:

好像这个方法已经换成CrudRepository#findById

【问题讨论】:

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


    【解决方案1】:

    请参阅与this commit 相关联的DATACMNS-944,该this commit 具有以下重命名

    ╔═════════════════════╦═══════════════════════╗
    ║      Old name       ║       New name        ║
    ╠═════════════════════╬═══════════════════════╣
    ║ findOne(…)          ║ findById(…)           ║
    ╠═════════════════════╬═══════════════════════╣
    ║ save(Iterable)      ║ saveAll(Iterable)     ║
    ╠═════════════════════╬═══════════════════════╣
    ║ findAll(Iterable)   ║ findAllById(…)        ║
    ╠═════════════════════╬═══════════════════════╣
    ║ delete(ID)          ║ deleteById(ID)        ║
    ╠═════════════════════╬═══════════════════════╣
    ║ delete(Iterable)    ║ deleteAll(Iterable)   ║
    ╠═════════════════════╬═══════════════════════╣
    ║ exists()            ║ existsById(…)         ║
    ╚═════════════════════╩═══════════════════════╝
    

    【讨论】:

    【解决方案2】:

    请注意,findById 并不是findOne 的完全替代品,它返回的是Optional 而不是null

    由于对新的 java 东西不是很熟悉,我花了一点时间才弄明白,但这将 findById 的行为变成了 findOne 的行为:

    return rep.findById(id).orElse(null);
    

    【讨论】:

    • 不是最好的主意:您的代码将继续工作,但您没有使用 API as you should。添加了Optional 以清除所有null 检查中的代码。只需更改您方法的返回类型并使用Optional 就像a good boy-scout should
    • @GabiM 如果您可以控制所有下游方法,那就太好了。即使您确实可以控制下游的所有内容并且您的项目不是其他第三方项目的依赖项,如果方法下游代码为 null(如,如果不存在则创建,或者如果缺少则执行一些逻辑),那么您有也要修复它们。
    • 在参考来自@GabiM 的链接时,我只想指出,即使该链接说“重要的是要注意 Optional 类的目的不是替换每个空引用”
    【解决方案3】:

    我们有数百次使用旧的findOne() 方法。我们最终没有着手进行大规模的重构,而是创建了以下中间接口,并让我们的存储库扩展了它,而不是直接扩展 JpaRepository

    @NoRepositoryBean
    public interface BaseJpaRepository<T, ID> extends JpaRepository<T, ID> { 
        default T findOne(ID id) { 
            return (T) findById(id).orElse(null); 
        } 
    } 
    

    【讨论】:

    • 最适合我的解决方案。无需铸造。 return findById(id).orElse(null); 足够了
    • 完全同意。为我节省了数百行更改。
    【解决方案4】:

    务实的转变

    老办法:

    Entity aThing = repository.findOne(1L);
    

    新方法:

    Optional<Entity> aThing = repository.findById(1L);
    

    【讨论】:

      【解决方案5】:

      另一种方式。添加@Query。即使我更愿意使用Optional:

      @Repository
      public interface AccountRepository extends JpaRepository<AccountEntity, Long> {
      
          @Query("SELECT a FROM AccountEntity a WHERE a.id =? 1")
          public AccountEntity findOne(Long id);
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-31
        • 2018-02-04
        • 2016-01-18
        • 1970-01-01
        • 2021-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多