Home » Support » Forum » Problem persisting a TreeSet field

Problem persisting a TreeSet field

#1

Hello,

I am getting a "java.lang.IllegalArgumentException: Can not set java.util.TreeSet field Message.children to java.util.HashSet" when persisting a Message object with a parent/children relationship:

@ManyToOne
private Message parent;

@OneToMany(mappedBy = "parent")
private TreeSet<Message> children;

Seems that objectdb is converting TreeSet into HashSet? Why? http://www.objectdb.com/java/jpa/entity/types says TreeSet is supported...

Thanks for pointers to the solution! I need the TreeSet to have a well defined traversal order when iterating through the Set.

Best regards, Benjamin

 

#2

Your definition of field children is invalid in JPA - Only collection interface types (Collection, Set, List, Map) should be used in persistent field definition. ObjectDB doesn't enforce this rule for ordinary persistent fields - but mapped by (inverse) fields are more limited.

The order of elements in a mapped by (inverse) collection field is set by an automatic query. You can define the field as List and use the @OrderBy annotation to refine the automatic query to return ordered elements.

Alternatively, you can use TreeSet with your order as an ordinary persistent field instead of as an inverse field - remove he mappedBy setting, and update the TreeSet explicitly - ObjectDB doesn't need a bidirectional relationship to store collections, and actually avoiding them may improve performance as explained in the manual.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
#3

Thank you very much for your professional, super fast support! Highly appreciated! 

Best, Benjamin

 


Post Reply

To post a reply and/or subscribe to update notifications - please login