具有1个外键jpa的复合主键,toplink

时间:2016-08-03 09:33:17

标签: jpa toplink

我需要在复合主键上创建一个具有一个外键的主键 ComponentMarks类:

@IdClass(SubQuesCompIDs.class)
@Entity
@Table(name="component_marks")

public class ComponentMarks {
     @Id
     private String component_ID;
     @Id
     private String question_ID;
     @Id
     @ManyToOne(targetEntity=ConsolidatedMarks.class)
     @JoinColumn(name="submission_unique_ID", referencedColumnName="submission_unique_ID")
     private ConsolidatedMarks consolidatedMarks;
//***********getters and setters***********
}

SubQuesCompIDs类:

public class SubQuesCompIDs implements Serializable{

    private ConsolidatedMarks consolidatedMarks;
    private String component_ID;
    private String question_ID;
/****************getters and setter************/
}

ConsolidatedMarks类:

 public class ConsolidatedMarks {
   @Id
   private String submission_unique_ID;
   /****************getters and setter************/
   }

错误:

   Internal Exception: javax.persistence.PersistenceException: Exception [TOPLINK-28018] (Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))): oracle.toplink.essentials.exceptions.EntityManagerSetupException
    Exception Description: predeploy for PersistenceUnit [RDEVAL_MySQL_DB] failed.
     Internal Exception: Exception [TOPLINK-7150] (Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))):       oracle.toplink.essentials.exceptions.ValidationException
      Exception Description: Invalid composite primary key specification. The names of the primary key fields or properties in the primary key class [com.eta.entityBeans.SubQuesCompIDs] and those of the entity bean class [class com.eta.entityBeans.ComponentMarks] must correspond and their types must be the same. Also, ensure that you have specified id elements for the corresponding attributes in XML and/or an @Id on the corresponding fields or properties of the entity class. 
   java.lang.Exception: 
    Exception Description: An exception was thrown while searching for persistence archives with ClassLoader: WebappClassLoader
     context: /ProjectName **
     delegate: false
   ----------> Parent Classloader:
  java.net.URLClassLoader@26b418

   Internal Exception: javax.persistence.PersistenceException: Exception [TOPLINK-28018] (Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))): oracle.toplink.essentials.exceptions.EntityManagerSetupException
    Exception Description: predeploy for PersistenceUnit [RDEVAL_MySQL_DB] failed.
     Internal Exception: Exception [TOPLINK-7150] (Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))): oracle.toplink.essentials.exceptions.ValidationException
      Exception Description: Invalid composite primary key specification. The names of the primary key fields or properties in the primary key class [com.eta.entityBeans.SubQuesCompIDs] and those of the entity bean class [class com.eta.entityBeans.ComponentMarks] must correspond and their types must be the same. Also, ensure that you have specified id elements for the corresponding attributes in XML and/or an @Id on the corresponding fields or properties of the entity class. 

以及“@IdClass(SubQuesCompIDs.class)”中的java类“ComponentMarks”中显示了此编译错误消息:

   The attribute matching the ID class attribute consolidatedMarks does not have the correct type com.eta.entityBeans.ConsolidatedMarks

ConsolidatedMarks来自com.eta.entityBeans包。

更新ComponentMarks:

 @Entity
 @Table(name="component_marks")
 @IdClass(SubQuesCompIDs.class)
 public class ComponentMarks {
    @Id
    private String component_ID;
    @Id
    private String question_ID;
    @Id
    @Column(name="submission_unique_ID")
    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="submission_unique_ID")
    private ConsolidatedMarks consolidatedMarks;
    /****************getters and setter************/
    }

更新了SubQuesCompIDs:

    public class SubQuesCompIDs implements Serializable{
    private String  consolidatedMarks;
    private String component_ID;
    private String question_ID;
    /****************getters and setter************/
    }

现在编译错误的第二个错误没有到来。

1 个答案:

答案 0 :(得分:0)

这是一个"派生的身份"。

您的@IdClass应如下所示:

public class SubQuesCompIDs implements Serializable{

    private String consolidatedMarks;
    private String component_ID;
    private String question_ID;
/****************getters and setter************/
}

请注意,与@ManyToOne consolidatedMarks字段对应的字段具有相同的字段名称,但其类型与ConsolidatedMarks主键字段的类型相匹配。

衍生身份在JPA 2.1规范第2.4.1节中讨论。