无法持久化和具有复合键的实体

时间:2014-05-26 20:19:32

标签: java hibernate jpa ejb conversation-scope

我试图保留一个具有合成主键的实体。 这是我的员工实体

  

public class Employee implements Serializable{
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    @NotNull
    @Size(max=50)
    private String name;

    @ManyToOne
    private Department department

    @ManyToOne
    private Post post;

    @OneToMany(mappedBy="employee")
    private List<EvaluationEvaluator> evaluationEvaluator;

   //SOME GETTER AND SETTERS

这是我的评估类:

@Entity
public class Evaluation implements Serializable {
@Id
@GeneratedValue
private Long id;  

@NotNull
private Date dateAsigned;

@OneToMany(mappedBy="evaluation" , cascade = {CascadeType.PERSIST, CascadeType.MERGE})
private List<EvaluationEvaluator> evaluationEvaluator;

@NotNull 
private String obs;

//HERE THE GETTER AND SETTERS

这是我的EvaluationEvaluator类,这是使用合成密钥的类。

 @Entity
@Table(name="EVALUATION_EVALUATOR")
@IdClass(EvaluationEvaluatorsId.class)
public class EvaluationEvaluator implements Serializable {

@Id
@Column(name="EMPLOYEE_ID", insertable= false, updatable = false)
private Long employeeID;

@Id
@Column(name="EVALUATION_ID" ,insertable= false , updatable = false)
private Long evaluationID;

//@NotNull
@ManyToOne
@JoinColumn(name="EMPLOYEE_ID")
private Employee employee; 

@ManyToOne
@JoinColumn(name="EVALUATION_ID")   
private Evaluation evaluation;

 @NotNull
 private String status;

 //SOME GETTER AND SETTERS HERE

这是EvaluationEvaluatorsId类:

public class EvaluationEvaluatorsId implements Serializable {

private Long employeeID;

private Long evaluationID;

这是我保存的方法

public String save() {     
    Iterator<Employee> iterator = evaluators.getTarget().iterator();
    while(iterator.hasNext()){
       EvaluationEvaluator ev = new EvaluationEvaluator();
       ev.setEmployee(iterator.next());
       ev.setEvaluation(evaluation);
       ev.setStatus("COMPLETED");
       evaluation.getEvaluationEvaluators().add(ev);
    }
    if (evaluation.getId() == null) {
        em.persist(evaluation);
    } else {
        em.merge(evaluation);
    }
    if (!conversation.isTransient()) {
        conversation.end();
    }
    return "/evaluation/evaluationsAsig.xhtml";

当我尝试坚持时,我得到以下异常。

这是堆栈跟踪:

    07:52:27,368 INFO  [stdout] (http-/127.0.0.1:8080-5)     insert 
07:52:27,369 INFO  [stdout] (http-/127.0.0.1:8080-5)     into
07:52:27,369 INFO  [stdout] (http-/127.0.0.1:8080-5)         Evaluacion
07:52:27,369 INFO  [stdout] (http-/127.0.0.1:8080-5)         (evaluado_id, 
    fechaAsignacion, observacion, id) 
07:52:27,369 INFO  [stdout] (http-/127.0.0.1:8080-5)     values
07:52:27,369 INFO  [stdout] (http-/127.0.0.1:8080-5)         (?, ?, ?, ?)
07:52:27,397 INFO  [stdout] (http-/127.0.0.1:8080-5) Hibernate: 
07:52:27,397 INFO  [stdout] (http-/127.0.0.1:8080-5)     insert 
07:52:27,397 INFO  [stdout] (http-/127.0.0.1:8080-5)     into
07:52:27,398 INFO  [stdout] (http-/127.0.0.1:8080-5)         EVALUACION_EVALUADORES
07:52:27,398 INFO  [stdout] (http-/127.0.0.1:8080-5)         (EMPLEADO_ID, 
    EVALUACION_ID, status) 
07:52:27,398 INFO  [stdout] (http-/127.0.0.1:8080-5)     values
07:52:27,398 INFO  [stdout] (http-/127.0.0.1:8080-5)         (?, ?, ?)
07:52:27,399 INFO  [org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl] 
    (http-/127.0.0.1:8080-5) HHH000010: On release of batch it still contained JDBC 
    statements
07:52:27,412 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] 
    (http-/127.0.0.1:8080-5) SQL Error: 0, SQLState: S1009
07:52:27,413 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] 
    (http-/127.0.0.1:8080-5) Parameter index out of range (4 > number of parameters, which 
     is 3).

   Caused by: org.hibernate.exception.GenericJDBCException: could not insert: 
   [py.com.test.bpartnerevaluator.model.evaluacion.EvaluacionEvaluadores]
at    org.hibernate.exception.internal.StandardSQLExceptionConverter.

    convert(StandardSQLExceptionConverter.java:54) 
    [hibernate-core-4.2.7.SP1-redhat-3.jar:4.2.7.SP1-redhat-3]
    at 
    org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125) 
    [hibernate-core-4.2.7.SP1-redhat-3.jar:4.2.7.SP1-redhat-3]
    at 
    org.hibernate.persister.entity.AbstractEntityPersister.insert
   (AbstractEntityPersister.java:3101) [hibernate-core-4.2.7.SP1-redhat-3.jar:4.2.7.SP1- 
    redhat-3]
    at 
   org.hibernate.persister.entity.AbstractEntityPersister.
   insert(AbstractEntityPersister.java:3523) [hibernate-core-4.2.7.SP1-redhat-
    3.jar:4.2.7.SP1-redhat-3]
    at 
   org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:88) 
   [hibernate-core-4.2.7.SP1-redhat-3.jar:4.2.7.SP1-redhat-3]
    at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:393) 
   [hibernate-core-4.2.7.SP1-redhat-3.jar:4.2.7.SP1-redhat-3]
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:385) 
   [hibernate-core-4.2.7.SP1-redhat-3.jar:4.2.7.SP1-redhat-3]
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:301) 
   [hibernate-core-4.2.7.SP1-redhat-3.jar:4.2.7.SP1-redhat-3]
    at 
  rg.hibernate.event.internal.AbstractFlushingEventListener.
  performExecutions(AbstractFlushingEventListener.java:339) [hibernate-core-4.2.7.SP1-
  redhat-3.jar:4.2.7.SP1-redhat-3]
    at 
  org.hibernate.event.internal.DefaultFlushEventListener.onFlush
  (DefaultFlushEventListener.java:52) [hibernate-core-4.2.7.SP1-redhat-3.jar:4.2.7.SP1-
  redhat-3]
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1240) [hibernate-
 core-4.2.7.SP1-redhat-3.jar:4.2.7.SP1-redhat-3]
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:404) 
 [hibernate-core-4.2.7.SP1-redhat-3.jar:4.2.7.SP1-redhat-3]
    at 
  org.hibernate.engine.transaction.synchronization.internal.
  SynchronizationCallbackCoordinatorImpl.beforeCompletion
  (SynchronizationCallbackCoordinatorImpl.java:113) [hibernate-core-4.2.7.SP1-redhat-
  3.jar:4.2.7.SP1-redhat-3]
    ... 81 more

0 个答案:

没有答案