0 Replies Latest reply on Nov 12, 2006 6:39 PM by John Schneider

    Mapping Exception for subclass with inherited composite key

    John Schneider Newbie

      I'm getting the error below for what I believe to be a valid mapping. Am I doing something wrong? Any ideas?

      org.hibernate.MappingException: Foreign key (FKFA49A91FAB488FB6:billing_document_lines [line_number])) must have same number of columns as the referenced primary key (document_lines [line_number,document_id])

      Here's the code:

      BillingDocument.java

      package document;
      
      import javax.persistence.Entity;
      import javax.persistence.Table;
      
      @Entity
      @Table(name = "billing_documents")
      public class BillingDocument extends Document {
      
       private static final long serialVersionUID = 1L;
      
      }
      



      BillingDocumentLine.java
      package document;
      
      import javax.persistence.Entity;
      import javax.persistence.Table;
      
      @Entity
      @Table(name = "billing_document_lines")
      public class BillingDocumentLine extends DocumentLine {
      
       private static final long serialVersionUID = 1L;
      
       public BillingDocumentLine(BillingDocument document, short lineNumber) {
       super(document, lineNumber);
       }
      
       BillingDocumentLine() {
       }
      
      }
      



      Document.java
      package document;
      
      import java.io.Serializable;
      
      import javax.persistence.Column;
      import javax.persistence.Id;
      import javax.persistence.Entity;
      import javax.persistence.Inheritance;
      import javax.persistence.InheritanceType;
      import javax.persistence.Table;
      
      @Entity
      @Table(name = "documents")
      @Inheritance(strategy = InheritanceType.JOINED)
      public abstract class Document implements Serializable {
      
       private static final long serialVersionUID = 1L;
      
       protected String documentID;
      
       protected Document() {
       }
      
       protected Document(String documentID) {
       setDocumentID(documentID);
       }
      
       @Override
       public int hashCode() {
       final int PRIME = 31;
       int result = 1;
       result = PRIME * result
       + ((documentID == null) ? 0 : documentID.hashCode());
       return result;
       }
      
       @Override
       public boolean equals(Object object) {
       if (object instanceof Document) {
       final Document document = (Document) object;
       if (documentID != null) {
       return documentID.equals(document.documentID);
       }
       }
       return false;
       }
      
       @Override
       public String toString() {
       return documentID;
       }
      
       @Id
       @Column(name = "document_id")
       public String getDocumentID() {
       return documentID;
       }
      
       protected void setDocumentID(String documentID) {
       this.documentID = documentID;
       }
      }
      



      DocumentLine.java

      package document;
      
      import java.io.Serializable;
      
      import javax.persistence.EmbeddedId;
      import javax.persistence.Entity;
      import javax.persistence.Inheritance;
      import javax.persistence.InheritanceType;
      import javax.persistence.Table;
      
      @Entity
      @Table(name = "document_lines")
      @Inheritance(strategy = InheritanceType.JOINED)
      public abstract class DocumentLine implements Serializable {
      
       private static final long serialVersionUID = 1L;
      
       protected DocumentLinePK primaryKey;
      
       protected DocumentLine(Document document, short lineNumber) {
       setPrimaryKey(new DocumentLinePK(document, lineNumber));
       }
      
       protected DocumentLine() {
       }
      
       @Override
       public int hashCode() {
       final int PRIME = 31;
       int result = 1;
       result = PRIME * result
       + ((primaryKey == null) ? 0 : primaryKey.hashCode());
       return result;
       }
      
       @Override
       public boolean equals(Object object) {
       if (object instanceof DocumentLine) {
       final DocumentLine documentLine = (DocumentLine) object;
       if (primaryKey != null) {
       return primaryKey.equals(documentLine.primaryKey);
       }
       }
       return false;
       }
      
       @Override
       public String toString() {
       return primaryKey.toString();
       }
      
       @EmbeddedId
       public DocumentLinePK getPrimaryKey() {
       return primaryKey;
       }
      
       protected void setPrimaryKey(DocumentLinePK primaryKey) {
       this.primaryKey = primaryKey;
       }
      }
      



      DocumentLinePK.java
      package document;
      
      import java.io.Serializable;
      
      import javax.persistence.Column;
      import javax.persistence.Embeddable;
      import javax.persistence.FetchType;
      import javax.persistence.JoinColumn;
      import javax.persistence.ManyToOne;
      
      @Embeddable
      public class DocumentLinePK implements Serializable {
      
       private static final long serialVersionUID = 1L;
      
       private Document document;
      
       private short lineNumber;
      
       public DocumentLinePK(Document document, short lineNumber) {
       setDocument(document);
       setLineNumber(lineNumber);
       }
      
       DocumentLinePK() {
       }
      
       @Override
       public int hashCode() {
       final int PRIME = 31;
       int result = 1;
       result = PRIME * result
       + ((document == null) ? 0 : document.hashCode());
       result = PRIME * result + lineNumber;
       return result;
       }
      
       @Override
       public boolean equals(Object object) {
       if (object == null) {
       return false;
       }
       if (getClass() == object.getClass()) {
       final DocumentLinePK primaryKey = (DocumentLinePK) object;
       if (document != null && lineNumber != 0) {
       return document.equals(primaryKey.document)
       && lineNumber == primaryKey.lineNumber;
       }
       }
       return false;
       }
      
       @Override
       public String toString() {
       return document.toString() + ":" + lineNumber;
       }
      
       @ManyToOne(optional = false, fetch = FetchType.LAZY)
       @JoinColumn(name = "document_id")
       public Document getDocument() {
       return document;
       }
      
       @Column(name = "line_number")
       public short getLineNumber() {
       return lineNumber;
       }
      
       protected void setDocument(Document document) {
       this.document = document;
       }
      
       protected void setLineNumber(short lineNumber) {
       this.lineNumber = lineNumber;
       }
      }