Spring Data findAll 正在处理所有数据库表,而不是请求的表
SpringData findAll is processing all database tables and not the one requested
我是 SpringData 的新手,我不明白这里发生了什么。我创建了一个扩展 PagingAndSortingRepository 的接口并重写了 findAll() 方法,如下所示:
@Override
@Query
List<MyEntity> findAll();
我正在我的服务中调用此方法,但它使我的应用程序抛出异常 Caused by: java.lang.WhosebugError
因为该方法正在读取整个数据库,而不仅仅是从 MyEntity
table 在数据库中。有什么想法吗?
显然问题出在 EclipseLink 的配置上。在 persistence.xml 中,我添加了这一行 <shared-cache-mode>NONE</shared-cache-mode>
,现在它可以正常工作了。
无需覆盖 getall() method.in 您的服务自动装配了该 dao 或存储库 class 并使用它可以直接调用 findall() 方法。
如果您想编写自定义方法而不是 spring data jpa 给出的方法,那么我们使用 @Query 来编写自定义查询。
No Need to Override. What i have done is i have created a repository i.e
FavoriteRepository which is extending JpaRepository and i have mentioned the
dbmodel name (Favorite)
like this JpaRepository<Favorite, Long> // Here Favorite is my model name
and Long is the type of primary key mentioned in db model Favorite as @Id
@Repository
public interface FavoriteRepository extends JpaRepository<Favorite, Long>{
}
Now you can use method findOne or findAll. As these methods are present in
Jparepository.Hope so it will help
If you want to add new method then use @Query with JpQL
@Query(value = "select f from Favorite f where f.userId=:userId ")
public List<Favorite> getFavoritesForUser(@Param("userId") String userId);
我是 SpringData 的新手,我不明白这里发生了什么。我创建了一个扩展 PagingAndSortingRepository 的接口并重写了 findAll() 方法,如下所示:
@Override
@Query
List<MyEntity> findAll();
我正在我的服务中调用此方法,但它使我的应用程序抛出异常 Caused by: java.lang.WhosebugError
因为该方法正在读取整个数据库,而不仅仅是从 MyEntity
table 在数据库中。有什么想法吗?
显然问题出在 EclipseLink 的配置上。在 persistence.xml 中,我添加了这一行 <shared-cache-mode>NONE</shared-cache-mode>
,现在它可以正常工作了。
无需覆盖 getall() method.in 您的服务自动装配了该 dao 或存储库 class 并使用它可以直接调用 findall() 方法。 如果您想编写自定义方法而不是 spring data jpa 给出的方法,那么我们使用 @Query 来编写自定义查询。
No Need to Override. What i have done is i have created a repository i.e
FavoriteRepository which is extending JpaRepository and i have mentioned the
dbmodel name (Favorite)
like this JpaRepository<Favorite, Long> // Here Favorite is my model name
and Long is the type of primary key mentioned in db model Favorite as @Id
@Repository
public interface FavoriteRepository extends JpaRepository<Favorite, Long>{
}
Now you can use method findOne or findAll. As these methods are present in
Jparepository.Hope so it will help
If you want to add new method then use @Query with JpQL
@Query(value = "select f from Favorite f where f.userId=:userId ")
public List<Favorite> getFavoritesForUser(@Param("userId") String userId);