I have:
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
This is my service:
@Service("empService")
public class EmpServiceImpl extends RemoteServiceServlet implements EmpService {
@Autowired
EmpHome empHome;
@Override
@Transactional
public Emp findById(short id) {
return empHome.findById(id);
}
Im trying to use my service in gwt:
EmpServiceAsync empServiceAsync = GWT.create(EmpService.class);
AsyncCallback<Emp> callback = new AsyncCallback<Emp>() {
@Override
public void onFailure(Throwable caught) {
Info.display("Failure", "что-то пошло не так");
}
@Override
public void onSuccess(Emp result) {
Info.display("Succes", result.getEname());
}
};
empServiceAsync.findById((short) 7844, callback);
I would highly discourage using Hibernate mapped object
Empin GWT client side directly.Your Hibernate session will only be available inside
findByIdas it is marked@Transactional, however, GWT will need to traverse the entireEmpobject to serialize it for client. That will obviously happen outsidefindByIdhence you will getLazyInitializationExceptionifEmpcontains any properties that require lazy loading (for example, association lists).The solution is to use intermediate data transfer object, for example
EmpDTOand convertEmptoEmpDTOinside your service transactional block.