Я пытаюсь найти все для сущности, имеющей 2 отношения OneToMany.Когда я пытаюсь получить GET на API, сервер печатает ошибку 500, без каких-либо указаний.Я думаю, что проблема исходит от формата JSON, потому что, когда я пытаюсь в XML, это работает.Эти 2 случая, похоже, имеют одну и ту же проблему, но решения не работают для меня:
Glassfish JAX-RS Отображение JSON Простой пример Внутренняя ошибка сервера 500 Нет созданных журналов
JAXB на JAX-RS - JPA Объект с отношениями не возвращает JSON HTTP 500, нет ошибок в журналах (Glassfish)
Объект:
/**
* Disruptive
*/
@Entity
@Table(name = "disruptive")
@XmlRootElement
public class Disruptive implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "disruptive_id_seq")
@SequenceGenerator(name = "disruptive_id_seq", sequenceName = "disruptive_id_seq", allocationSize = 1)
@Column(name = "id_disruptive")
protected Integer idDisruptive;
private String name;
private String content;
@ManyToOne
@JoinColumn(name = "id_initial_situation", referencedColumnName = "id_initial_situation")
protected InitialSituation initialSituation;
@OneToMany(mappedBy = "disruptive") //,cascade = CascadeType.ALL,orphanRemoval = true
protected List<Event> events;
@OneToMany(mappedBy = "disruptive")
protected List<FinalSituation> finalSituations;
public Disruptive() {
}
public Disruptive(InitialSituation initialSituation) {
this.setInitialSituation(initialSituation);
}
public Integer getIdDisruptive() {
return idDisruptive;
}
public void setIdDisruptive(Integer idDisruptive) {
this.idDisruptive = idDisruptive;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public InitialSituation getInitialSituation() {
return initialSituation;
}
public void setInitialSituation(InitialSituation initialSituation) {
this.initialSituation = initialSituation;
}
public List<FinalSituation> getFinalSituations() {
return finalSituations;
}
public void setFinalSituations(List<FinalSituation> finalSituations) {
this.finalSituations = finalSituations;
}
public List<Event> getEvents() {
return events;
}
public void setEvents(List<Event> events) {
this.events = events;
}
@Override
public String toString(){
return this.getName();
}
@Override
public int hashCode() {
int hash = 7;
hash = 53 * hash + Objects.hashCode(this.idDisruptive);
hash = 53 * hash + Objects.hashCode(this.name);
hash = 53 * hash + Objects.hashCode(this.content);
hash = 53 * hash + Objects.hashCode(this.initialSituation);
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Disruptive other = (Disruptive) obj;
if (!Objects.equals(this.name, other.name)) {
return false;
}
if (!Objects.equals(this.content, other.content)) {
return false;
}
if (!Objects.equals(this.idDisruptive, other.idDisruptive)) {
return false;
}
if (!Objects.equals(this.initialSituation, other.initialSituation)) {
return false;
}
return true;
}
}
ОбъектWebService:
/**
* DisruptiveFacadeREST
*/
@Stateless
@Path("disruptive")
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
public class DisruptiveFacadeREST extends AbstractFacade<Disruptive> {
@PersistenceContext(unitName = "StoryGeneratorPU")
private EntityManager em;
public DisruptiveFacadeREST() {
super(Disruptive.class);
}
@POST
@Override
public void create(Disruptive entity) {
super.create(entity);
}
@PUT
@Path("{id}")
public void edit(@PathParam("id") Integer id, Disruptive entity) {
super.edit(entity);
}
@DELETE
@Path("{id}")
public void remove(@PathParam("id") Integer id) {
super.remove(super.find(id));
}
@GET
@Path("{id}")
public Disruptive find(@PathParam("id") Integer id) {
return super.find(id);
}
@GET
@Override
public List<Disruptive> findAll() {
return super.findAll();
}
@GET
@Path("random")
public Disruptive findRandom(@QueryParam("idInitialSituation") Integer idInitialSituation) {
System.out.println(idInitialSituation);
Query query;
query = this.getEntityManager().createQuery("SELECT dis FROM Disruptive dis JOIN dis.initialSituation insi WHERE insi.idInitialSituation = :idInitialSituation");
query.setParameter("idInitialSituation", idInitialSituation);
List<Disruptive> list= query.getResultList();
Disruptive dis = null;
if (!list.isEmpty()){
Collections.shuffle(list);
dis = (Disruptive) list.get(0);
}
return dis;
}
@Override
protected EntityManager getEntityManager() {
return em;
}
}
Я использую сервер Glassfish и JAX-RS для сервисов.