我想从数据库中检索给定 ID 的实体列表。但这个id不是我表的主键。
public List<Notification> view(ApplicationUser applicationUser){
List<Notification> notification = repository.findAll();
return notification;
}
在我的代码中,我有一个 applicationUser ID,它不是通知表的主键。它只是一个外键。在上面的情况下,我可以检索整个表。我也尝试了repository.getOne()方法。但我的要求不能完全满足。
我想将所有实体添加到通知列表中,其中表的 appliclationUserId 字段等于用户提供的值。
这是我的通知表
CREATE TABLE `notification` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`),
`message` varchar(100),
`dateAndTime` time DEFAULT NULL,
`read` tinyint(1) NOT NULL DEFAULT '0',
`notificationType` int(11) DEFAULT NULL
`userId` bigint(20),
FOREIGN KEY (`userId`) REFERENCES `application_user` (`id`)
);
非常感谢任何帮助。z
最佳答案
存储库类:
@Query("select nt from notification nt where nt.userId = ?1")
List<Notification> findByUserId(BigInteger userId);
调用:
repository.findByUserId(BigInteger.valueOf(applicationUser.getId());
关于java - Spring : How to retrieve set of entities using JpaRepository method without using the primary key?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46313944/