java - Ordered collection with Hibernate -


i have container class this:

@entity class container {      private set<item> itemsinternal;      private observableset<item> items;      private observableset<item> roitems;       // orm access property       public container() {          setitemsinternal(new hashset<>());      }       @transient      public observableset<item> getitems() {return roitems;}       // private methods orm          @onetomany(mappendby = "parent")      private set<item> getitemsinternal() {           return itemsinternal;      }       private void setitemsinternal(set<item> value) {          itemsinternal = value;          items = fxcollection.observableset(itemsinternal);          roitems = fxcollection.unmodifiableobservableset(items);      } } 

as can see, container object-relational mapped , contains set of items , exposes clients unmodifiable observableset of items (also, container implements other methods working items, not shown here). after need ordered items timestamp, somethig this:

 @onetomany(mappedby = "parent")  @orderby("created")  set<item> items = new linkedhashset<item>(); 

but in current implementation shown before, orm sets set setter:

 private void setitemsinternal(set<item> value) {      itemsinternal = value;      items = fxcollection.observableset(itemsinternal);      roitems = fxcollection.unmodifiableobservableset(items);  } 

and haven't possibility use linkedhashset background set.

how can use linkedhashset background set in case? enough set required class in constructor?

 public container() {          setitemsinternal(new linkedhashset<>());  } 

or may replace set list (given want have access items index)? here drawbacks?

from bauer, java persistence hibernate, second edition, p.143, cite "we don't recommend initializing collections late, in constructor or setter methods". , further "hibernate wraps collection you've initialized on declaration of filed, or replaces it, if it's not right one". in book says, if need set stable interation order, can use linkedhashset. javadoc of persistentset (a persistent wrapper set) see, "the underlying collection hashset."

so, first, not clear me, why isn't recommended initialize collections in constructor or setter, seems in shown case ok. second, if set marked @orderby, when retriving entity hibernate uses linkedhashset underlying collection instead of hashset. so, stable iteration order enough mark set annotation @orderby , use linkedhashset first time initialization.

@entity class container {      private set<item> itemsinternal;      private observableset<item> items;      private observableset<item> roitems;       // orm access property       public container() {         // first time collection initialization           setitemsinternal(new linkedhashset<>());      }       @transient      public observableset<item> getitems() {return roitems;}       // private methods orm          @onetomany(mappendby = "parent")      // order data when querying db       // , use linkedhashset underlying collection      @orderby("created")       private set<item> getitemsinternal() {           return itemsinternal;      }       private void setitemsinternal(set<item> value) {          itemsinternal = value;          items = fxcollection.observableset(itemsinternal);          roitems = fxcollection.unmodifiableobservableset(items);      } } 

Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -