使用 SpringData 创建只读存储库

问题描述 投票:0回答:6

是否可以使用 Spring Data 创建只读存储库?

我有一些链接到视图的实体和一些子实体,我想为其提供一个存储库,其中包含一些方法,例如

findAll()
findOne()
以及一些带有
@Query
注释的方法。我想避免提供像
save(…)
delete(…)
这样的方法,因为它们没有意义并且可能会产生错误。

public interface ContactRepository extends JpaRepository<ContactModel, Integer>, JpaSpecificationExecutor<ContactModel> {
    List<ContactModel> findContactByAddress_CityModel_Id(Integer cityId);

    List<ContactModel> findContactByAddress_CityModel_Region_Id(Integer regionId);

    // ... methods using @Query

    // no need to save/flush/delete
}

谢谢!

java spring jpa spring-data spring-data-jpa
6个回答
69
投票

是的,正确的方法是添加手工制作的基础存储库。你通常使用这样的东西:

@NoRepositoryBean
public interface ReadOnlyRepository<T, ID extends Serializable> extends Repository<T, ID> {

  Optional<T> findById(ID id);

  Iterable<T> findAll();
}

您现在可以对刚刚定义的具体存储库进行扩展:

public interface PersonRepository extends ReadOnlyRepository<Person, Long> {
  
  Optional<T> findByEmailAddress(String emailAddress);
}

定义基础存储库的关键部分是,方法声明带有与CrudRepository中声明的方法“非常相同的签名”,如果在这种情况下,我们仍然可以将调用路由到支持存储库代理的实现bean中。我在 SpringSource 博客中写了一篇关于该主题的更详细的

博客文章


37
投票

import org.springframework.data.repository.NoRepositoryBean; import org.springframework.data.repository.Repository; @NoRepositoryBean public interface ReadOnlyRepository<T, ID extends Serializable> extends Repository<T, ID> { T findOne(ID id); List<T> findAll(); }



7
投票
org.springframework.data.repository.Repository

.


6
投票
Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property findOne found for type


@NoRepositoryBean public interface ReadOnlyRepository<T,ID> extends Repository<T, ID> { Optional<T> findById(ID var1); boolean existsById(ID var1); Iterable<T> findAll(); Iterable<T> findAllById(Iterable<ID> var1); long count(); }



5
投票
PagingAndSortingRepository

package com.oracle.odc.data.catalog.service.core.repository; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.data.repository.NoRepositoryBean; import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.rest.core.annotation.RestResource; /** * Extension of {@link PagingAndSortingRepository} but without modification capabilities * * @author XYZ * @see Sort * @see Pageable * @see Page */ @NoRepositoryBean public interface ReadOnlyPagingAndSortingRepository<T, ID> extends PagingAndSortingRepository<T, ID> { @Override @RestResource(exported=false) <S extends T> S save(S entity); @Override @RestResource(exported=false) <S extends T> Iterable<S> saveAll(Iterable<S> entities); @Override @RestResource(exported=false) void deleteById(ID id); @Override @RestResource(exported=false) void delete(T entity); @Override @RestResource(exported=false) void deleteAll(Iterable<? extends T> entities); @Override @RestResource(exported=false) void deleteAll(); }

如果您尝试 POST 或 DELETE,您将收到 405(方法不允许)。


1
投票

import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.repository.NoRepositoryBean; import org.springframework.lang.NonNull; import java.util.List; @NoRepositoryBean public interface ReadOnlyRepository<T, ID> extends JpaRepository<T, ID> { @Override @NonNull default <S extends T> S save(@NonNull S entity) { throw new RuntimeException("Action not allowed"); } @Override @NonNull default <S extends T> List<S> saveAll(@NonNull Iterable<S> iterable) { throw new RuntimeException("Action not allowed."); } @Override @NonNull default <S extends T> S saveAndFlush(@NonNull S s) { throw new RuntimeException("Action not allowed."); } @Override default void delete(@NonNull T entity) { throw new RuntimeException("Action not allowed."); } @Override default void deleteAll() { throw new RuntimeException("Action not allowed."); } @Override default void deleteAll(@NonNull Iterable<? extends T> entities) { throw new RuntimeException("Action not allowed."); } @Override default void deleteAllInBatch() { throw new RuntimeException("Action not allowed."); } @Override default void deleteById(@NonNull ID id) { throw new RuntimeException("Action not allowed."); } @Override default void deleteInBatch(@NonNull Iterable<T> iterable) { throw new RuntimeException("Action not allowed."); } }

希望对大家有帮助(ノ^∇^)

© www.soinside.com 2019 - 2024. All rights reserved.