2 Replies Latest reply on May 29, 2007 11:50 AM by Gavin King

    Question: hot to instantiate Home class correctly?

    Mihai Anescu Newbie

      Hi,

      We have a Seam generated app, we're making some modifications, so we have the XHome class to manage the entity X.

      Now, we have a hierarchy, A, B, C, all inherit from D, each has it's own home class (the use joined for inheritance type, each class has its own attributes).

      We have another entity E, which has a relation to D (but in fact under the hood can be any of A, B, C)

      Now, we have an action when if I save E, I have to create a new intance of D and save it. So for this, I made a factory to retrieve the correct Home for the D object (that is, retrieve AHome for a A entity and so on)

      I have tried 2 aproaches, here they are:
      1) In EHome, I use @In to declare AHome, BHome, CHome
      In the factory, I use a TreeMap to store key pairs, between the entity and the home class bound name (ex: "package1.A", "aHome")
      I have a method that retrieves from the context the Home based on the name.

      2) In the factory I store in the map keys to retrieve the home class name (ex: "package1.A", "package2.AHome")
      Then I instantiate it like this:

      Class.forName( homeName ).newInstance()


      In both cases the method returns the BasicHome (extended by AHome, BHome, CHome), and we call the persist method on it.

      The first method works, but since we have about 20 classes extending from D it is not very good.
      The second throws some NullPointerException inside some of the Seam framework classes:
      I managed to figure one out, if i put this code I get reed of the first exception:
      home.setEntityClass( package1.A.class );

      Here is the stack:
      java.lang.NullPointerException
       at org.jboss.seam.framework.Controller.debug(Controller.java:183)
       at org.jboss.seam.framework.Home.createdMessage(Home.java:52)
       at org.jboss.seam.framework.EntityHome.persist(EntityHome.java:51)


      So I'm asking what is the difference?

      I mean, what is different is that the solution 1) uses the @In, and 2) uses the normal way of creating a class instance. I looked into the Seam code and I have not found any special constructors, one that sets some attributes or does anything special. So, what did I miss???

      The line where the error is produced is this:

      log.debug(object, params);

      So log is null.
      Log is instantiated like this:
      @Logger Log log;


      So, apparently, with the @In annotation, the log gets instantiated, but on the second method I tried it doesn't.
      Does Seam provide some methods to instantiate the classes?