I have a java rest application, where I want to display some information as json. I have two entity classes in play right now Pet and event. The relationship is one too many, so one Pet has a collection of Events.
previously i have made a list of Entity pets and made a custom static methods to transform the Pet entities into DTO objects as such:
@Path("/allPets")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getPetsfromCollection() {
List<Pet> petList = new ArrayList<>();
List<DTOPet> DTOPetList = new ArrayList<>();
// here i'm looping through my list and then converting them to DTO object, and adding dem to a string called json
petList.addAll(facade.returnAllPets());
for(Pet pet: petList){
DTOPet dtopet = EntitytoDTO.convertDTOPet(pet);
DTOPetList.add(dtopet);
}
String json = gson.toJson(DTOPetList);
return Response.ok().entity(json).build();
}
Now I want to do the same, but I want to display the event collection, inside of the PetDTO aswell
Here is my PetDTO class
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package DTO;
import Entities.Event;
import java.util.Collection;
import java.util.Date;
/**
*
* @author kristoffer
*/
public class DTOPet {
private Integer id;
private String name;
private Date birth;
private String species;
private Date death;
private Collection<DTOEvent> eventCollection;
public DTOPet(Integer id, String name, Date birth, String species, Date death, Collection<DTOEvent> eventCollection) {
this.id = id;
this.name = name;
this.birth = birth;
this.species = species;
this.death = death;
this.eventCollection = eventCollection;
}
public DTOPet() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public String getSpecies() {
return species;
}
public void setSpecies(String species) {
this.species = species;
}
public Date getDeath() {
return death;
}
public void setDeath(Date death) {
this.death = death;
}
public Collection<DTOEvent> getEventCollection() {
return eventCollection;
}
public void setEventCollection(Collection<DTOEvent> eventCollection) {
this.eventCollection = eventCollection;
}
}
How can I do this?
I have tried several methods but nothing has succeeded yet.
Here I what I have got so far:
@Path("/allPetsE")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getEventofPets() {
//med denne metode skal vi bruge et DTO(data transfer object til at formatere til Json)
List<Pet> petList = new ArrayList<>();
List<DTOPet> DTOPetList = new ArrayList<>();
List<Event> eventList = new ArrayList<>();
List<DTOEvent> DTOevents = new ArrayList<>();
petList.addAll(facade.returnAllPets());
eventList.addAll(facade.returnAllEvents());
//dette er ikke en pæn løsning, men lad os prøve at få hver event lavet om til et DTO og tilføje det til de forskellige dyr
for(Pet pet: petList){
DTOPet dtopet = EntitytoDTO.convertDTOPet(pet);
dtopet.setEventCollection(DTOevents);
DTOPetList.add(dtopet);
//dette bliver ikke performance optimere i føste omgang, vi skal bare få det til at virke
}
String json = gson.toJson(DTOPetList);
return Response.ok().entity(json).build();
}
the issue with my code is that I have no way of converting an entire collection of Event Entity objects into DTO objects, I could try looping through inside the method, but that would have a horrible impact on the performance, alternatively i could try making a JPQL query, but that would still not solve converting to DTO objects.
Another way this could be solved was to change the returntype my current JPQL query like this:
public Collection<Event> returnAllEvents (){
EntityManager em = emf.createEntityManager();
//vi laver en typed query for at specificere hvilken datatype, det er vi leder efter, i dette tilfælde er det en Pet
TypedQuery<Event> query = em.createNamedQuery("Event.findAll", Event.class);
return query.getResultList();
}
so it would return a Collection of collections instead, but I'm not sure how to do this.