JSON output of JavaEE Entity with TomEE server

心不动则不痛 提交于 2019-12-12 03:28:40

问题


I try to make output of my Entity Flight, which have OneToOne relationship with my other Entity Airplane. Before I add airplaneDetail field in my Flight Entity the result was perfect, but I after that I get empty result and Eclipse start throwing exceptions in gson.

My Flight class :

@Entity
@NamedQueries({ @NamedQuery(name = "Flight.getAll", query = "SELECT f FROM Flight f"), })
@NamedQuery(name = "Flight.getByDestination", query = "SELECT f FROM Flight f WHERE f.destinationFrom=:destinationFrom AND f.destinationTo=:destinationTo")
public class Flight implements Serializable {

@Transient
private static final long serialVersionUID = 1L;

public Flight() {
    super();
}

public Flight(FlightDestination destinationFrom, FlightDestination destinationTo, Integer flightPrice, Date date,
        Airplane airplaneDetail) {
    super();
    this.destinationFrom = destinationFrom;
    this.destinationTo = destinationTo;
    this.flightPrice = flightPrice;
    this.date = date;
    this.airplaneDetail = airplaneDetail;
}

public Flight(FlightDestination destinationFrom, FlightDestination destinationTo, Integer flightPrice, Date date) {
    super();
    this.destinationFrom = destinationFrom;
    this.destinationTo = destinationTo;
    this.flightPrice = flightPrice;
    this.date = date;
}

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

@Enumerated(EnumType.STRING)
private FlightDestination destinationFrom;

@Enumerated(EnumType.STRING)
private FlightDestination destinationTo;

private Integer flightPrice;

@Temporal(TemporalType.DATE)
private Date date;

@OneToOne(cascade = { CascadeType.PERSIST, CascadeType.REMOVE })
@JoinColumn(name = "airplane_fk")
private Airplane airplaneDetail;

@Override
public String toString() {
    return "Flight [id=" + id + ", destinationFrom=" + destinationFrom + ", destinationTo=" + destinationTo
            + ", flightPrice=" + flightPrice + ", date=" + date + ", airplaneDetail=" + airplaneDetail + "]";
}}

Airplane Entity:

@Entity

public class Airplane implements Serializable {

private static final long serialVersionUID = 1L;

public Airplane() {
    super();
}

public Airplane(String planeModel, Integer seatingCapacity) {
    super();
    this.planeModel = planeModel;
    this.seatingCapacity = seatingCapacity;
}

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

private String planeModel;

private Integer seatingCapacity;

@OneToOne(mappedBy = "airplaneDetail")
private Flight flight;

@Override
public String toString() {
    return "Airplane [id=" + id + ", planeModel=" + planeModel + ", seatingCapacity=" + seatingCapacity
            + ", flight=" + flight + "]";
}}

My WebService :

@Path("/flights")
public class FlightsWebService {

@PersistenceContext(unitName = "airline")
private EntityManager em;

@EJB
private FlightService fs;

@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Flight> getFlights() {

    List<Flight> fList = fs.getAllFlights();
    return fList;

}}

But the result I get is this, when I add OneToOne relationship :

[{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":

来源:https://stackoverflow.com/questions/35976066/json-output-of-javaee-entity-with-tomee-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!