title: My LinkedList is not saved as it should be (regarding its elements order)
body: <p>First of all, pardon me for my poor english level. I will try to be as understandable as I can.</p>
  
  <p>I am trying to re-order a playlist of music files. A Playlist is basically a <code>LinkedList&lt;MusicFiles&gt;</code> with a name.<br/>
  I change the position of an element, it seems to be as it should, cool. But when  I save it in the database the order doesn't change! I am doing something wrong, that's a fact, but after hours spent debugging, my mind could really use a debugger for itself...</p>
  
  <p>Here is my jsf code (inside a p:datatable):</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;p:commandButton title="Move Down" 
                   ajax="false" 
                   image="down" 
                   action="#{playlistMBean.increasePosition(musicFile.URL)}"  
                   onclick="show_my_playlists?face-redirect=true"/&gt;
  </code></pre>
  
  <p><br/>
  The backing bean code:</p>
  
  
  
  <pre class="lang-java prettyprint-override"><code>@Named(value = "playlistMBean")
  @SessionScoped
  public class PlaylistMBean implements Serializable {
      @EJB
      private PlaylistManager playlistManager;
      private Playlist currentPlaylist;
      //...
  
      public void increasePosition(String musicURL) {
          currentPlaylist.increasePosition(musicURL);
          playlistManager.save(currentPlaylist);
      }
  
      //...
  }
  </code></pre>
  
  <p><br/>
  "currentPlaylist" is obviously a Playlist, so here's the code of the method in the entity bean "Playlist":</p>
  
  <pre class="lang-java prettyprint-override"><code>@Entity
  @NamedQueries(/*...*/)
  public class Playlist implements Serializable  {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private int id;
  
      @OneToMany(cascade= {CascadeType.REFRESH, CascadeType.MERGE},fetch= FetchType.EAGER)
      @OrderBy(/* ??????????? */
      private LinkedList&lt;MusicFile&gt; musicList;
  
      private String name;
  
      //...
  
      public void increasePosition(String url) {
          if (url != null) {
              MusicFile mf = getMusicFileByURL(url);
              int position = getPosition(url);
              if (position &lt; musicList.size() - 1) {
                  musicList.remove(position);
                  musicList.add(position + 1, mf);
              }
           }
       }
  </code></pre>
  
  <p><br/>
  And finally the code of the playlist manager which should save the reordered playlist in the database:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Stateless
  @LocalBean
  public class PlaylistManager {
      @PersistenceContext(unitName = "Doozer-ejbPU")
      private EntityManager em;
  
      public void save(Playlist playlist) {
          Playlist p = em.find(Playlist.class, playlist.getId());
          if (p != null) {
              em.merge(playlist);
          }
          else {
              em.persist(playlist);    
          }
      }
  
      //...
  }
  </code></pre>
  
  <p><br/>
  The given playlist in this last step is the good one (reordered). So my guess is that my problem is with the entity manager (I'm such a genius, I know...). </p>
  
  <p>Does anyone know why?</p>
  
  <p>May it comes from the cascade type? Due to my lack of knowledge about it I'm not sure which one should I put. I have tried <code>CascadeType.ALL</code> but then it raises exception when adding music files. <code>CascadeType.DETACH</code> was my first choice since I dont't want to delete the musics when deleting a playlist... But here again I really don't know for sure if I know what I'm talking about :(</p>
  
  <p>[Edit]: Thanks to Piotr Nowicki, my question has changed quite a lot: how can I define the <code>@OrderBy</code> annotation in order to sort the <code>LinkedList</code> according to its inner order? Is it even possible? The easy/ugly method would be to add a property <code>position</code> to the <code>MusicFile</code> entity but I'd rather not.</p>