如何强制初始化 Hibernate JPA 代理以在 JSON 调用中使用它

发布于 2024-10-11 21:35:23 字数 196 浏览 4 评论 0原文

我有一个 Spring 3 + JPA 2.0 应用程序。在我的 @Controller 中,我需要一个初始化的对象,但是我有代理,我需要能够以编程方式初始化它。我需要类似于 org.hibernate.Hibernate.initialize(Object) 的功能。

有人可以帮忙吗?该对象用于 AJAX 操作。如果属性是代理,我无法将其作为 JSON 发送

I have a Spring 3 + JPA 2.0 application. In my @Controller I need an initialized object, but I have proxies , I need to be able to initialize it programmatically. I need functionality similar to org.hibernate.Hibernate.initialize(Object) .

Can someone help . The object is used for AJAX operations. If the properties are proxies I cannot send it as JSON

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

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

发布评论

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

评论(3

我不咬妳我踢妳 2024-10-18 21:35:23

据我所知,没有 JPA 选项。您应该使用Hibernate.initialize(..)。

事实上,当我查看 hibernate 实现时,惰性集合似乎在许多情况下被初始化,这是人们意想不到的。就像 entityManager.contains(..)Persistence.getPersistenceUtil().isLoaded(...) 一样。尝试一下,但我认为您不应该依赖这样的实现细节。

No JPA option to my knowledge. You should use Hibernate.initialize(..).

In fact, when I took a look at the hibernate implementation, lazy collections appear to be initialized in many cases that one wouldn't expect. Like entityManager.contains(..) and Persistence.getPersistenceUtil().isLoaded(...). Give these a try, but I don't think you should rely on such implementation details.

明明#如月 2024-10-18 21:35:23

我们在应用程序中做了类似的事情,我们发现分割数据库实体对象并为 JSON 输出提供另一组类很有用。

如果您使用的 JSON 框架仅检查您的对象并为对象上的每个属性抛出一些 JSON,则能够拥有以下对象:

PersonEntity - 由 JPA 管理的类

PersonJsonOutput - 专门为 JSON 输出设计的类

从长远来看可能更安全。这允许您的数据库更改不会自动反映在 JSON 服务中,您可能希望对 JSON 服务进行版本控制,而不是在数据库实体更改后立即破坏旧版本。

它还可以让您更好地控制 JSON 输出,例如日期格式或强制数据库中的数字成为 JSON 中的字符串等......

这个答案实际上只取决于您如何生成 JSON,但听起来就像你的图书馆进行一些内省一样。

We are doing a similar thing in our application and we have found it useful to split our database entity objects and have another bunch of classes for the JSON output.

If you're using a JSON framework that just inspects your object and chucks out some JSON for each and every property on the object then being able to have objects such as:

PersonEntity - Class managed by JPA
and
PersonJsonOutput - Class specifically designed for JSON output

Might be safer in the long run. This allows you to have database changes that don't automatically get reflected in your JSON service, you might want to version your JSON service perhaps rather than break old versions as soon as your database entity changes.

It also gives you more control of your JSON output in terms of say date formats or forcing numbers in the database to be strings in your JSON, etc...

This answer really just depends on how you're generating your JSON, but it sounds like your library does some introspection.

压抑⊿情绪 2024-10-18 21:35:23

我知道已经晚了,答案已被接受,但另一种标准的 JPA 方法是在从 DAO 返回对象之前调用您希望初始化的列表上的 size() 方法:

Object.getList().size(); 

这使您不必作弊并使用实现- 初始化的特定机制

I know it is late and the answer is accepted, but another standard JPA way is to call the size() method on the list you wish to initialize prior to returning the object from the DAO:

Object.getList().size(); 

This saves you having to cheat and use an implementation-specific mechanism for initialization

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