Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JPA问题@OneToMany-如何从数据库中获取特定对象?_Java_Spring_Jpa - Fatal编程技术网

Java JPA问题@OneToMany-如何从数据库中获取特定对象?

Java JPA问题@OneToMany-如何从数据库中获取特定对象?,java,spring,jpa,Java,Spring,Jpa,我有用户和系列实体。用户具有系列的列表 public class User { @Id @GeneratedValue private int id; private String userName; private String password; @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true) @JoinColumn(name = "user_id&q

我有
用户
系列
实体。用户具有系列的列表

public class User {

    @Id
    @GeneratedValue
    private int id;

    private String userName;
    private String password;
    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "user_id")
    private List<Series> series = new ArrayList<>();
}
我想得到分配给特定用户的序列。 在系列服务中,我执行以下操作以获取系列:

@Override
public List<Series> getAllSeries() {
    return seriesRepository.findAll();
}
@覆盖
公共列表getAllSeries(){
返回序列repository.findAll();
}
但我从数据库中得到了所有序列。有人能告诉我我做错了什么吗?我应该在请求中传递用户吗?

您正在获取序列,因为您使用findAll()查询序列存储库

如果要获取属于特定用户的序列,只需将userId传递给相应的方法,如:

@Override
public List<Series> getSeriesByUserId(int userId) {
   return seriesRepository.findByUserId(userId);
}
@覆盖
公共列表getSeriesByUserId(int userId){
返回seriesRepository.findByUserId(userId);
}

系列
实体中,用户外键的列名是什么?您能否提供一个
系列
实体以及您的存储库界面?我编辑了我的帖子:DfindAll()将返回数据库中所有可用的系列。您需要传递一些输入以获得所需的结果集。利用JPA方法,如
findByUser(字符串用户)
@GiorgiTsiklauriJpaRepository@GiorgiTsiklauri是的,我想现在可以了
@Override
public List<Series> getSeriesByUserId(int userId) {
   return seriesRepository.findByUserId(userId);
}