2 Replies Latest reply on Jan 6, 2005 6:00 AM by ekkelenkamp

    Howto use order by query on composite primary key fields?

    ekkelenkamp

      Hi,

      I would like to know how I can use an order by clause on the composite primary key part of an entity EJB.
      If I use an order by on a non-composite primary key field, it works just fine.

      I tried the following.

      My UserEJB entity bean has a composite primary key:

      /**
      * Return the composite primary key.
      *
      * @return com.finalist.entity.tuser.UserPK with the primary key.
      */
      @Id(generate = GeneratorType.NONE)
      @Dependent({
      @DependentAttribute(name = "UserId", column = {@Column(name = "USER_ID")} )
      ,@DependentAttribute(name = "Name", column = {@Column(name = "NAME")} )
      })
      public com.finalist.entity.tuser.UserPK getPrimaryKey() {
      return primaryKey;
      }

      I would like to use a query where i can use a order by on the primary key fields. I tried the following queries:

      from com.finalist.entity.tuser.UserEJB e order by e.primaryKey.userId, e.primaryKey.name
      from com.finalist.entity.tuser.UserEJB e order by e.userId, e.name

      It will result in the following exception:

      org.hibernate.QueryException: could not resolve property: primaryKey.userId of: com.finalist.entity.tuser.UserEJB [from com.finalist.entity.tuser.UserEJB e order by e.primaryKey.userId, e.primaryKey.name]

      Hope you can help.

      Rudie Ekkelenkamp.

        • 1. Re: Howto use order by query on composite primary key fields
          graysonpierce

          Have you tried creating @Transient properties that point to your UserPK ID/NAME and then doing the order by based on the name you gave to the transient properties?

          • 2. Re: Howto use order by query on composite primary key fields
            ekkelenkamp

            Hi,

            I already tried you suggested solution and unfortunately it didn't work.
            In my UserEJB the fields are defined as follows:


            /**
            * Returns the value of the name property.
            *
            */
            @javax.ejb.Transient public java.lang.String getName() {
            if (primaryKey == null) {
            return null;
            }
            return primaryKey.getName();
            }

            /**
            * Sets the value of the name property.
            *
            * @param name the value for the name property
            */
            @javax.ejb.Transient public void setName(java.lang.String name) {
            if (primaryKey == null) {
            primaryKey = new com.finalist.entity.tuser.UserPK();
            }
            primaryKey.setName(name);
            }