3 Replies Latest reply on Feb 3, 2006 11:32 AM by Ralf Alt

    multi column primary key

    Ralf Alt Newbie

      Hallo,

      I've an entity with 3 columns are primary key.
      Table userappconfig
      userid int (PK)
      appid int (PK)
      confentry varchar(200) (PK)
      confvalue varchar(200)

      The Entity-Bean:
      @Entity
      @Table(name="userappconfig")
      public class Userappconfig implements Serializable
      {
      private static final long serialVersionUID = -1865032716488077338L;
      private int applicationid = 0;
      private int userid = 0;
      private String myEntry = null;
      private String myValue = null;

      @Id(generate=GeneratorType.NONE)
      @Column(name="userid")
      public int getUserid()
      {
      return userid;
      }

      public void setUserid(int id)
      {
      userid = id;
      }

      @Id(generate=GeneratorType.NONE)
      @Column(name="appid")
      public int getApplicationid() {
      return applicationid;
      }

      public void setApplicationid(int id) {
      applicationid = id;
      }

      @Id(generate=GeneratorType.NONE)
      @Column(name="confentry")
      public String getEntry() {
      return myEntry;
      }

      public void setEntry(String entry) {
      myEntry = entry;
      }

      @Column(name="confvalue",nullable=true)
      public String getValue()
      {
      return myValue;
      }

      public void setValue(String mvalue)
      {
      myValue = mvalue;
      }
      }

      The session bean:
      @Stateless
      public class ApplicationUserConfig implements IApplicationUserConfig {
      @PersistenceContext(unitName="PersistCtx")
      private EntityManager entityMgr;

      public ArrayList getByUserIdApplicationId(int userid, int applicationid) throws StandardException {
      String query = "FROM " + Userappconfig.class.getName() + " a " +
      " WHERE a.userid = :_userId" +
      " AND a.applicationid = :_appId" ;
      Query ejbq = entityMgr.createQuery(query)
      .setParameter("_appId", applicationid)
      .setParameter("_userId", userid);
      return new ArrayList(ejbq.getResultList());
      }

      If I call the getByUserIdApplicationId() method, i get the error
      org.hibernate.QueryException: could not resolve property: userid of: Userappconfig [FROM Userappconfig a WHERE a.userid = :_userId]

      If I use only one column with the ID annotation it's all right.

      Any hints?

      Ralf

        • 1. Re: multi column primary key
          Cesar Izurieta Newbie

          You should use a PK Entity something like:

          @Entity
          @Table(name="userappconfig")
          public class Userappconfig implements Serializable
          {
          private static final long serialVersionUID = -1865032716488077338L;
          private String myValue = null;
          private UserappconfigPK id;
          
          @Id(generate=GeneratorType.NONE)
          public UserappconfigPK getId()
          {
          return id;
          }
          
          public void setId(UserappconfigPK id)
          {
          this.id = id;
          }
          
          @Column(name="confvalue",nullable=true)
          public String getValue()
          {
          return myValue;
          }
          
          public void setValue(String mvalue)
          {
          myValue = mvalue;
          }
          }


          @Embeddable
          public class UserappconfigPK implements Serializable
          {
          private static final long serialVersionUID = -1865032716488077338L;
          private int applicationid = 0;
          private int userid = 0;
          private String myEntry = null;
          
          @Column(name="userid")
          public int getUserid()
          {
          return userid;
          }
          
          public void setUserid(int id)
          {
          userid = id;
          }
          
          @Column(name="appid")
          public int getApplicationid() {
          return applicationid;
          }
          
          public void setApplicationid(int id) {
          applicationid = id;
          }
          
          @Column(name="confentry")
          public String getEntry() {
          return myEntry;
          }
          
          public void setEntry(String entry) {
          myEntry = entry;
          }
          


          • 2. Re: multi column primary key
            Bill Burke Master

            That is wrong. You should be using @EmbeddedId or @IdClass.

            • 3. Re: multi column primary key
              Ralf Alt Newbie

              Thanks a lot, it works.