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

标签 java mysql maven spring-mvc spring-data-jpa

我想从数据库中检索给定 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/

相关文章:

Java + Hadoop + NoSql(使用什么组合)

Java @Nonnull 默认情况下一切

MySQL:选择特定时间之间的数据

mysql - MySQL数据库数据迁移到SQL Server

基于多个where子句的M​​ysql记录

java - 有没有办法模拟 Portal?

java - 类型参数 T 使用 Eclipse 在 <T> T[] toArray(T[] a) 中隐藏类型 T

java - 是否存在依赖 Artifact 的 Maven "compiler-only"范围

Eclipse 在构建期间从部署路径中删除文件/文件夹

maven - "tagNameFormat"属性未应用于 Maven Release 插件