Hibernate 在创建后立即加载对象

发布于 2024-12-12 03:54:01 字数 1302 浏览 1 评论 0 原文

我有一个 Hibernate 实体,它具有一个或多个 映射,例如

<hibernate-mapping>
  <class name="MyClass" table="my_table">
    <cache usage="nonstrict-read-write"/>
      <composite-id>
        <key-property name="id" length="30"/>
        <key-property name="someRef" length="30" column="foreign_key_to_something"/>
      </composite-id>
    <many-to-one name="mappedProperty" column="foreign_key_to_something" insert="false" update="false"/>
    <property name="foo" column="foo"/>
    ...
  </class>
</hibernate-mapping>

我需要创建这样一个实体,并在创建后立即访问 mappedProperty 。我在这里可以看到两种方法:

1) 创建实体并手动设置所有相关的 映射。这种方法的明显缺点是需要跑腿工作,特别是在映射的<多对一实体数量很高的情况下。如果框架可以为您做一些事情,为什么还要手动做一些事情呢?

2) 仅通过初始化必要的参数(例如上述情况下的主键、idsomeRef)来创建实体,然后立即保存并重新加载。加载应自动初始化 mappedProperty 或根据需要提供延迟初始化。

我更喜欢选项 2),但是,我注意到在某些情况下,未设置 mappedProperty 属性。 load() 返回与我传递给 create() 相同的对象,只是初始化了主键。我仍然不确定为什么会发生这种情况,但为了解决这个问题,我必须将对象从 Hibernate 会话中分离出来,这样 load() 将被迫访问数据库并重新获取。再说一遍,听起来太复杂了,不是吗?

我在这里错过了什么吗?还有其他方法可以解决这个问题吗?

I have a Hibernate entity which has one or more <many-to-one mappings, e.g.

<hibernate-mapping>
  <class name="MyClass" table="my_table">
    <cache usage="nonstrict-read-write"/>
      <composite-id>
        <key-property name="id" length="30"/>
        <key-property name="someRef" length="30" column="foreign_key_to_something"/>
      </composite-id>
    <many-to-one name="mappedProperty" column="foreign_key_to_something" insert="false" update="false"/>
    <property name="foo" column="foo"/>
    ...
  </class>
</hibernate-mapping>

I need to create such an entity and get immediate access to mappedProperty after the creation. I can see two approaches here:

1) Create the entity and set all related <many-to-one mappings manually. The obvious disadvantage of this approach is the legwork required, especially if the amount of mapped <many-to-one entities is high. Why do something manually if framework can do it for you?

2) Create the entity by only initializing necessary parameters (e.g. primary keys, id and someRef in the above case), then save and re-load it immediately. Loading should initialize the mappedProperty automatically or provide lazy initialization on demand.

I prefer option 2), however, I have noticed that in some cases, mappedProperty property is not set. load() returns the same same object I passed to create(), only having primary keys initialized. I am still not sure why this happens, but to fight it I'd have to detach the object from the Hibernate session so the load() would be forced to go to the database and get anew. Again, sounds quite overcomplicated, doesn't it?

Am I missing something here? Are there any other ways to solve this problem?

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

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

发布评论

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

评论(2

追我者格杀勿论 2024-12-19 03:54:01

当您需要从数据库刷新对象的状态时,请使用refresh()

When you need to refresh state of the object from the database, use refresh().

入画浅相思 2024-12-19 03:54:01

loadget 从会话中返回对象(如果该对象已存在)。因此,您的第二个选项需要您刷新,然后从会话中删除实体,然后重新加载它(对您知道的数据发出选择查询)。对刷新的调用会更容易地执行相同的操作,但仍然会无济于事地发出额外的选择查询。

我真的更喜欢第一个选项:只需确保每次设置关联时,也会映射相应的映射属性。您负责实体的一致性和不变量(与双向关联一样,双方都必须得到维护)。

第三种选择是完全删除映射的属性,并简单地调用entity.getKey().getSomeRef().getId()。

load and get return the object from the session if it's already there. Your second option would thus need you to flush, then remove the entity from the session, then reload it (issuing a select query for data you know). A call to refresh would do the same, more easily, but would still issue an additional select query for nothing.

I really prefer the first option : just make sure that each time the association is set, then the corresponding mapped property is mapped as well. You're responsible for the coherence and invariants of the entities (as with bidirectional associations, where both sides must be maintained).

A third option would be to remove the mapped property completely, and simply call entity.getKey().getSomeRef().getId().

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