当对象具有多对多关系时,如何将对象从控制器发送到视图?

发布于 2024-12-26 10:19:13 字数 262 浏览 3 评论 0原文

我有一个学校实体,它与年级有多对多的关系。 我可以成功加载我的 school 对象并将其发送到我的视图(这是一个 .ftl 页面,而不是 jspx),问题是;当我以 JSON 形式请求 url(仅接收序列化数据)(在执行 jquery get 后用于填充字段)时,我得到 --failed to lazily初始化角色集合:-- 我发现当我请求一个 JSON 格式的页面,并且该对象没有多对多关系,它会给我序列化字符串,但是当该对象具有多对多关系时,我会收到该错误。 有谁遇到过这个错误,或者知道我需要做什么?

I have a school entity and it has a many to many relationship with grade-levels.
I can successfully load my school object and send it to my view (which is a .ftl page, not jspx) the problem is; when I request the url as a JSON (to only receive the serialized data)(which I use to populate fields after doing a jquery get) I get a --failed to lazily initialize a collection of role:-- I have found that when I request a page as JSON and the object does not have a many to many relationship it will give me the serialized string just fine, however when the object has a many to many relationship I get that error.
Has anyone come across this error, or know what I need to do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

野鹿林 2025-01-02 10:19:13

我找到了这个解决方案,但我不知道它是否是最好的...有什么意见...我将此代码添加到我的 web.xml 文件中,

<filter>
    <filter-name>OpenEntityManagerInViewFilter</filter-name>
    <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>OpenEntityManagerInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

这允许对象返回。
希望它可以帮助任何尝试做同样事情的人。有谁有更好的解决方案或者这是正确的吗?

谢谢大家

I found this solution but i dont know if it is the best... any opinions... i added this code to my web.xml file

<filter>
    <filter-name>OpenEntityManagerInViewFilter</filter-name>
    <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>OpenEntityManagerInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

this allowed the object to come back.
hope it helps anyone that was trying to do the same thing. does anyone have a better solution or is this correct?

thanks everyone

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