如何使用 GWT 修复 LazyInitializationException?
我有:
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
这是我的服务:
@Service("empService")
public class EmpServiceImpl extends RemoteServiceServlet implements EmpService {
@Autowired
EmpHome empHome;
@Override
@Transactional
public Emp findById(short id) {
return empHome.findById(id);
}
我正在尝试在 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 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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我强烈建议不要直接在 GWT 客户端使用 Hibernate 映射对象
Emp
。您的 Hibernate 会话仅在
findById
内可用,因为它被标记为@Transactional
,但是,GWT 将需要遍历整个Emp
对象来序列化为客户。这显然会发生在findById
之外,因此如果Emp
包含任何需要延迟加载的属性(例如关联列表),您将得到LazyInitializationException
。解决方案是使用中间数据传输对象,例如
EmpDTO
并在服务事务块内将Emp
转换为EmpDTO
。I would highly discourage using Hibernate mapped object
Emp
in GWT client side directly.Your Hibernate session will only be available inside
findById
as it is marked@Transactional
, however, GWT will need to traverse the entireEmp
object to serialize it for client. That will obviously happen outsidefindById
hence you will getLazyInitializationException
ifEmp
contains any properties that require lazy loading (for example, association lists).The solution is to use intermediate data transfer object, for example
EmpDTO
and convertEmp
toEmpDTO
inside your service transactional block.实际上,我通过为域对象创建 CustomFieldSerializer 解决了这个问题。
看一下这个文件: https://github.com/dartmanx/mapmaker/blob/0.5.2/src/main/java/org/jason/mapmaker/shared/model/FeaturesMetadata_CustomFieldSerializer.java
我已经注释掉了相关行,因为我最终不需要它,但代码如下:
最后一行接受实例对象的 getFeatureList() 的参数,这实际上是一个Hibernate PersistanceBag,并用所述 PersistentBag 的内容写出一个实际的 ArrayList。
I actually got around this issue by creating a CustomFieldSerializer for my domain objects.
Take a look at this file: https://github.com/dartmanx/mapmaker/blob/0.5.2/src/main/java/org/jason/mapmaker/shared/model/FeaturesMetadata_CustomFieldSerializer.java
I've commented out the relevant lines because I ended up not needing it, but here's the code:
The final line takes the takes an argument of the instance object's getFeatureList(), which is actually a Hibernate PersistentBag, and writes out an actual ArrayList with the contents of said PersistentBag.