3 Replies Latest reply on Jun 22, 2006 2:03 PM by hilwi

    Loosing PK on query

    hilwi

      Hello!

      I'm having a strange problem with my EJBs. I have created a base-class for my entity-ejbs that contains the primary-key-field and some other fields:

      ...
      @MappedSuperclass
      public class JbsObject {
      
       private long id;
      
       @Id
       public long getId() {
       return id;
       }
      
       public void setId(long id) {
       this.id = id;
       }
      
      ...
      }
      


      Now I have an entity-ejb named "Address" that defines some more fields like "name", ...

      @Entity
      public class Address extends JbsObject implements Serializable {
      ...
      }
      


      The tables are created correctly and I can add new entities without any problems.

      Now I want to fetch a list of addresses with a simple query in my stateless session-bean "AddressesBean":

      @Stateless
      public class AddressesBean implements Addresses {
      
       @PersistenceContext(unitName="testapp")
       private EntityManager manager;
      
       public List getAllAddresses() {
       Query q = manager.createQuery("from Address");
       return q.getResultList();
       }
      ...
      }
      


      When I invoke this method from my client-application I can retrieve all addresses with their data except their id. The id of every instance is 0. I have printed out the ids within the getAllAddresses-function - all is ok there. Only on the client I have no ids.

      What am I doing wrong?

      I'm using JBoss 4.04GA with Linux.

      Achim



        • 1. Re: Loosing PK on query
          hilwi

          I forgot to post my client-code to retrieve the addresses:

           protected void GetAddresses() {
           try {
           Context context = ClientTools.getInitialContext();
           Object ref = context.lookup("AddressesBean/remote");
           Addresses addresses = (Addresses)PortableRemoteObject.narrow(ref,Addresses.class);
           setLstAddresses(addresses.getAllAddresses());
          
           for (int i=0; i<lstAddresses.size(); i++) {
           System.out.println(((Address)lstAddresses.get(i)).getName1()+String.valueOf(((Address)lstAddresses.get(i)).getId()));
           }
          
           } catch (Exception e) {
           e.printStackTrace();
           }
           }
          


          As I manetioned in my previous post, the function getName1() returns the currect names of the addresses but the getId() always returns 0.

          • 2. Re: Loosing PK on query
            abl

            try:

            ...
            @MappedSuperclass
            public class JbsObject implements Serializable {

            private long id;

            ...

            • 3. Re: Loosing PK on query
              hilwi

              This works!! Thank you!