0

so I have done two entities with one to many relationship, I have one category whohas many visitors, and this is my code:

this is the Category entity :

@Entity
public class Category implements Serializable {

private Integer id;
private String name;
private List<Visitor> visitors = new ArrayList<Visitor>();

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
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;
}

@OneToMany(cascade=CascadeType.REMOVE, fetch = FetchType.LAZY, mappedBy = "category", orphanRemoval = true)
public List<Visitor> getVisitors() {
    return visitors;
}

public void setVisitors(List<Visitor> visitors) {
    this.visitors = visitors;
}

}

and here is the Visitor Entity :

@Entity
public class Visitor extends User {

private String passport;
private String citizenship;
private String gender;
private Company company;
private Category category;  

public String getPassport() {
    return passport;
}

public void setPassport(String passport) {
    this.passport = passport;
}

public String getCitizenship() {
    return citizenship;
}

public void setCitizenship(String citizenship) {
    this.citizenship = citizenship;
}

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
public Category getCategory() {
    return category;
}

public void setCategory(Category category) {
    this.category = category;
}

public String getGender() {
    return gender;
}

public void setGender(String gender) {
    this.gender = gender;
}

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
public Company getCompany() {
    return company;
}

public void setCompany(Company company) {
    this.company = company;
}

and here is the service method who list all the visitors and works fine :

public List<Visitor> findAllVisitors() {
    return em.createQuery(
            "SELECT v from Visitor v left join fetch v.category",
            Visitor.class).getResultList();

}

with this method I can list all the visitors each with his category object associated,

now the problem is in the other side of the relationship , here is the method who list the categories each with their visitors list :

public List<Category> findAllCategories() {

    return em.createQuery("select c from Category c",
            Category.class).getResultList();
}

I want to get the list of all the categories but when I call this method in a REST call , I get this result :

enter image description here

I want just to get a simple list of categories (id and name). what is wrong in my code please help me i am confused.

UPDATE:

this is how I get JSON from persistence context with RESTful method :

@Inject
private CategoryServiceLocal categoryServiceLocal;

@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Category> dofindAllCategories() {
    return categoryServiceLocal.findAllCategories();
}
majd hwas
  • 95
  • 2
  • 2
  • 10

1 Answers1

0

You have a lazy association from Category to visitors. To load all visitors you need to use left join fetch too.

select c from Category c left join fetch c.visitors

Please, use additional annotations to control how to JSON generated

Infinite Recursion with Jackson JSON and Hibernate JPA issue

Community
  • 1
  • 1
v.ladynev
  • 19,275
  • 8
  • 46
  • 67