2 Replies Latest reply on Feb 8, 2011 3:48 AM by samwun9988

    Annotation error with Many-to-Many.

    samwun9988

      Hello,

       

      There is a problem in my HIbernate/JPA annotation here.

      In Eclipse, it highlighted the "mappedBy = "productsForRelatedProductId"" and mappedBy = "productsForProductId" both has the following error:

       

      "In attribute 'productsForProductId', the "mapped by" attribute 'productsForRelatedProductId' also maps this attribute.  Every relationship must have an owner."

       

      and

       

      "In attribute 'productsForRelatedProductId', the "mapped by" attribute 'productsForProductId' also maps this attribute.  Every relationship must have an owner."

       

      Here is the table schema:

       

      CREATE TABLE IF NOT EXISTS

      related_product (

      product_id int unsigned not null,

      related_product_id int unsigned not null,

      PRIMARY KEY(product_id, related_product_id),

      FOREIGN KEY(product_id) REFERENCES product(product_id),

      FOREIGN KEY(related_product_id) REFERENCES product(product_id)

      ) TYPE=INNODB;

       

       

      Here is the code:

       

      @ManyToMany(fetch = FetchType.LAZY, mappedBy = "productsForRelatedProductId")

                public Set<Product> getProductsForProductId() {

                          return this.productsForProductId;

                }

       

                @ManyToMany(fetch = FetchType.LAZY, mappedBy = "productsForProductId")

                public Set<Product> getProductsForRelatedProductId() {

                          return this.productsForRelatedProductId;

                }

       

       

      Very appreciate for any suggestion and help.

       

      Thanks

      Sam