使用 CrudRepository 方法 findById(Long Id) 时是否可以更改列名?
Is it possible to change the column name while using CrudRepository method findById(Long Id)?
我正在使用 JPA,Spring 引导示例应用程序并扩展 CrudRepository。我进行 REST 调用并传递一个 Id 以搜索具有该 Id 的所有行。
我在内部调用 repository.findById(Long Id) 方法进行搜索。但是,此方法始终在 "Primary key" 列上进行搜索。是否可以使用此方法或任何其他方法在同一 table 中的 "Foreign Key" 列上进行搜索?
我的 table 有以下几列
Name DataType
--------------------------------------
Id BIGIN(8) PK, AI
comment VARCHAR(100) Not Null
userId BIGINT(8) FK Not Null
我想按 userId 搜索,但 findById 将 Id 作为默认值
您必须在存储库中添加新方法。
如果您在实体 class 中没有任何关系,则使用本机查询(假设 Comment
作为您的实体)
@Query(value = "SELECT * FROM Comment WHERE userId = ?1", nativeQuery = true)
List<Comment> findByUserId(long userId);
如果您在实体 class 中有关系,则使用此。
List<Comment> findByUserId(long userId);
我正在使用 JPA,Spring 引导示例应用程序并扩展 CrudRepository。我进行 REST 调用并传递一个 Id 以搜索具有该 Id 的所有行。
我在内部调用 repository.findById(Long Id) 方法进行搜索。但是,此方法始终在 "Primary key" 列上进行搜索。是否可以使用此方法或任何其他方法在同一 table 中的 "Foreign Key" 列上进行搜索?
我的 table 有以下几列
Name DataType
--------------------------------------
Id BIGIN(8) PK, AI
comment VARCHAR(100) Not Null
userId BIGINT(8) FK Not Null
我想按 userId 搜索,但 findById 将 Id 作为默认值
您必须在存储库中添加新方法。
如果您在实体 class 中没有任何关系,则使用本机查询(假设 Comment
作为您的实体)
@Query(value = "SELECT * FROM Comment WHERE userId = ?1", nativeQuery = true)
List<Comment> findByUserId(long userId);
如果您在实体 class 中有关系,则使用此。
List<Comment> findByUserId(long userId);