составной первичный ключ с 1 внешним ключом 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. 

а также в классе Java «ComponentMarks» в «@IdClass (SubQuesCompIDs.class)» отображалось это сообщение об ошибке компиляции:

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

ConsolidatedMarks находится в пакете com.eta.entityBeans.

Обновить маркеры компонентов:

 @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************/
    }

обновленные SubQuesCompID:

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

теперь вторая ошибка, которая является ошибкой компиляции, не появляется.


person Nagaumadevi Palaparthy    schedule 03.08.2016    source источник


Ответы (1)


Это «производная идентичность».

Ваш @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.

person Brian Vosburgh    schedule 03.08.2016
comment
Спасибо за ответ. Я изменил свой код в соответствии с вашими предложениями, но получаю ту же ошибку «Неверная спецификация составного первичного ключа». Имена полей или свойств первичного ключа в классе первичного ключа [com.eta.entityBeans.SubQuesCompIDs] и в классе объектного компонента [class com.eta.entityBeans.ComponentMarks] должны совпадать, и их типы должны быть одинаковыми. Кроме того, убедитесь, что вы указали элементы id для соответствующих атрибутов в XML и/или @Id для соответствующих полей или свойств класса объектов. - person Nagaumadevi Palaparthy; 04.08.2016
comment
Я также добавляю свой код создания таблицы. CREATE TABLE component_marks ( submission_unique_ID VARCHAR(50) NOT NULL, component_ID VARCHAR(30) NOT NULL, question_ID VARCHAR(30) NOT NULL); - person Nagaumadevi Palaparthy; 04.08.2016
comment
Я не уверен, что это вызывает ошибку, но у вас не должно быть аннотации @Column(name="submission_unique_ID") в поле consolidatedMarks. - person Brian Vosburgh; 09.08.2016