3 Replies Latest reply on Mar 18, 2009 2:39 AM by srikondoji

    ManyToMany mapping errors.Please help


      Hi,
      I am using seam-gen to get jump started with my existing relational database.
      Ater secuting seam setup, generate-project.... i executed 'seam generate-model'
      After resolving all issues, i still have 2 errors that i am unable to resolve.

      Error message:
      compile:


      "`    javac Compiling 12 source files to C:\jboss\workspace\parties\exploded-arc
      hives\parties.ear\parties.jar
          javac C:\jboss\workspace\parties\src\main\org\parties\model\user\Usergroup
      s.java:99: cannot find symbol
          javac symbol  : method JoinColumns()
          javac location: @interface javax.persistence.JoinTable
          javac             JoinColumns={@JoinColumn(name="groupid")},
          javac                 ^
          javac C:\jboss\workspace\parties\src\main\org\parties\model\user\Usergroup
      s.java:99: annotation not valid for a value of type <any>
          javac             JoinColumns={@JoinColumn(name="groupid")},
          javac                                               ^
          javac 2 errors

      `

      Explanation of tables and entity classes
      I have two tables
      usergroups and usercontacts. There is a ManyToMany relation ship between these two tables, so i have an Associate table as well.

      Relational schema
      CREATE TABLE usergroups
      (
              id BIGINT NOT NULL,
              pid bigint NOT NULL,
              groupname TINYTEXT NOT NULL,
              groupdescription TINYTEXT NOT NULL,
              FOREIGN KEY (pid) REFERENCES userprofile(id),
              PRIMARY KEY (id)
      );

      CREATE TABLE usercontacts
      (
              id BIGINT NOT NULL,
              pid bigint NOT NULL,
              contactname TINYTEXT NOT NULL,
              contactmail TINYTEXT NOT NULL,
              contactfax TINYTEXT NOT NULL,
              PRIMARY KEY (id)
      );

      CREATE TABLE usergroupcontacts
      (
              groupid BIGINT NOT NULL,
              contactid BIGINT NOT NULL,
              FOREIGN KEY (groupid) REFERENCES usergroups(id),
              FOREIGN KEY (contactid) REFERENCES usercontacts(id),
              PRIMARY KEY (groupid, contactid)
      );

      code snippets of Enity classes

      import java.util.HashSet;
      import java.util.Set;
      import javax.persistence.CascadeType;
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.FetchType;
      import javax.persistence.Id;
      import javax.persistence.ManyToMany;
      import javax.persistence.JoinTable;
      import javax.persistence.JoinColumns;
      import javax.persistence.JoinColumn;
      import javax.persistence.ManyToOne;
      import javax.persistence.OneToMany;
      import javax.persistence.Table;
      import org.hibernate.validator.NotNull;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      /**
      * Usergroups generated by hbm2java
      */
      @Entity
      @Table(name = "usergroups")
      public class Usergroups implements java.io.Serializable {

      --several getter and setter methods followed by---

      @ManyToMany
              @JoinTable(name="usergroupcontacts",
                      JoinColumns={@JoinColumn(name="groupid")},
                      inverseJoinColumns={@JoinColumn(name="contactid")})
              public Set<Usercontacts> getUsercontactses()
              {
                      return this.usercontactses;
              }
              public void setUsercontactses(Set<Usercontacts> usercontactses)
              {
                      this.usercontactses = usercontactses;
              }

      }

      Similarly
      /**
      * Usercontacts generated by hbm2java
      */
      @Entity
      @Table(name = "usercontacts")
      public class Usercontacts implements java.io.Serializable {


      --several getter and setter methods followed by---

              @ManyToMany(mappedBy = "usercontactses")
              public Set<Usergroups> getUsergroupses() {
                      return this.usergroupses;
              }
              public void setUsergroupses(Set<Usergroups> usergroupses)
              {
                      this.usergroupses = usergroupses;
              }

      }
      "



      Question: What did i do wrong?
      I thought i should not create entity representation for associate table. However, i do i have a composite primary key and iam not sure i this breaks the default behavior.

      I would really appreciate, i you can tell me what i should do to overcome the errors.
      I am using jboss-seam-2.1.1.GA





      `
        • 1. Re: ManyToMany mapping errors.Please help
          swd847

          JoinColumns should be joinColumns. There is no capital on the J

          • 2. Re: ManyToMany mapping errors.Please help

            Hi Stuart
            Is that all i need to do? Surprising that a case sensitivity issue blocked me for so long. I will try this at home today and will let you know the result.


            In any case, having a compisite primary key shouldn't be any problem. right?
            The seam in action book samples didn't show any type of Composite primary key on associate tables.
            Thanks again for your response
            --sri



            Stuart Douglas wrote on Mar 15, 2009 23:14:


            JoinColumns should be joinColumns. There is no capital on the J


            Click HELP for text formatting instructions. Then edit this text and check the preview.

            • 3. Re: ManyToMany mapping errors.Please help
              <blockquote>
              _Stuart Douglas wrote on Mar 15, 2009 23:14:_<br/>

              JoinColumns should be joinColumns. There is no capital on the J
              </blockquote>

              Thanks Stuart, that took care of the error. After generating UI successfully, now i am getting validation error.

              Here is the code snippet on length of gender field.
              a) xhtml code snippet
                          <s:decorate id="genderField" template="layout/edit.xhtml">
                              <ui:define name="label">Gender</ui:define>
                              <h:inputText id="gender"
                                        value="#{userprofileHome.instance.gender}">
                                  <a:support event="onblur" reRender="genderField" bypassUpdates="true" ajaxSingle="true"/>
                              </h:inputText>
                          </s:decorate>

              b) Entity class code snippet:
                      @Enumerated(EnumType.STRING)
                      @Column(name = "gender", length = 6)
                      @Length(max = 6)
                      public GenderType getGender()
                      {
                              return this.gender;
                      }
                      public void setGender(GenderType gender)
                      {
                              this.gender = gender;
                      }

              c) anum snippet
              public enum GenderType
              {
                      male,
                      female
              }

              d) schema code snippet
              CREATE TABLE userprofile
              (
                      id BIGINT NOT NULL,    
                      name TINYTEXT NOT NULL,
                      gender ENUM ('male','female') NULL,
                      lastupdated DATETIME NULL,
                      PRIMARY KEY (id)
              );

              Even though iam entering the gender field with values 'male' or 'female', iam receiving length validation error 'length should be between 0 and 6 characters.
              Database is using utf8 encoding.