Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Error updating @OneToMany mapping
PostPosted: Mon May 07, 2012 8:14 pm 
Newbie

Joined: Mon May 07, 2012 8:04 pm
Posts: 1
Hi All,
I am stuck at a point where i am trying to update a @OneToMany mapping.

Problem:
I have 2 entities: Criteria and Tasks. A criteria can contain multiple tasks.

Code:
class Criteria {
@OneToMany(mappedBy = "criteria", cascade = { CascadeType.PERSIST,
         CascadeType.MERGE, CascadeType.REFRESH }, fetch = FetchType.LAZY)
   @Cascade({ org.hibernate.annotations.CascadeType.SAVE_UPDATE,
         org.hibernate.annotations.CascadeType.ALL,
         org.hibernate.annotations.CascadeType.MERGE,
         org.hibernate.annotations.CascadeType.PERSIST })
   private Set<Task> tasks;
}

class Task {
@ManyToOne
   @JoinColumn(name = "CRITERIA_ID", nullable=false)
   private Criteria criteria;
}


I am trying to update an existing Criteria with new set of tasks. This requires removing all existing tasks and adding new tasks.
Here is how i am doing:
Code:
               criteriaDao.persist(criteria);
      Set<Task> existingTasks=criteria.getTasks();
      if(existingTasks != null) {
         for (Task task : existingTasks) {
            task.setCriteria(null);
         }
         criteria.setTasks(tasks);
                        //more code which sets criteria for each task - this works if i try to save new criteria with new tasks
      }


Hibernate is throwing an exception:

Caused by: java.sql.BatchUpdateException: ORA-00001: unique constraint (CCA_DEV_5.TASK__UN) violated
This exception is thrown because it didn't remove the existing tasks.

If i only delete all the tasks, it works perfectly and removed all the tasks for the given criteria with below code:
Code:
                criteriaDao.persist(criteria);
      Set<Task> existingTasks=criteria.getTasks();
      if(existingTasks != null) {
         for (Task task : existingTasks) {
            task.setCriteria(null);
         }
      }


I tried all options of using merge after adding new tasks and more stuff but nothing seems to work for me. I am badly stuck here and definately missing something very basic in Hibernate.

Thanks in advance.!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.