如何在单个 POJO 中表示 Hibernate 对象的集合?
考虑以下 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一旦删除了客户表,客户对象将不再由 Hibernate 管理。如果您仍然希望拥有包含行程和酒店的客户对象,则需要以编程方式完成。我不确定你是否真的在寻找这个。
一种可能的方法是,
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