3

Can we have an ElementCollection annotation on a field with collection within a collection?

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

I am getting MappingException at the moment with this so not sure if there are additional annotations or code involved or if I have to make a new class and perhaps use Embeddable and Embedded

  • Be careful with nested collections. What happens if you need to later change `List` to a `Set`? See the helpful advice from @AlexR on using a bean instead: http://stackoverflow.com/questions/16516107/how-can-i-store-hashmapstring-arrayliststring-inside-a-list – riddle_me_this Jul 07 '16 at 21:44

1 Answers1

1

You can use UserType or Atribute Converter with JPA attached to your collection I believe. I have never tested it on collection but I believe it is worth the shot:

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

When it comes to Hibernate you can try something like this UserCollectionType:

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

Alexander Petrov
  • 9,204
  • 31
  • 70