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 -
錯誤。我認爲這是因爲沒有會話連接。
請發表你的實體類的代碼'Application'和'advertiser' – Yogesh