Mapping a Map<String, Double> with Hibernate and JPA

别等时光非礼了梦想. 提交于 2019-12-06 06:42:38

问题


I try the following mapping

@ElementCollection
private Map<String, Double> doubleValues;

But when I generate my schema from this (using mvn hibernate3:hbm2ddl), I get the following errors:

org.hibernate.MappingException: Could not determine type for: java.util.Map, at table: ImagerAnalysisResult, for columns: [org.hibernate.mapping.Column(doubleValues)]

I've tried adding column and type information to this, but I keep getting the same error. Also, I don't think that information should be given as hibernate should use the generic declaration (according to How to map a Map<String,Double>).

I'm using hibernate version 3.6.4.Final and I've tried other versions

Any suggestions? Thanks.

Edit: it appears that the maven hibernate plugin is two years old and depends on an older hibernate version... why is this plugin not being maintained?

Edit: named field "doubleValues" to make sure there's no reserved-name-issue.


回答1:


The word "values" is a SQL reserved keyword and you cannot use it for an identifier in SQL. The same thing would haven if you try to name something "index". If you change the name to something else it should work.

You know:

insert into table values (...)

I've had similar issues in the past while autogenerating code due to the use of other keywords too.

By the way, in the past I experimented several issues with Hibernate 3.6.2 and map collections. You might like to take a look a this answer, since you seem to be heading the same way.

I hope that helps!




回答2:


You can try to be more explicit:

@ElementCollection
@CollectionTable(name = "MY_ENTITY_VALUES", joinColumns = @JoinColumn(name = "entity_id"))
@Column(name = "DOUBLE_VALUES")
private Map<String, Double> doubleValues;


来源:https://stackoverflow.com/questions/6096525/mapping-a-mapstring-double-with-hibernate-and-jpa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!