2014-02-20 65 views
0

我有以下REST服務有道關閉實體管理器

public class myRestService { 
    @SuppressWarnings("unchecked") 
    @GET 
    @Path("") 
    @Produces(MediaType.APPLICATION_JSON) 
    public Response getApplications(
      @QueryParam("Id") String Id) throws Exception { 
     EntityManager manager = HibernateUtil.getEntityManager(); 
     EntityTransaction transaction = manager.getTransaction(); 
     List<Application> applications = new ArrayList<Application>(0); 

     try { 
      transaction.begin(); 
      Query query = manager.createQuery("select a from Application a where id= :Id"); 
      query.setParameter("Id", Id); 
      applications = query.getResultList(); 
      transaction.commit(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      //manager.close(); 
     } 

     return Response.ok(applications).build(); 
    } 
} 

返回的語句如下不定式循環。我認爲這是因爲我沒有關閉經理。

[{"name":"app1","id":1,"host":"http://www.app1.com","appType":"1","advertiser":{"name":"advertiser1","id":3,"status":"Inactive","applications":[{"name":"app1","id":1,"host":"http://www.app1.com","appType":"1","advertiser": 
    {"name":"advertiser1","id":3,"status":"Inactive","applications":...................... 

如果我關閉管理器,我會得到org.codehaus.jackson.map.JsonMappingException: failed to lazily initialize a collection of role: com.model.Advertiser.applications, could not initialize proxy -錯誤。我認爲這是因爲沒有會話連接。

+0

請發表你的實體類的代碼'Application'和'advertiser' – Yogesh

回答

0

我認爲你面臨這個無限循環的原因是你的數據庫配置有一個循環。

像這樣的東西(例子):

A db record references B db record 
    B db record references A db record 
...