1

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.

Sunny Patel
  • 7,830
  • 2
  • 31
  • 46

1 Answers1

0

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)

Community
  • 1
  • 1
makasprzak
  • 5,082
  • 3
  • 30
  • 49
  • thanks, for the quickly answer. Then make no sense to do this, right ? listEvaluators.add(ev);I just do: while (iterator.hasNext()){ EvaluationEvaluator ev = new EvaluationEvaluator(); ev.setEmployee(iterator.next()); em.persist(ev). } – user3653454 May 19 '14 at 20:05
  • Adding **EvaluationEvaluators** to **Evaluation** is creating the actual association, you still need to do this. But the cascading just means all the entities are persisted together when you persist the root. – makasprzak May 19 '14 at 20:19
  • I did what you recommended but it throws the following exception. "A different object with same identifier was already associated with the session" I understand it is a problem with the session related to memory but I can not find it, I try to use the method clear () before persisting the object but did not work. Do not understand what I'm doing wrong.Thanks in advance – user3653454 May 20 '14 at 12:13
  • This exception means that there is already an entity with same id registered in entity manager. You can think of it as you are trying to store two rows with same ids. I'll update the answer with more details... – makasprzak May 20 '14 at 19:13