1 Reply Latest reply on Mar 1, 2008 3:48 PM by Mathieu Le Cain

    Problem in mapping a many to many relation to a list

    Elenh Tsigka Newbie

      Hi,

      I have a problem with mapping a many to many relation to a list.

      I have a Many to Many relation in my database between tables baskets and products. This relation is implemented with a join table basket_products, which has two fields which correspond to the foreign keys to tables baskets and products. The join table need to have another one column "order", which defines the order of the products for the specific basket.

      In my mapping I have:

      
      @Entity
      @Table(name = "baskets", uniqueConstraints = {})
      public class Components implements java.io.Serializable {
      
      @ManyToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
       @JoinTable( name="baskets_products",
       joinColumns=@JoinColumn(name="b_id", referencedColumnName="pr_id"),
       inverseJoinColumns=@JoinColumn(name="pr_id", referencedColumnName="pr_id"))
      
       //This of course doesn't work!!
       //@OrderBy(value="order")
      
       public List<Products> getProductses() {
       return this.productses;
       }
      
       public void setProductses(
       List<Products> productses) {
       this.productses = productses;
       }
      
      
      
      @Entity
      @Table(name = "products", uniqueConstraints = { })
      public class Products implements java.io.Serializable {
      
      @ManyToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "productses")
       public Set<Baskets> getBasketses() {
       return this.basketses;
       }
      
       public void setBasketses(Set<Basket> basketses) {
       this.basketses = basketses;
       }
      
      
      




      In the EJB spec I read that :

      "The property or field name must correspond to that of a persistent property or field of the associated
      class."


      What to do if I want to use a field from the join table to do the ordering of the list??
      My problem is that the field "order", is not defined to the EJB container as a persistent field, so it cannot be used. But for my needs it is necessary that this field is included in the join table.

      I thought that I could use the secondary table annotation to map the join table to the Baskets class, as well, so that I can define the order field, but this solution seems to me as a really wrong one.


      Can anyone help me??

      Thanks,
      Elenh.