简体   繁体   中英

Persist an entity that have associated a list of entities that use @idclass

I have an Evaluation entity that has an associated list of EvaluationEvaluator . I need to explicitly create that entity because it required an extra column "STATUS". Before I continue evaluation. I do: evaluation.setEvaluationEvaluator(listEvaluator) where listEvaluator is a list of EvaluationEvaluator type. Then persist(evaluation) . When I run this, it does not throw any kind of exception. But in the database, it inserts in the Evaluation table, and not inserted into the EvaluationEvaluator table.

Below my Evaluation entity.

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

      //MORE FIELDS

      @OneToMany(mappedBy="evaluation")
      private List<EvaluationEvaluator> evaluators;

      //CONSTRUCTORS
      //GETTER AND SETTERS
}

This is my EvalutionEvaluator Entity:

@Entity
@Table(name= "EVALUATION_EVALUATOR")
@IdClass(EvaluationEvaluatorId.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;

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

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

    @NotNull
    private String status;

    //CONSTRUCTORS

    //GETTER AND SETTERS
}

This is my EvaluationEvaluatorId class

public class EvaluationEvaluatorId implements Serializable{
    private Long employeeID;
    private Long evaluationID;

    //CONSTRUCTOR
    //GETTER AND SETTERS
}

And finally, this is my EvaluationBean class

@Stateful
@Named
@LocalBean
@ConversationScoped
public class EvaluationBean {
   @PersistentContext(type= PersistenceContextType.EXTENDED)
   private EntityManager em;

   @Inject
   Conversation conversation;  

   private Evaluation evaluation;

   //IN MY WEBPAGE I IMPLEMENT PRIMEFACES PICKLIST AND IT REQUIRE DUALIST TO HANDLE
   private DualListModel<Employe> evaluators;

   private EvaluationEvaluator evaluationEvaluator;

   private List<EvaluationEvaluator> listEvaluators;

   @Inject
   private EmployeeList employeeList;

   //GETTER AND SETTERS

   public String begin(){
      if (conversation.isTransient()){
          converstaion.begin();
      }
      evaluationEvaluator = new EvaluationEvaluator();
      listEvaluators = new ArrayList<EvaluationEvaluator>();
      evaluation = new Evaluation();
      List<Employee> source = employeeList.findAll();
      target = new ArrayList<Employee>();
      evaluators = new DualListModel<Employee>(source, target);
      return "/evalution/evaluationAsig.xhtml"
   }
    public String save(){
        Iterator<Employee> iterator = evaluators.getTarget().iterator();
        while (iterator.hasNext()){
            EvaluationEvaluator ev = new EvaluationEvaluator();
            ev.setEmployee(iterator.next());
            listEvaluators.add(ev);
        }
        evalution.setEvaluationEvaluators(listEvaluators);
        if(evaluation.getId()==null){
           em.persist(evalution); 
        } else{
           em.merge(evalution);
        }
        if(!conversation.isTransient()){
           convesation.end();
        }
        return "/evalution/evaluationsAsig.xhtml"
    }
}

When I debug my application,apparently everything is correct, but I mentioned above, doesn't persist in EvaluationEvaluator table.

Your @OneToMany association is missing cascading configuration.

Add cascade = CascadeType.ALL or cascade = {CascadeType.PERSIST, CascadeType.MERGE} to the @OneToMany annotation. JPA assumes no cascading by default so you would need to persist each EvaluationEvaluator by yourself explicitely otherwise.


UPDATE

There is another thing wrong with the code - the Ids of EvaluationEvaluators are never assigned. You have a complex key made of two Long columns. Both are marked not insertable nor updatable which tells to JPA that the id is going to be somehow generated on database level and it should not care about it. There is however no sequence configured explicitely in your entity (although it is not necessarily required) and also from your comment:

I did what you recommended but it throws the following exception. "A different object with same identifier was already associated with the session"

I assume that this is not the case and both id column values default to null or zero and are same for all EvaluationEvaluators you are trying to persist. If you'd like the database to generate the id for you automatically use @GeneratedValue - Configure JPA to let PostgreSQL generate the primary key value - here you can find explanation how to do this (the database part is database dependent, this is for PostgreSQL). The most common use case however, is to configure the sequence but let hibernate pick the next value, instructions here - Hibernate sequence on oracle, @GeneratedValue(strategy = GenerationType.AUTO)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM