【发布时间】:2016-10-21 17:34:14
【问题描述】:
我现在被困了一会儿,我需要一些帮助。
我想在两个表中的任何一个中都不是 PK 的 2 个字段之间在休眠中进行映射
Table Category (
catId Numeric(10),
categoryBusinessRef Numeric(10)
)
Table Product (
productId Numeric(10),
categoryBusinessRef Numeric(10)
)
Sql 查询是:
SELECT *
from Category as a
Join Product as b on a.categoryBusinessRef = b.categoryBusinessRef
但在 Hibernate 中,它让我有了这个映射
SELECT *
from Category as a
Join Product as b on a.categoryId = b.categoryBusinessRef
我的hbms看起来很喜欢
<class name="Category" table="A">
<id name="categoryId" length="10">
<column name="categoryid" />
<generator class="sequence">
<param name="sequence">S_category</param>
</generator>
</id>
<set name="categoryBusinessRefs" table="B" >
<key>
<column name="categoryBusinessRef" />
</key>
<one-to-many class="ProductClass" />
</set>
</class>
<class name="Product" table="B">
<id name="productId" length="10">
<column name="productid" />
<generator class="sequence">
<param name="sequence">S_product</param>
</generator>
</id>
<property name="categoryBusinessRef" length="10">
<column name="categoryBusinessRef" />
</property>
</class>
所以这是一个一对多的关系,但它必须与另一个值作为类别的 PK 进行映射
感谢您的帮助
编辑:
如果我做不到,那好吧!但是如果我可以这样做,我知道所有带有主键的东西,但我的问题不是“在 Hibernate 中映射到 PK”而是“在 Hibernate 中映射到另一个字段而不是 PK”,所以说地图的答案我对PK不感兴趣:p
【问题讨论】:
-
我猜 Table
A是 OneToMany-Relation 的1端,对吗?那么,表A中的属性disc是错误的。 -
我没有选择我认为的商品名称,我将重命名那些变量
-
这不是名字好坏的问题。我的假设正确吗?
-
是的,它是正确的,但这些不是唯一的
-
确实是多对多
标签: java hibernate mapping one-to-many hibernate-onetomany