如何配置 Element-collection 以映射 JPA orm.xml 配置中的现有数据库表?

发布于 2024-12-07 08:57:59 字数 1057 浏览 1 评论 0原文

我已将一个应用程序从全 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月隐月明月朦胧 2024-12-14 08:57:59

看起来像一个错误。我无法指出您的映射有任何问题,我使用 EclipseLink 2.3.0 进行了尝试,并创建了一个名为 PREFERENCE 的映射表。还尝试使用 Hibernate 3.5.6,表名是 USER_PREFERENCES 正如你所说。

看起来他们只是放弃了集合表的名称。但与注释相同效果很好:

@ElementCollection(targetClass = java.lang.String.class)
@MapKeyClass(java.lang.String.class)
@MapKeyColumn(name="name")
@CollectionTable(name = "PREFERENCE", joinColumns = @JoinColumn(name="ID"))
@Column(name="value")

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:

@ElementCollection(targetClass = java.lang.String.class)
@MapKeyClass(java.lang.String.class)
@MapKeyColumn(name="name")
@CollectionTable(name = "PREFERENCE", joinColumns = @JoinColumn(name="ID"))
@Column(name="value")
孤芳又自赏 2024-12-14 08:57:59

您是否尝试过这种结构:

Set<Preference> preferences;

然后将您的集合转换为计算地图。

Have you tried this structure:

Set<Preference> preferences;

then transform your set to a map computationaly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文