简体   繁体   English

Spring JpaRepository 不适用于事务性

[英]Spring JpaRepository not working with transactional

I have the following interface:我有以下界面:

public interface SnapshotRepository extends JpaRepository<Snapshot, Integer>, ISnapshotRepositoryExtra { }

that as you can see extends:如您所见,扩展:

public interface ISnapshotRepositoryExtra {
    Optional<Snapshot> latest();
}

... I've implemented that latest() method in the following classes: ...我已经在以下类中实现了该latest()方法:

@Transactional
public class SnapshotRepositoryExtra implements ISnapshotRepositoryExtra {

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Optional<Snapshot> latest() {
        return (Optional<Snapshot>) entityManager
                .createQuery("SELECT r FROM snapshot r order by timestamp DESC LIMIT 1")
                .getResultStream().findFirst();
    }
}

However when I launch the project, I see:但是,当我启动该项目时,我看到:

java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.Optional com...persistance.snapshot.ISnapshotRepositoryExtra.latest()! No property latest found for type Snapshot!

So my question is, why Spring does not see the class SnapshotRepositoryExtra that I've just made?所以我的问题是,为什么 Spring 没有看到我刚刚制作的 class SnapshotRepositoryExtra

Spring Data is picky about how you name things. Spring 数据对您如何命名事物很挑剔。 According to the documentation :根据文档

The most important part of the class name that corresponds to the fragment interface is the Impl postfix. class 名称中与片段接口对应的最重要部分是 Impl 后缀。

In other words, you need to call your interface something like SnapshotRepositoryExtra and the implementation SnapshotRepositoryExtraImpl for Spring Data to recognise it (most examples I've seen would use something like CustomSnapshotRepository for the interface, but I don't think that convention is required).换句话说,您需要调用类似SnapshotRepositoryExtra之类的接口和 Spring Data 的实现SnapshotRepositoryExtraImpl来识别它(我见过的大多数示例都会使用CustomSnapshotRepository之类的接口作为接口,但我认为不需要约定) .

That's for the latest version;这是最新版本; previous versions of Spring Data had different naming requirements .以前版本的 Spring 数据有不同的命名要求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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