Java 肯定的。谢谢。使用findAll()意味着您将把该类型的所有文档提取到内存中,然后对它们进行操作。通常最好选择要在查询中提取的文档。例如,如果您的表有50000个文档,其中10000个是索引类型,并且您对其中的一个感兴趣,那么最好只获取该文档,而不是100
Java 肯定的。谢谢。使用findAll()意味着您将把该类型的所有文档提取到内存中,然后对它们进行操作。通常最好选择要在查询中提取的文档。例如,如果您的表有50000个文档,其中10000个是索引类型,并且您对其中的一个感兴趣,那么最好只获取该文档,而不是100,java,hibernate,spring-boot,repository,Java,Hibernate,Spring Boot,Repository,肯定的。谢谢。使用findAll()意味着您将把该类型的所有文档提取到内存中,然后对它们进行操作。通常最好选择要在查询中提取的文档。例如,如果您的表有50000个文档,其中10000个是索引类型,并且您对其中的一个感兴趣,那么最好只获取该文档,而不是10000或50000。要使用Spring执行此操作,您需要使用findBy。。。方法。如果您提供了表和实体定义,我可以向您展示如何执行此操作。因此,如果我没有弄错,您可以在同一个表中拥有不同的配置文档类型,对吗?@NiVeR Yes,并且对于每个文
肯定的。谢谢。使用findAll()意味着您将把该类型的所有文档提取到内存中,然后对它们进行操作。通常最好选择要在查询中提取的文档。例如,如果您的表有50000个文档,其中10000个是索引类型,并且您对其中的一个感兴趣,那么最好只获取该文档,而不是10000或50000。要使用Spring执行此操作,您需要使用findBy。。。方法。如果您提供了表和实体定义,我可以向您展示如何执行此操作。因此,如果我没有弄错,您可以在同一个表中拥有不同的配置文档类型,对吗?@NiVeR Yes,并且对于每个文档,我都有不同的存储库。谢谢。使用findAll()意味着您将把该类型的所有文档提取到内存中,然后对它们进行操作。通常最好选择要在查询中提取的文档。例如,如果您的表有50000个文档,其中10000个是索引类型,并且您对其中的一个感兴趣,那么最好只获取该文档,而不是10000或50000。要使用Spring执行此操作,您需要使用findBy。。。方法。如果您提供了表和实体定义,我可以向您展示如何做到这一点。
public interface IndexRepository extends CrudRepository<Index, Long> {
}
StreamSupport.stream(this.configurationRepository.findAll().spliterator(), false).
map(
return Index;
).//A Collector here perhaps?
Index findFirstIndex();
indexRepository.findAll().stream()
.findFirst()
.orElseThrow(
() -> new IndexNotFoundException("Index cannot be found")
);
// Can only insert/update index entity for now
interface IndexRepository extends Repository<Index, String> {
Index save(Index index);
}
@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
}
}
}
interface IndexRepository extends Repository<Index, String> {
@Query("select i from Index i where i.id = :#{T(com.stackoverflow.so44249828.Index).INDEX_ID}")
Index load();
Index save(Index index);
}
public interface IndexRepository extends CrudRepository<Index, Long> {
Index findFirstByOrderByIdAsc();
}
public interface IndexRepository extends CrudRepository<Index, Long> {
}
Optional<Index> optional = StreamSupport.stream(repository.findAll().sliterator(), false)
.filter(i -> i instanceof Index)
.findFirst();