JPA实体到JSON

时间:2017-02-07 12:45:51

标签: hibernate rest jpa moxy

将JPA实体自动转换为JSON会导致Glassfish中出现HTTP 500内部服务器错误。

当内部映射实体为空时,转换工作正常,但当它们被链接时,我得到错误。

这些是实体,服务和REST资源。

我查看过有关同一主题的其他帖子,但无法正确理解。我是一名新手程序员。

Flight.java

@Entity
@XmlRootElement
public class Flight implements Serializable {

    @Transient
    private static final long serialVersionUID = 1L;

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

    @Enumerated(EnumType.STRING)
    private FlightDestinations flightOrigin;

    @Enumerated(EnumType.STRING)
    private FlightDestinations flightDestination;

    private Integer price;

    @Temporal(TemporalType.TIMESTAMP)
    private Date flightTime;

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

    @OneToMany(mappedBy = "flight")
    @XmlTransient
    private List<Pilot> pilots;


    @ManyToMany
    @JoinTable(name = "f_p_join", joinColumns = @JoinColumn(name = "flight_fk"), 
    inverseJoinColumns = @JoinColumn(name = "passenger_fk"))
    private List<Passenger> passengers;

    // getters and setters

}

Passenger.java

@Entity
@XmlRootElement
public class Passenger implements Serializable {

    @Transient
    private static final long serialVersionUID = 1L;

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

    private String firstName;
    private String lastName;

    @Temporal(TemporalType.DATE)
    private Date dob;

    @Enumerated(EnumType.STRING)
    private Gender gender;

    @Enumerated(EnumType.STRING)
    private FlightClass flightClass;

    @ManyToMany(mappedBy = "passengers")
    private List<Flight> flights;

    // getters and setters

}

Pilot.java

@Entity
public class Pilot implements Serializable {

    @Transient
    private static final long serialVersionUID = 1L;

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

    private String firstName;

    private String lastName;

    private Integer pilotLicense;

    @Enumerated(EnumType.STRING)
    private PilotRank pRank;

    @ManyToOne
    @JoinColumn(name = "flight_fk")
    private Flight flight;

    //getters and setters

}

Airplane.java

@Entity
public class Airplane implements Serializable {

    @Transient
    private static final long serialVersionUID = 1L;

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

    private String planeMake;

    private String modelName;

    private Integer seatinCapacity;

    @OneToOne(mappedBy ="airplaneDetails")
    private Flight flightDetails;

    //getters and setters

}

从服务类中的数据库获取所有航班的方法

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

public List<Flight> getFlights() {

        TypedQuery<Flight> query = em.createQuery("SELECT f FROM Flight f", Flight.class);
        List<Flight> results = query.getResultList();


        return results;
        }

REST方法

@EJB
FlightService fs;

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

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

}

0 个答案:

没有答案