0 Replies Latest reply on Nov 20, 2006 3:03 PM by Dogan Koslu

    List and @OneToMany relationships in EJB3

    Dogan Koslu Newbie

      (I'm reposting this from the "Design of EJB 3.0" forum)

      With EJB 3.0 I am trying to persist classes that use Lists (ie, an order preserving collection) using a "JoinTable".

      Eg, I want a class A to contain a list of instances of class B:

      class A {
      List< B> list;

      class B {

      If I specify a @OneToMany annotation on "list", then the generated schema is 3 tables: A, B, and A_B where A_B has 2 columns: a (non-unique) foreign key reference to A, and a foreign key reference to B.

      This is fine for Collections of course but how is the list ordering preserved? I would expect a 3rd column called "position" or "index" to sort on.

      I tried it with JBoss and the packaged HSQL database using a @OneToMany annotation (with defaults), and my simple tests seemed to work, that is, list order was preserved.

      But I suspect the only reason that it worked is because JBoss is relying on the database to retrieve the rows in the same order it persists them, which is a bad assumption. I think the SQL standard says you can not rely on any particular ordering in a general select statement.

      At least I don't see how else it works.

      Is this the case?

      I saw a very old post (2004) by Bill Burke that said JBoss's EJB3 doesn't support Lists yet, but I also read in his new EJB 3.0 book that it does.

      I'm afraid to rely on Lists persisting/retrieving correctly.

      Perhaps there is a way I can manually specify a sort column in the join table through @JoinTable or some other annotations..?

      I can also resort to manually adding and maintaining a sortable "index" field to class B, and using @OrderBy, but that is not always practical (nor correct).

      thanks -dk