如何在 Hibernate 中映射一组对象而不使用同名的主键?
我有一个代表表的域对象类。 该类与另一个表有关联,但源类的属性与目标类的属性命名不同,我不确定如何休眠映射它。
下面是一个具有该集合的类的示例(一个 CT 到多个 R 实例):
public class CT {
// This is the property in the property-ref down below
private String b;
// The set of Rs I want to get - there may be none that correspond to a CT instance.
private Set rs;
}
public class R {
// This property is mapped to the column name below.
private String rBc;
}
<!--Snippet of the mapping for class CT-->
<set name="rs" lazy="true" sort="MyComparator" table="R" >
<key column="R_COLUMN_NAME_THAT_REPRESENTS_THE_RELATIONSHIP" property-ref="b" />
<one-to-many class="CLASS_THAT_R_IS" />
</set>
Hibernate 接受此映射,但是当我从我知道应该存在的 CT 实例中提取 R 集合时,我只得到一个空的 PersistentSet 。
请注意,对于每个 CT 实例,完全有可能没有或有多个 R。 这就是为什么我在那里有比较器 - 我不知道如何轻松地告诉 Hibernate 如何在没有显式 SQL 的情况下执行 ORDER BY 子句(我在犹豫是否要在 Hibernate 映射中编写它)。
有人可以帮助我吗? ?
I have a domain object class that represents a table. This class has an association to another table, but the source class' property is not named the same as the target class' property, and I'm not sure how to hibernate map it.
Here is an example of the class to have the set (one CT to many R instances):
public class CT {
// This is the property in the property-ref down below
private String b;
// The set of Rs I want to get - there may be none that correspond to a CT instance.
private Set rs;
}
public class R {
// This property is mapped to the column name below.
private String rBc;
}
<!--Snippet of the mapping for class CT-->
<set name="rs" lazy="true" sort="MyComparator" table="R" >
<key column="R_COLUMN_NAME_THAT_REPRESENTS_THE_RELATIONSHIP" property-ref="b" />
<one-to-many class="CLASS_THAT_R_IS" />
</set>
Hibernate accepts this mapping, but when I pull up the set of Rs from a CT instance that I know should exist, I just get an empty PersistentSet.
Note that it is entirely possible that there may be none or more than one R for each CT instance. This is why I have the comparator in there - I can't figure out how to easily tell Hibernate how to do an ORDER BY clause without explicit SQL (which I'm hesitant to write in the Hibernate mapping.
Could someone help me out here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题可能出在
映射中的table="R"
。 这告诉hibernate使用连接表而不是直接在子表中查找外键。 从命名中我了解到您没有使用连接表。 那是对的吗?The problem might be with the
table="R"
in the<set ..>
mapping. This tells hibernate to use a join table in stead of looking for foreign key in the child table directly. From the naming I understand that you are not using a join table. Is that correct?