gpt4 book ai didi

java - Spring : How to retrieve set of entities using JpaRepository method without using the primary key?

转载 作者:行者123 更新时间:2023-11-29 02:13:09 25 4
gpt4 key购买 nike

我想从我的数据库中检索给定 ID 的实体列表。但是这个id不是我表的主键。

public List<Notification> view(ApplicationUser applicationUser){
List<Notification> notification = repository.findAll();

return notification;
}

在我的代码中,我有一个 applicationUser ID,它不是通知表的主键。它只是一个外键。在上述情况下,我可以检索整个表。我也尝试了 repository.getOne() 方法。但我的要求不能完全满足。

我想将所有实体添加到通知列表中,其中我表的 applicationUserId 字段等于用户提供的值。

这是我的通知表

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com