如何配置 Element-collection 以映射 JPA orm.xml 配置中的现有数据库表?
我已将一个应用程序从全 Hibernate 功能的应用程序迁移到基于 JPA/Hibernate 的应用程序,但我的 JPA 映射存在问题。
我们已经设法使用 orm.xml 来映射我们的实体。一切都工作正常,直到我不得不处理元素集合。 实际上,我有一个名为 User 的实体,它有一个嵌入式地图(名为 preferences 的地图集合)。所以我们有类似的东西:
public class User {
private Long id;
private Map<String, String> preferences;
}
在我们的完整休眠版本中,生成了一个 PREFERENCE(ID, name, value) 表。但是当我们尝试将其迁移到 JPA 时,我们使用了以下映射:
<element-collection name="preferences" target-class="java.lang.String" fetch="LAZY">
<map-key-class class="java.lang.String" />
<map-key-column name="[name]" />
<column name="[value]" />
<collection-table name="PREFERENCE">
<join-column name="ID" />
</collection-table>
</element-collection>
并生成了一个新的 User_Preferences 表。即使我在 xml 配置中指定了 name='PREFERENCES' 属性,我仍无法让元素集合指向现有表 PREFERENCES。
您是否经历过这种情况? 任何帮助将不胜感激。
非常感谢各位,
I have migrated an app from a full-Hibernate featured one to a JPA/Hibernate based-one and I have a problem with my JPA mapping.
We have managed to use orm.xml to map our entities. Everything was working fine until I have had to deal with an Element-collection.
Actually, I have an entity called User which has an embedded map (Map collection called preferences). So we have something like that :
public class User {
private Long id;
private Map<String, String> preferences;
}
In our full-hibernate version, a PREFERENCE(ID, name, value) table gets generated. But when we tried to migrate it to JPA, we used the following mapping :
<element-collection name="preferences" target-class="java.lang.String" fetch="LAZY">
<map-key-class class="java.lang.String" />
<map-key-column name="[name]" />
<column name="[value]" />
<collection-table name="PREFERENCE">
<join-column name="ID" />
</collection-table>
</element-collection>
and a new User_Preferences table gets generated. Even though I have specified a name='PREFERENCES' attribute in the xml configuration, I can't get the element-collection to point to the existing table PREFERENCES.
Have you ever experienced this situation ?
Any help would really be appreciated.
Thanks a lot guyz,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来像一个错误。我无法指出您的映射有任何问题,我使用 EclipseLink 2.3.0 进行了尝试,并创建了一个名为 PREFERENCE 的映射表。还尝试使用 Hibernate 3.5.6,表名是 USER_PREFERENCES 正如你所说。
看起来他们只是放弃了集合表的名称。但与注释相同效果很好:
Looks like a bug. I cannot point anything wrong with your mappings and I tried it with EclipseLink 2.3.0 and such a mapping created table named PREFERENCE. Also tried with Hibernate 3.5.6, and name of table is USER_PREFERENCES as you said.
Looks like they simply discard name for collection-table. But same with annotations works fine:
您是否尝试过这种结构:
然后将您的集合转换为计算地图。
Have you tried this structure:
then transform your set to a map computationaly.