persistence@glassfish.java.net

Re: mappedBy problem in subclasses

From: Guy Pelletier <guy.pelletier_at_oracle.com>
Date: Tue, 22 May 2007 08:39:38 -0400

WG: mappedBy problem in subclassesYou should use mappedBy="classOwner" since you are using property access.

Cheers,
Guy
  ----- Original Message -----
  From: Juerg.Treichler_at_gs-vbs.admin.ch
  To: persistence_at_glassfish.dev.java.net
  Sent: Tuesday, May 22, 2007 8:26 AM
  Subject: WG: mappedBy problem in subclasses





  I created three entity classes:

  An abstract class Type
  @Entity
  public abstract class Type implements Serializable {
      private static final long serialVersionUID = 1L;
      private Long id;
      private java.sql.Timestamp Version;

      public Type() {
      }

      public void setId(Long id) {
          this.id = id;
      }

      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      public Long getId() {
          return id;
      }

      @Version
      public Timestamp getVersion() {
          return Version;
      }

      public void setVersion(Timestamp Version) {
          this.Version = Version;
      }



  }

  A subclass "ClassDef"

  @Entity
  public class ClassDef extends Type implements Serializable {
    private static final long serialVersionUID = 1L;
    private String Name;
    private boolean Abstract;
    java.util.List<Attribute> Attributes;
    
    public ClassDef() {
      Attributes = new java.util.ArrayList<Attribute>();

      
    }
    
    @Column(nullable=false)
    public String getName() {
      return Name;
    }
    
    public void setName(String Name) {
      this.Name = Name;
    }
    
    public boolean isAbstract() {
      return Abstract;
    }
    
    public void setAbstract(boolean Abstract) {
      this.Abstract = Abstract;
    }
    
    
    @OneToMany(cascade=javax.persistence.CascadeType.ALL,mappedBy="ClassOwner")
    public List<Attribute> getAttributes() {
      return Attributes;
    }
    
    public void setAttributes(List<Attribute> Attributes) {
      this.Attributes = Attributes;
    }
    
    public void addAttribute(Attribute a) {
      Attributes.add(a);
      a.setClassOwner(this);
    }
    
    public Attribute newAttribute() {
      Attribute a = new Attribute();
      this.addAttribute(a);
      return a;
    }
    
    public Attribute getAttribute(long iID) {
      for (Attribute a:this.getAttributes()) {
        if (a.getId() == iID)
          return a;
      }
      return null;
    }
    
    public void removeAttribute(Attribute a) {
      Attributes.remove(a);
      a.setClassOwner(null);
    }
      
  }

  And a third class "Attribute".

  @Entity
  public class Attribute implements Serializable {
    private static final long serialVersionUID = 1L;
    private Long id;
    private String Name;
    private ClassDef ClassOwner;
    private Type Type;
    
    
    public Attribute() {
    }
    
    public void setId(Long id) {
      this.id = id;
    }
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
      return id;
    }
    
    @Column(nullable=false)
    public String getName() {
      return Name;
    }
    
    public void setName(String Name) {
      this.Name = Name;
    }
    
    @ManyToOne()
    @JoinColumn(nullable=false)
    public ClassDef getClassOwner() {
      return ClassOwner;
    }
    
    public void setClassOwner(ClassDef ClassOwner) {
      this.ClassOwner = ClassOwner;
    }
    
    @ManyToOne
    public Type getType() {
      return Type;
    }
    
    public void setType(Type Type) {
      if (Type == this.getClassOwner()) {
        //TODO Exception if an attribute is of the same type as his owner
        return;
      }
      this.Type = Type;
    }



     
  }

  The error message i get is:

  Internal Exception: Exception [TOPLINK-7154] (Oracle TopLink Essentials - 2.0 (Build b47-beta3 (05/16/2007))): oracle.toplink.essentials.exceptions.ValidationException

  Exception Description: The attribute [attributes] in entity class [class org.JTSoft.POEM.BaseLib.ClassDef] has a mappedBy value of [ClassOwner] which does not exist in its owning entity class [class org.JTSoft.POEM.BaseLib.Attribute]. If the owning entity class is a @MappedSuperclass, this is invalid, and your attribute should reference the correct subclass.

          at oracle.toplink.essentials.exceptions.EntityManagerSetupException.predeployFailed(EntityManagerSetupException.java:229)

  I can't fix the problem. Where is the missing part of the puzzle?

  Kind regards

  Jürg



  -----------------------------------------------------------------------------------------------
  dipl. Informatik-Ing. ETH Jürg Treichler
  IT-Architekt

  Eidgenössisches Departement für Verteidigung,
  Bevölkerungsschutz und Sport VBS
  Generalsekretariat VBS
  Informatik VBS
  Maulbeerstrasse 9; 3003 Bern
  +41 (31) 324 73 42 (Tel.)
  +41 (31) 324 88 99 (Fax)
  juerg.treichler_at_gs-vbs.admin.ch
  www.vbs.admin.ch
  -----------------------------------------------------------------------------------------------