2016-07-07 93 views
2

我们可以在集合中的集合的字段上有一个ElementCollection注释吗?JPA休眠和ElementCollection地图

@ElementCollection 
private Map<String, List<String>> mappedData = new HashMap<String, List<String>>(); 

我这个所以不知道让MappingException此刻如果涉及额外的注解或代码,或者如果我必须做出一个新的类,也许使用嵌入式和嵌入式

+0

小心嵌套集合。如果您以后需要将List更改为Set,会发生什么情况?请参阅@AlexR对使用bean的有用建议:http://stackoverflow.com/questions/16516107/how-can-i-store-hashmapstring-arrayliststring-inside-a-list – bphilipnyc

回答

1

您可以使用用户类型或附带JPA的Atribute Converter与我相信。我从来没有测试它的集合,但我相信这是值得出手:

@ElementCollection 
    // applies to each element in the collection 
@Convert(YourCustomConverter.class) 
private Map<String, List<String>> mappedData = new HashMap<String, List<String>>(); 

当涉及到Hibernate,您可以尝试这样UserCollectionType:

http://www.javalobby.org/java/forums/m91832311.html