开发者

How to configure an Element-collection to map an existing DB table in JPA orm.xml configuration?

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 preferen开发者_开发百科ces). 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,


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")


Have you tried this structure:

Set<Preference> preferences;

then transform your set to a map computationaly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜