0 Replies Latest reply on May 25, 2007 4:54 PM by J.R. Ector

    CRUD framework and subclasses

    J.R. Ector Newbie

      I used seam-gen to generate my entities from my db schema.
      I have one entity, Foo that has a subclass SubFoo.
      The problem I'm having is that I never directly use Foo, it's always a subclass so, I'm trying to figure out how to wire the super class Foo to a Collection of data that does not have a direct relationship with the subclass.

      Here's the code that will illustrate what I'm talking about:

      
      @Entity
      @Name("foo")
      @Table(name = "FOO")
      @Inheritance(strategy=InheritanceType.JOINED)
      public class Foo implements java.io.Serializable
      {
      
       private int projectNumber;
      
       protected Program program;
      
       protected List<Commitment> commitments = new ArrayList<Commitment>();
      
       ......
      
       @Id
      
       @Column(name = "PROJECT_NUMBER", unique = true, nullable = false, precision = 6, scale = 0)
       @NotNull
       public int getProjectNumber()
       {
       return this.projectNumber;
       }
      
       public void setProjectNumber(int projectNumber)
       {
       this.projectNumber = projectNumber;
       }
      
      @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "foo")
       public List<Commitment> getCommitments()
       {
       return this.commitments;
       }
      
       public void setCommitments(List<Commitment> commitments)
       {
       this.commitments = commitments;
       }
      
      }
      
      


      @Entity
      @Table(name = "SUB_FOO")
      @PrimaryKeyJoinColumn(name="PROJECT_NUMBER")
      public class SubFoo extends Foo implements java.io.Serializable {
      
       private int projectNumber;
      
      
       @Column(name = "PROJECT_NUMBER", insertable=false, updatable=false)
       @NotNull
       public int getProjectNumber() {
       return this.projectNumber;
       }
      
       public void setProjectNumber(int projectNumber) {
       this.projectNumber = projectNumber;
       }
      
       .....
      }



      The relationship in CommitmentHome is tied to the super class, Foo.

      
      package gov.dot.marad.persistence.ejb.model;
      
      import javax.persistence.EntityManager;
      
      import org.jboss.seam.annotations.Factory;
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.framework.EntityHome;
      
      @Name("commitmentHome")
      public class CommitmentHome extends EntityHome<Commitment> {
      
       @In(create = true)
       FooHome fooHome;
      
       @In(create = true)
       EntityManager entityManager;
      
       @In(value="#{fooProjectNumber}")
       private String fooProjectNumber;
      
       public EntityManager getEntityManager()
       {
       return entityManager;
       }
      
       @Factory("commitment")
       public Commitment initCommitment()
       {
       return getInstance();
       }
      
       public void setCommitmentCommitmentId(Integer id) {
       setId(id);
       }
      
       public Integer getCommitmentCommitmentId() {
       return (Integer) getId();
       }
      
       @Override
       protected Commitment createInstance() {
       Commitment commitment = new Commitment();
       commitment.setFoo(getEntityManager().find(Foo.class, new Integer(fooProjectNumber)));
       return commitment;
       }
      
       public void wire() {
       Foo foo = fooHome.getDefinedInstance();
       if (foo != null) {
       getInstance().setFoo(foo);
       }
       }
      
       public boolean isWired() {
       return true;
       }
      
       public Commitment getDefinedInstance() {
       return isIdDefined() ? getInstance() : null;
       }
      
       /**
       * @return the projectNumber
       */
       public String getFooProjectNumber()
       {
       return fooProjectNumber;
       }
      
       /**
       * @param projectNumber the projectNumber to set
       */
       public void setFooProjectNumber(String projectNumber)
       {
       this.fooProjectNumber = projectNumber;
       }
      
      }
      


      Commitment.java
      
       .....
      
      @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
       @JoinColumn(name = "PROJECT_NUMBER")
       public Foo getFoo() {
       return this.foo;
       }
      
       .....
      
      


      Is there a way to do this with EntityHome's or should I go back to Session beans?