如何使用 GWT 修复 LazyInitializationException?

发布于 2024-12-05 18:28:36 字数 977 浏览 0 评论 0原文

我有:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尹雨沫 2024-12-12 18:28:36

我强烈建议不要直接在 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 entire Emp object to serialize it for client. That will obviously happen outside findById hence you will get LazyInitializationException if Emp contains any properties that require lazy loading (for example, association lists).

The solution is to use intermediate data transfer object, for example EmpDTO and convert Emp to EmpDTO inside your service transactional block.

瞎闹 2024-12-12 18:28:36

实际上,我通过为域对象创建 CustomFieldSerializer 解决了这个问题。

看一下这个文件: https://github.com/dartmanx/mapmaker/blob/0.5.2/src/main/java/org/jason/mapmaker/shared/model/FeaturesMetadata_CustomFieldSerializer.java

我已经注释掉了相关行,因为我最终不需要它,但代码如下:

public static void serialize(SerializationStreamWriter writer, FeaturesMetadata instance) throws SerializationException {        
    writer.writeInt(instance.getId());
    writer.writeString(instance.getState());
    writer.writeString(instance.getStateAbbr());
    writer.writeString(instance.getUsgsDate());
    writer.writeString(instance.getFilename());
    writer.writeString(instance.getStateGeoId());
    writer.writeString(instance.getCurrentStatus());
    if (instance.getFeatureList().size() == 0) {
        writer.writeObject(new ArrayList<Feature>());
    } else {
        writer.writeObject(new ArrayList<Feature>(instance.getFeatureList()));
    }
}

最后一行接受实例对象的 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:

public static void serialize(SerializationStreamWriter writer, FeaturesMetadata instance) throws SerializationException {        
    writer.writeInt(instance.getId());
    writer.writeString(instance.getState());
    writer.writeString(instance.getStateAbbr());
    writer.writeString(instance.getUsgsDate());
    writer.writeString(instance.getFilename());
    writer.writeString(instance.getStateGeoId());
    writer.writeString(instance.getCurrentStatus());
    if (instance.getFeatureList().size() == 0) {
        writer.writeObject(new ArrayList<Feature>());
    } else {
        writer.writeObject(new ArrayList<Feature>(instance.getFeatureList()));
    }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文