0

Here is my entity class

    @Entity
@Table( name = "NEO_TEAM",  schema = "METRICS" )
public class ETeam implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="ID")
    private int id;

    @Column(name="NAME")
    private String name;

    @Column(name="DESCRIPTION")
    private String description;

    //bi-directional many-to-one association to ETeamQueue
    @OneToMany(mappedBy="eteam"  , fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)

    private List<ETeamQueue> teamQueue;

    public ETeam(int id,String name,String description){
        this.id = id;
        this.name = name;
        this.description = description;
    }
    public ETeam(String name,String description){
        this.name = name;
        this.description = description;
    }
    public ETeam() {
    }

    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public List<ETeamQueue> getTeamQueue() {
        return this.teamQueue;
    }

    public void setTeamQueue(List<ETeamQueue> teamQueue) {
        this.teamQueue = teamQueue;
    }

    public ETeamQueue addTeamQueue(ETeamQueue teamQueue) {
        getTeamQueue().add(teamQueue);
        teamQueue.setEteam(this);

        return teamQueue;
    }

    public ETeamQueue removeTeamQueue(ETeamQueue teamQueue) {
        getTeamQueue().remove(teamQueue);
        teamQueue.setEteam(null);

        return teamQueue;
    }


}

And My REST call and JPQL query is

@Path("team")
@Produces(MediaType.APPLICATION_JSON)
public class TeamResource {
    TeamService ts = new TeamService();
    @GET
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public List<ETeam> listTeam(@PathParam("id") int id){

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("NeoMetrics");
        EntityManager em = emf.createEntityManager();

        Query query = em.createQuery("select e from ETeam e   where e.id = :id ",ETeam.class);
        query.setParameter("id", id);
        List<ETeam> lis = (List<ETeam>) query.getResultList();

        //List<ETeam> lis = ts.getTeam(id);

        return lis;
    }

i want to fetch only one record from the team table but it give me result like shown in picture , which show the result is looped thousand times , its very long output result i only put part of it , any help will be really appreciated

Rest call out put

Afzaal
  • 42
  • 4
  • You have a bidirectional association. So serializing a team serializes its queues, and serializing a queu serialises its team, which serializes its queues, which serializes its team, etc. Use your JSON mapper capabilities to prevent this recusrive loop (use `@JsonIgnore` if you use Jackson, for example). Or serialize DTOs rather than entities. – JB Nizet Dec 04 '16 at 18:58
  • So @JBNizet you mean when ever there is bi directional association , i have to use mapper . – Afzaal Dec 04 '16 at 19:03
  • JAX-RS uses a mapper to transform the List to JSON. You need to decide what you want to return, and configure your classes appropriately. – JB Nizet Dec 04 '16 at 19:05
  • Thanks alot @JBNizet – Afzaal Dec 04 '16 at 19:11

0 Answers0