11 Replies Latest reply: Jul 20, 2012 10:16 AM by gimbal2 RSS

    object.getId() returns null. (JPA)

    EdChouaffe
      Hi guys.

      I'm using a Class call Boom that is persisted using JPA. I have also written DAO classes that implement database access methods for that class.
      I also use transactions (EJB stateless session beans) to access the dao classes.

      One problem that I have been facing is that findById(Long id) returns a not null object, but when I call getId() on that object, it returns null. A little precision, I have declared the id with nullable = false within the Boom class.
      The most surprising is the fact that, when I call toString() on the return object, the ID is set while at the same time object.getId() returns null.

      For example:

      Boom object = this.getBoomDAO().findById(anId);
      System.out.println(object.getId());
      System.out.println(object.toString());


      Output:
      null
      1023

      Best regards.
        • 1. Re: object.getId() returns null. (JPA)
          gimbal2
          Post the entity class code. There is going to be an incredibly silly mistake in there.

          And btw: use \
           tags to post code and make it readable.                                                                                                                                                                                                                                                                                                            
          • 2. Re: object.getId() returns null. (JPA)
            EdChouaffe
            Here is the code:
            @Entity
            @Table ( name = "BOOM" )
            public class Boom implements Serializable
            {
                @Id
                @Column ( name = "ID", nullable = false )
                private Long               id;
                public final Long getId()
                {
                    return this.oid;
                }
                
                
                /**
                 * Sets the value of id [long].
                 * <p>
                 * The method is exclusivly called by Hibernate!!
                 * 
                 * @param id
                 *            the id to set
                 */
                private final void setId( final Long id )
                {
                    this.id = id;
                }
            
               some other getter and setters
            
            @Override
                public String toString()
                {
                    return "Customer [OID: " + this.getOid() + "]";
                }
            }
            • 3. Re: object.getId() returns null. (JPA)
              gimbal2
              Jebus...
               private Long               id;  // ID
                  public final Long getId()
                  {
                      return this.oid; // OID
                  }
              • 4. Re: object.getId() returns null. (JPA)
                EdChouaffe
                My mistake.
                @Entity
                @Table ( name = "BOOM" )
                public class Boom implements Serializable
                {
                    @Id
                    @Column ( name = "ID", nullable = false )
                    private Long               id;
                    public final Long getId()
                    {
                        return this.id;
                    }
                    
                    
                    /**
                     * Sets the value of id [long].
                     * <p>
                     * The method is exclusivly called by Hibernate!!
                     * 
                     * @param id
                     *            the id to set
                     */
                    private final void setId( final Long id )
                    {
                        this.id = id;
                    }
                 
                   some other getter and setters
                 
                @Override
                    public String toString()
                    {
                        return "Boom[ID: " + this.getId() + "]";
                    }
                }
                • 5. Re: object.getId() returns null. (JPA)
                  gimbal2
                  So in stead of just copy/pasting code you typed it? :s

                  Make the setter public.
                  • 6. Re: object.getId() returns null. (JPA)
                    EdChouaffe
                    Making the setter public doesn't solve the problem.
                    That is really awkward.
                    • 7. Re: object.getId() returns null. (JPA)
                      gimbal2
                      You may want to remove the final from the setter too; just use getters and setters as the IDE would generate them for you because those are according to the proper Java bean guidelines.

                      If that doesn't change anything... yeah I can't know what is wrong with code I can't see. I stick to my belief that this is going to be something very silly; problems that seem "weird" usually are something so insignificant that you wouldn't normally consider it.
                      • 8. Re: object.getId() returns null. (JPA)
                        EdChouaffe
                        thanks for the advise.
                        • 9. Re: object.getId() returns null. (JPA)
                          EdChouaffe
                          How is it even possible that the toString() methode returns the right value while a call to getID() returns a null?
                          I have som experience in Java, but I can 't explain that to me though.
                          • 10. Re: object.getId() returns null. (JPA)
                            EdChouaffe
                            I have found the source of the Problem.

                            Here is the scenario:
                            We have two classes Baam and Boom. Baam has a @ManyToOne assiociation to Boom and Boom a @OneToMany to Baam, both with FetchType.LAZY.

                            Any time I call
                            Baam ba = findById(id);
                            Boom bo = findById(id2);
                            bo doesn't have the id field filled. This appear even if the previous
                            bo
                            object is not the one
                            ba
                            points to.
                            When I call
                            ba.getBoom()
                            an Object is returned, but when I call
                            ba.getBoom().getId()
                            the returned Id is null.

                            Best regards.

                            Ed.
                            • 11. Re: object.getId() returns null. (JPA)
                              gimbal2
                              You say you found the source of the problem - but I see no information stating what you can do differently such that it works as you expect it to. You have just found more ways to get a null-ID.

                              The behavior is highly irregular in any case. No matter if you fetch lazy or eager and no matter if you fetch directly by ID or through a relational mapping in another entity, the properties of an entity must be filled.