Resteasy JPA problem
leader01 May 27, 2013 9:27 AMHello, please I need your help
I'm working in my jboss entreprise project with JPA EJB and Jboss AS 7.0.2 as my server runtime.
Well I'm new in rest and I don't know how I can do the thing with JPA, this is my the entity that I want lunch a list of as ressource:
package model;
import java.io.Serializable;
import javax.persistence.*;
import java.util.List;
/**
* The persistent class for the agence database table.
*
*/
@Entity
public class Agence implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@Lob
private String adresse;
@Lob
private String agence;
@Lob
private String fax;
@Lob
private String tel;
private String ville;
//bi-directional many-to-one association to Banque
@ManyToOne
@JoinColumn(name="idbanque")
private Banque banque;
//bi-directional many-to-one association to Salarie
@OneToMany(mappedBy="agence")
private List<Salarie> salaries;
//bi-directional many-to-one association to Societe
@OneToMany(mappedBy="agence")
private List<Societe> societes;
public Agence() {
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getAdresse() {
return this.adresse;
}
public void setAdresse(String adresse) {
this.adresse = adresse;
}
public String getAgence() {
return this.agence;
}
public void setAgence(String agence) {
this.agence = agence;
}
public String getFax() {
return this.fax;
}
public void setFax(String fax) {
this.fax = fax;
}
public String getTel() {
return this.tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getVille() {
return this.ville;
}
public void setVille(String ville) {
this.ville = ville;
}
public Banque getBanque() {
return this.banque;
}
public void setBanque(Banque banque) {
this.banque = banque;
}
public List<Salarie> getSalaries() {
return this.salaries;
}
public void setSalaries(List<Salarie> salaries) {
this.salaries = salaries;
}
public List<Societe> getSocietes() {
return this.societes;
}
public void setSocietes(List<Societe> societes) {
this.societes = societes;
}
}
and this is the ejb mb
package ejb;
import java.util.ArrayList;
import javax.ejb.LocalBean;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import model.Agence;
/**
* Session Bean implementation class ejbParameters
*/
@Stateful
@LocalBean
public class ejbParameters implements ejbParametersRemote {
@PersistenceContext(type = PersistenceContextType.EXTENDED)
EntityManager em;
/**
* Default constructor.
*/
public ejbParameters() {
}
@Override
public ArrayList<Agence> allAgences() {
ArrayList<Agence> societes = new ArrayList<Agence>();
societes.addAll(em.createQuery("select a from Agence a").getResultList());
return societes;
}
@Override
public void addAgnece(Agence a) {
// TODO Auto-generated method stub
}
}
and this is the SW:
package ejbrest;
import java.util.ArrayList;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import model.Agence;
import ejb.ejbParametersRemote;
@Path("/ressources")
@Stateless
@LocalBean
public class ServicesWS {
@EJB
ejbParametersRemote ejb;
@GET
@Path("/agences")
@Produces("application/json")
public ArrayList<Agence> getCustomer() {
return ejb.allAgences();
}
}
when I access to the URL localhost:8080/myproject/ressources/agences as a result I get :
[{"adresse":"Cartier Hassan II, Agadir","salaries":[],"tel":"05...........","ville":"Agadir","fax":null,"agence":"Agence Kettani","banque":{"nom":"Banque Populaire","agences":[{"adresse":"Cartier Hassan II, Agadir","salaries":[],"tel":"05...........","ville":"Agadir","fax":null,"agence":"Agence Kettani","banque":{"nom":"Banque Populaire","ag
I have only one row in my table Agence, and it have a "many to one" relation with the table Banque and it looks like that the resteasy publichess an infinite cycle of jsons one row inside wich there is a banque wich has a number of agences(one to many) and etc.......
I forgot to mention that it's only after changing the annotation @Stateless to @Statefull of the ejb that the error bellow goes:
LazyInitializationException: failed to lazily initialize a collection of role...................
and also I tried to add the annotation @XMLRoot ... but nothing changed
I appreciate your help
Cordialy....