text: Q:
  
  Order inside TreeMap wrong
  
  I try to store the "modules" data in a specific order inside a map. I use TreeMap to realise this. 
  When I read the data i get it in exactly the wrong order. It starts with key value 9 and stops a 0. ??? Does JPA do s.t. else then "putting" the values into the TreeMap?
  @OneToMany(
      mappedBy="prditem", 
      fetch=FetchType.LAZY, 
      cascade=CascadeType.ALL,
      orphanRemoval=true,
      targetEntity=PubModule.class
  )
  @JoinColumn(name="prditmID")
  @MapKey(name="order")
  private Map<Integer, PubModule> modules = new PubModulesMap();
  
  public class PubModulesMap extends TreeMap<Integer,PubModule> {
  
      @Override
  public PubModule put(Integer key, PubModule value) {
      PubModule old = (PubModule)this.get(key);
          if (old!=null) {
              old.setPrditem(value.getPrditem());
      ....
  }
  
  Iterator <PubModule> it = list.values().iterator();
  while (it.hasNext()) {
      PubModule pm = it.next();
      System.out.println(pm.getOrder());
  }
  
  9
  8
  7
  6
  5
  4
  3
  2
  1
  0
  
  A:
  
  The reason for unexpected order is that, JPA does not understand that the map should be sorted.
  Generic solution would be to create a getter method for your map:
  public Map<Integer, PubModule> getModules() {
     if (modules == null) {
        modules = new PubModulesMap();
     } else if (!(modules instanceof TreeMap)) {
        modules = new TreeMap(modules); // Or new PubModulesMap(modules)
     }
     return modules;
  }
  
  If you're using hibernate, you should add @Sort(type = SortType.NATURAL) annotation.
  
  