使用SpringBoot CrudRepository查找一个已知条目

时间:2017-05-29 20:18:23

标签: java hibernate spring-boot repository

我目前正在使用一个使用SpringBoot的项目。

我使用Hibernate创建了一个名为configuration_documents的表。该表可以存储我创建的不同类型的文档对象。

我创建的一个文档对象是Index,我想检索索引数据,所以我创建了一个这样的存储库:

public interface IndexRepository extends CrudRepository<Index, Long> {
}

我知道一个事实,即configuration_document表应该只有一个Index条目,否则只返回表中的第一个索引。实现这个的最佳方法是什么?

在不修改IndexRepository的情况下,我目前正在考虑这样的事情(IndexRepository是自动装配的):

StreamSupport.stream(this.configurationRepository.findAll().spliterator(), false).
                map(
                    return Index;
                ).//A Collector here perhaps?

感谢。

5 个答案:

答案 0 :(得分:0)

因此,据我所知,您无法使用jpa基于行号进行查询。在jpa中,您必须指定By参数作为where子句。有关您可以撰写的查询方法,请参阅official documentation

答案 1 :(得分:0)

您可以在构建查询时使用TopFirst个关键字,is the documentation

你必须声明这样的方法:

Index findFirstIndex();
<{1>}中的

,这将解决您的问题。

或者,如果您不允许修改IndexRepository,您可以使用流API以某种方式获取第一个结果

IndexRepository

但第一种选择会更加优化。

答案 2 :(得分:0)

首先,我建议您考虑将来要对Index做什么。 如果它只是readonly immutable实体,那么可能最好的选择是在应用程序启动之后(例如使用原始EntityManager)将一些'IndexHolder'组件注入实体并使用此组件在整个应用程序中访问Index

但我假设您希望能够更新Index。 因此,在这种情况下,我可以建议扩展org.springframework.data.repository.Repository而不是CrudRepository。它会将Index实体上可能的操作集限制为NONE,但我们仍然可以使用spring-data功能。

因此存储库可能如下所示:

// Can only insert/update index entity for now
interface IndexRepository extends Repository<Index, String> {

    Index save(Index index);
}

显然这还不够,我们至少需要一个检索操作。 在这里,我建议依靠fixed的{​​{1}} ID。 它可能是序列范围之外的某个值或'0'或guid(取决于您正在使用的内容)。

然后我们需要在使用此实体的所有持久性操作上强制使用此ID,但结果查询将快速而简单。

我想你正在为你的类建模:

Index

让我们添加固定ID:

@Entity
@Table(name = "configuration_documents")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type")
@DiscriminatorValue("DOCUMENT")
public class ConfigurationDocument {
    @Id
    private String id;

    public void setId(String id) {
        this.id = id;
    }
}

@Entity(name = "Index")
@DiscriminatorColumn(name = "type")
@DiscriminatorValue("INDEX")
public class Index extends ConfigurationDocument {
    ...
}

最后一步是向存储库添加检索操作:

@Entity(name = "Index")
@DiscriminatorColumn(name = "type")
@DiscriminatorValue("INDEX")
@EntityListeners(Index.IndexPersistentId.class)
public class Index extends ConfigurationDocument {
    //This is going to be our fixed id.
    public static final String INDEX_ID = "42";

    public static class IndexPersistentId {
        @PrePersist
        public void prePersist(Index index) {
            index.setId(INDEX_ID); //enforce using fixed ID
        }
    }
}

答案 3 :(得分:0)

在春季jpa中,您可以限制结果,如下所示:

electron

参考

http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.limit-query-result

答案 4 :(得分:0)

我记得看到类似于你正在寻找的东西。但是找不到那个来源了。

您仍然可以保留与现在相同的存储库:

public interface IndexRepository extends CrudRepository<Index, Long> {
}

对于流媒体部分,您可以执行Index类的过滤器实例,并在同一行代码中同时获取它的第一个实例,如下所示:

Optional<Index> optional = StreamSupport.stream(repository.findAll().sliterator(), false)
                                        .filter(i -> i instanceof Index)
                                        .findFirst();

这应该是你所要求的最直接的。