在 Restful-WebService 响应中包含所有 @OneToMany 实体?

Including all @OneToMany Entities in Restful-WebService response?

首先 Post 在这里,所以请原谅任何笨拙的方法,以最好地引起您的注意/帮助我找到答案。 :)

无需详细介绍我的实际程序:

我有一个 用户 可以注册许多 设备 可以关联到许多 遥控器 可以有很多 按钮 (命令(准确地说是 LIRC 命令))。

这是我的 4 个实体的代码 @ManyToOne@OneToMany 部分。

用户

@OneToMany(mappedBy = "user")
private List<Devices> devices;

设备

@ManyToOne
@JoinColumn(name = "USER_ID")
private Users user;

@OneToMany(mappedBy = "device")
private List<Remotes> remotes;

远程

@ManyToOne
@JoinColumn(name = "DEVICE_ID")
private Devices device;

@OneToMany(mappedBy = "remote")
private List<Commands> commands;

命令

@ManyToOne
@JoinColumn(name = "REMOTE_ID")
private Remotes remote;

它们都工作正常并且数据库条目都有适当的外键,如果我理解正确的话这应该是双向的。

我的问题:

我的 Restful Web 服务请求是否可以通过不仅响应 User 还响应所有数据来响应用户的 findAll()从 Devices 依次返回所有 Devices' Remote's 和属于 [=42] 的 Commands =]远程.

@GET
@Override
@Produces({"application/json"})
public List<Users> findAll() {
    return super.findAll();
}

如果需要,我将不得不检查所有实体并自己拼凑一个 JSON,但在我开始这段旅程之前,我想知道是否有更简单的方法来解决我的问题。

干杯

快速修复:

我必须更改实体中所有@OneToMany 和@ManyToOnes 的获取和级联设置。

@XmlTransient
@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "USER_ID")
private Users user;

@OneToMany(
        mappedBy = "device",
        fetch = FetchType.EAGER,
        cascade = {CascadeType.REMOVE, CascadeType.MERGE,CascadeType.PERSIST})
private List<Remotes> remotes;