Spring Data Jpa通用投影findAll

时间:2018-01-25 11:02:41

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

根据https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projection.dynamic我需要实现通用方法findAll,有没有办法在没有@Query注释的情况下做到这一点? 我试过这种方式:

<T> List<T> findAll(Class<T> type);

但我得到了:

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property findAll found for type

2 个答案:

答案 0 :(得分:2)

问题解决了

<T> Set<T> findBy(Class<T> type);

工作了......:)

答案 1 :(得分:0)

首先,findAll()方法默认情况下已在每个jpa存储库中提供,因此无需为此创建通用方法。

您应该为涉及某些属性的此类查询创建泛型方法。

interface PersonRepository extends Repository<Person, UUID> {

  Collection<T> findByLastname(String lastname, Class<T> type);
} 

在这里,他们还提到了一个属性(姓氏)。

另外一个例子可以帮助您: -

@MappedSuperclass
public abstract class AbstractMappedType {
  …
  String attribute
}

@Entity
public class ConcreteType extends AbstractMappedType { … }

@NoRepositoryBean
public interface MappedTypeRepository<T extends AbstractMappedType>
  extends Repository<T, Long> {

  @Query("select t from #{#entityName} t where t.attribute = ?1")
  List<T> findAllByAttribute(String attribute);
}

public interface ConcreteRepository
  extends MappedTypeRepository<ConcreteType> { … }

有关详细信息,请参阅此JPA Repositories文档,这非常有用。