简体   繁体   English

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

[英]Including all @OneToMany Entities in Restful-WebService response?

First Post here,so please excuse any noobish approaches to best get your attention / help me you find an answer. 首先在这里发布,所以请原谅任何愚蠢的做法,以最好地引起您的注意/帮助我找到答案。 :) :)

Without going into too much detail of my actual Program: 无需过多介绍我的实际程序:

I have a User who can register many Devices which can be associated to many Remotes which can have many Buttons ( Commands (LIRC Commands to be precise)). 我有一个可以注册许多设备用户 ,这些设备可以与许多可以具有许多按钮的 远程控制器相关联(命令(准确地说是LIRC命令))。

Here is my Code for the 4 Entities' @ManyToOne and @OneToMany Sections. 这是我对4个实体的@ManyToOne@OneToMany部分的代码。

USERS USERS

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

DEVICES 设备

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

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

REMOTES 遥控器

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

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

COMMANDS COMMANDS

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

They all work fine and the Database entries all have the appropriate foreign keys, and if I've understood correctly this should be bi-directional. 它们都可以正常工作,并且数据库条目都具有适当的外键,如果我正确理解的话,这应该是双向的。

To My Question: 对我的问题:

Is there a way my Restful Webservice Request can respond to a findAll() on the User by not only responding with the User but also with all the data from Devices in turn returning all the Devices' Remote's and the Commands which belong to the Remote . 有没有办法我RESTful Web服务请求能到的findAll()上的用户,不仅与用户响应,但也与所有从设备依次将数据返回响应所有设备远程的和属于远程命令

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

If need be I'll have to go through all the entities and patch together a JSON myself, but before I embark on that journey I was wondering if there's an easier way of going about my problem. 如果需要的话,我将不得不遍历所有实体并亲自将JSON修补在一起,但是在我踏上这一旅程之前,我想知道是否有更简单的方法来解决我的问题。

Cheers 干杯

Quick Fix: 快速解决:

I had to change the Fetch and cascade settings for all of my @OneToMany's and @ManyToOnes' in my Entities. 我必须更改实体中所有@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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM