9 Replies Latest reply on Sep 17, 2010 8:17 AM by zottel

    Manually loading an EntityHome

    etaham

      How do I manually load the instance of an entity in an EntityHome object.  Can I just call setInstance(new Instance) on the entity home after I query for the entity OR must I use setId(id) function of the entityHome with the id of the entity I just queried and let the entityHome re-query for the instance.
      Are there any issues with calling setEntity manually?


        • 1. Re: Manually loading an EntityHome
          dan.j.allen

          Using setInstance() should work fine. The way this class works is that it only queries the database if the instance is null. Then it consults the id. When you use setId(), the instance is set to null internally.

          • 2. Re: Manually loading an EntityHome
            etaham

            Thank you.

            • 3. Re: Manually loading an EntityHome
              jkronegg

              When calling EntityHome.setInstance(E), the EntityHome id is not set. This leads to features such as:


              // I have an entity class "MyEntity" with a field "myField",
              // and a EntityHome class "MyEntityHome" which extends EntityHome
              
              // persist a new instance
              myEntityHome.getInstance().setMyField("test");
              myEntityHome.persist();
              // myEntityHome.id has been set by the persist to lets say 123
              
              // set a new instance taken from the database
              MyEntity myEntity = entityManager.find(MyEntity.class, 456);
              myEntityHome.setInstance(myEntity);
              // myEntityHome.id is not 456 but is still 123
              



              Changing the MyEntityHome id by calling setId will invalidate the entity by means of a dirty flag. But this does not work the other way round: setInstance does not set/invalidate the EntityHome id.


              Is it the expected behavior?

              • 4. Re: Manually loading an EntityHome
                nickarls

                Hmm. You could try doing a clearInstance() first, although in normal use I haven't seen any side effects of the id lagging (I haven't done finds/refreshes on mutating homes)

                • 5. Re: Manually loading an EntityHome
                  zottel

                  I'm currently having a problem that might come from the same source:


                  I have an entity that is already managed. I want to load this entity into an EntityHome, do stuff with it and then call update() on the EntityHome to save the changes:




                    public String supersede(TextModule textModule)
                    {
                      this.setInstance(textModule);
                      // ... do stuff
                      return this.update();
                    }
                  



                  textModule is the entity, of course.


                  This leads to a NullPointerException in update().


                  Is that because the EntityHome's id is null? Do I really have to something like setId(textModule.getId()); getInstance();?

                  • 6. Re: Manually loading an EntityHome
                    zottel

                    Uhm ... the actual reason seems to be that this.updatedMessage is null. Huh?!

                    • 7. Re: Manually loading an EntityHome
                      zottel

                      Hm, I'm completely at loss now. It's not the updateMessage.


                      Here's the important part of the stack trace:


                      Caused by: java.lang.NullPointerException
                           at org.jboss.seam.framework.Controller.debug(Controller.java:197)
                           at org.jboss.seam.framework.Home.updatedMessage(Home.java:54)
                           at org.jboss.seam.framework.EntityHome.update(EntityHome.java:65)
                           at de.sealsystems.ddb.session.TextModuleHome.supersede(TextModuleHome.java:316)



                      Line 54 of Home.java (according to My Link) is


                      debug("updated entity #0 #1", getEntityClass().getName(), getId());



                      But neither getEntityClass() nor its getName() nor getId() return null (I changed the code above to use setId() and getInstance()). I can't figure out what the problem might be. Any hints?


                      • 8. Re: Manually loading an EntityHome
                        lvdberg

                        Hi,


                        have you checked if the logger itself is not null ?


                        Leo

                        • 9. Re: Manually loading an EntityHome
                          zottel

                          Yeah, that's what I figured out, too, meanwhile. I had instantiated the TextModuleHome simply by new TextModuleHome(), so no injection took place (the logger is theoretically injected in the debug class, but as I instantiated the EntityHome object during a response phase, nothing was injected before I tried to use the update() function).


                          I'm doing what I want to achieve differently now, so now other problems arise. X-)