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