1 Reply Latest reply on Jan 26, 2006 10:50 PM by dwebster

    EJB-QL query on partial part of a compound key

    dwebster

      I cannot seem to get the syntax down here. I have a compound primary defined something like this:

      @Entity
      @Table(name = "Person")
      public class Person implements Serializable {
      PersonPk pk;
      .
      .
      .

      @EmbeddedId({
      @AttributeOverride(name=?ssn?),
      @AttributeOverride(name=?tshirtsize?)})
      public PersonPK getPk()
      { return pk; }




      @Embeddable
      public class PersonPK implements Serializable
      {
      private String ssn;
      private String tshirtsize;
      ...
      }

      But when I go to do a query on the leading field of the compound key, in this case to get all the tshirtsizes associated with a specific ssn in a query like:

      public @Stateless class PersonEJBBean implements PersonEJB
      {

      @PersistenceContext (unitName = "Personnel")
      protected EntityManager vEm;

      List getTshirtsizeBySsn (long ssn) {

      List ssnList = Em.createQuery("from Person p where p.ssn = :Ssn)
      .setParameter("Ssn", new Long(ssn))
      .getResultList();

      .
      .
      .
      I get an InvocationException the states:
      could not resolve property: ssn of Person [from Person p where ......]

      ?????

      I've tried everything I can think of to execute a query on the leading part of a compound key, but cannot seem to come up with an acceptable syntax.

      Any ideas?

        • 1. Re: EJB-QL query on partial part of a compound key
          dwebster

          answering my own question. Using an alternate annotation approach (@IdClass instead of @Embedded in the PK class and @EmbeddedId in the main class) works as you have to duplicate the compound primary key members in both the PK and the main Entity bean. The EM query can now recognize the key members. You loose the convenience of the PK mechanism, but it works.