如何在单个 POJO 中表示 Hibernate 对象的集合?

发布于 2024-12-13 03:50:40 字数 1367 浏览 6 评论 0原文

考虑以下 Hibernate 映射:

<hibernate-mapping package="org.example">
  <class name="Customer" table="CUSTOMER">
    <id name="customerId" column="customer_id"/>

    <bag name="itineraries" table="ITINERARY" inverse="true" cascade="all">
      <key column="customer_id"/>
      <one-to-many class="Itinerary"/>
    </bag>

    <bag name="hotels" table="HOTEL" inverse="true" cascade="all">
      <key column="customer_id"/>
      <one-to-many class="Hotel"/>
    </bag>
  </class>
</hibernate-mapping>

<hibernate-mapping package="org.example">
    <class name="Itinerary" table="ITINERARY">
      <many-to-one name="customer" column="customer_id" update="false" not-null="true"/>
      ...other properties...
    </class>
</hibernate-mapping>

<hibernate-mapping package="org.example">
    <class name="Hotel" table="HOTEL">
      <many-to-one name="customer" column="customer_id" update="false" not-null="true"/>
      ...other properties...
    </class>
</hibernate-mapping>

现在假设您需要删除 CUSTOMER 表。您将如何重构映射/模型,以便 Customer Java 对象继续包含基于 customerId 的 Itinerary 和 Hotel 对象列表?上述 Hotel 和 Itinerary 对象仍然需要由 Hibernate 管理。

我能想到的最好的办法是,当调用者请求列表时,Customer 对象会遵从 DAO。是否有更干净的方法仍然允许客户对象存在于每个行程和酒店对象中?

Consider the following Hibernate mappings:

<hibernate-mapping package="org.example">
  <class name="Customer" table="CUSTOMER">
    <id name="customerId" column="customer_id"/>

    <bag name="itineraries" table="ITINERARY" inverse="true" cascade="all">
      <key column="customer_id"/>
      <one-to-many class="Itinerary"/>
    </bag>

    <bag name="hotels" table="HOTEL" inverse="true" cascade="all">
      <key column="customer_id"/>
      <one-to-many class="Hotel"/>
    </bag>
  </class>
</hibernate-mapping>

<hibernate-mapping package="org.example">
    <class name="Itinerary" table="ITINERARY">
      <many-to-one name="customer" column="customer_id" update="false" not-null="true"/>
      ...other properties...
    </class>
</hibernate-mapping>

<hibernate-mapping package="org.example">
    <class name="Hotel" table="HOTEL">
      <many-to-one name="customer" column="customer_id" update="false" not-null="true"/>
      ...other properties...
    </class>
</hibernate-mapping>

Now say you need to remove the CUSTOMER table. How would you refactor the mappings/model such that the Customer Java object continues to contain Lists of Itinerary and Hotel objects based on a customerId? Said Hotel and Itinerary objects still need to be managed by Hibernate.

The best I can come up with is the Customer object deferring to DAOs when callers request a List. Is there a cleaner approach that will still allow the Customer object to live in each Itinerary and Hotel object?

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

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

发布评论

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

评论(1

汐鸠 2024-12-20 03:50:40

一旦删除了客户表,客户对象将不再由 Hibernate 管理。如果您仍然希望拥有包含行程和酒店的客户对象,则需要以编程方式完成。我不确定你是否真的在寻找这个。

一种可能的方法是,

  1. 假设您有一个带有 getCustomer() 方法的 DAO。这接受客户 ID。
  2. 此方法会再触发两个查询,以根据客户 ID 获取行程和酒店列表。
  3. 它创建一个 Customer 对象,并将 Itinerary 和 Hotel 列表设置到其中,然后返回它和填充的客户对象。

Once you remove the customer table, the customer object will not be managed by Hibernate anymore. If you still want to have a customer object containing Itinerary and Hotel, then that needs to be done programmatically. I am not sure if you are really looking for this.

One possible way of doing this is

  1. Lets say you have a DAO with a method getCustomer(). This accepts a customer id.
  2. This method fires two more queries to fetch lists of Itinerary and Hotel based on the customer id.
  3. It creates a Customer object and sets the lists of Itinerary and Hotel into it and returns it and populated customer object.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文