gpt4 book ai didi

java - Hibernate:指定父实体 中的哪一列应该使用

转载 作者:行者123 更新时间:2023-12-04 06:47:11 24 4
gpt4 key购买 nike

我的 HBM 文件中有以下实体:


<join table="v_price_change_current_prices" fetch="join" inverse="true">
<key column="product_color_id" />
<property name="oldMSRP" column="msrp" />
<property name="oldList" column="list" />
</join>


我的 PRICE_CHANGE 表有一个 ID 列(主键)和一个 PRODUCT_COLOR_ID 列。

我希望 SQL Hibernate 生成将 V_PRICE_CHANGE_CURRENT_PRICES 加入到 PRICE_CHANGE.PRODUCT_COLOR_ID,如下所示:
    SELECT * from PRICE_CHANGE a, V_PRICE_CHANGE_CURRENT_PRICES b 
where a.product_color_id=b.product_color_id

但它加入到 PRICE_CHANGE.ID 代替
    SELECT * from PRICE_CHANGE a, V_PRICE_CHANGE_CURRENT_PRICES b 
where a.product_color_id=b.product_color_id

有没有办法告诉元素使用 PRICE_CHANGE.PRODUCT_COLOR_ID 而不是 PRICE_CHANGE.ID?

最佳答案

根据文档,这可能可以使用 property-ref 来实现。在 <key>元素:

5.1.21. Key

The <key> element has featured a few times within this guide. It appears anywhere the parent mapping element defines a join to a new table that references the primary key of the original table. It also defines the foreign key in the joined table:

<key
column="columnname" (1)
on-delete="noaction|cascade" (2)
property-ref="propertyName" (3)
not-null="true|false" (4)
update="true|false" (5)
unique="true|false" (6)
/>
  1. column (optional): the name of the foreign key column. This can also be specified by nested element(s).
  2. on-delete (optional - defaults to noaction): specifies whether the foreign key constraint has database-level cascade delete enabled.
  3. property-ref (optional): specifies that the foreign key refers to columns that are not the primary key of the original table. It is provided for legacy data.
  4. not-null (optional): specifies that the foreign key columns are not nullable. This is implied whenever the foreign key is also part of the primary key.
  5. update (optional): specifies that the foreign key should never be updated. This is implied whenever the foreign key is also part of the primary key.
  6. unique (optional): specifies that the foreign key should have a unique constraint. This is implied whenever the foreign key is also the primary key.


但我不确定这是否真的实现了(见 HHH-551HHH-1829 )。

如果您真的不想使用共享主键,另一种选择是使用组合和“真实” one-to-one协会代替。

引用
  • Hibernate 核心引用文档
  • 5.1.13. One-to-one
  • 5.1.20. "Join"
  • 5.1.21. Key
  • 关于java - Hibernate:指定父实体 <join/> 中的哪一列应该使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3579618/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com