-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 posts ] 
Author Message
 Post subject: Merge deached collection
PostPosted: Wed Aug 20, 2008 4:43 pm 
Beginner
Beginner

Joined: Sun May 07, 2006 2:44 pm
Posts: 33
Hey,

I have class A, which contain collection of B (cascade=all)

in detach mode i remove elmement from the collection and then call to
entitymanager.merge(a)

The element is not removed.

I think there is a JIRA about it, but it was supposed to be fixed.

Thank you


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 20, 2008 5:53 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
hello,
do entities B have a link back to A? (in other words, is the link bidirectional) ?
make sure you update both ends of the bidirectional link correctly before merging.

some code could help, try reproducing the problem with a minimal test.

did you find the Jira?
alo you don't mention any hibernate versions.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Details
PostPosted: Wed Aug 20, 2008 9:55 pm 
Beginner
Beginner

Joined: Sun May 07, 2006 2:44 pm
Posts: 33
unidirectional

simple onetomany with cascade. version 3.2.2


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 21, 2008 3:23 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
thanks,
did you find the JIRA too?

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: not didnt find yet
PostPosted: Thu Aug 21, 2008 3:25 am 
Beginner
Beginner

Joined: Sun May 07, 2006 2:44 pm
Posts: 33
but i am 90% sure that i saw something like this.

do you know that it work for remove?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 21, 2008 5:55 am 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
We also have an appplication based on detached entities and we use the same constructs. When our client removes some entity from a collection and then sends the owner entity of the collection back to the server, then Hibernate will delete the deleted entity when we do JPA merge() call. Of course, the mapping must be correct. We use the Hibernate annotation DELETE_ORPHAN.
Example:
Code:
@OneToMany(mappedBy="project", cascade = { CascadeType.ALL }, fetch=FetchType.EAGER)
@org.hibernate.annotations.Cascade(value=org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
private Set<Party>parties = new HashSet<Party>();


Our Party object has a backpointer to it's owner, but this isn't important in this situation, because it was removed from the collection.

_________________
Carlo
-----------------------------------------------------------
please don't forget to rate if this post helped you


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 21, 2008 6:00 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
maybe you should try making it bidirectional, just to see if it changes behaviour.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: I change version and it work
PostPosted: Thu Aug 21, 2008 7:05 am 
Beginner
Beginner

Joined: Sun May 07, 2006 2:44 pm
Posts: 33
look on this JIRA:
http://opensource.atlassian.com/projects/hibernate/browse/HHH-2292


Top
 Profile  
 
 Post subject: carlolf , i think it does importatnt
PostPosted: Thu Aug 21, 2008 7:10 am 
Beginner
Beginner

Joined: Sun May 07, 2006 2:44 pm
Posts: 33
I think remove from collection is not suffiecent, because the owner side is the many


Top
 Profile  
 
 Post subject: Strange behaviouer with version 3.2.6GA
PostPosted: Thu Aug 21, 2008 7:18 am 
Beginner
Beginner

Joined: Sun May 07, 2006 2:44 pm
Posts: 33
With bidirectional and delete orphan it delete all elements when remove single entity from the collection.

Here is the code:

Code:
@Entity
public class Permission {
   private static int counter = 0;
   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   public int id;
   
   @Column
   public int name;
   
   @ManyToOne(cascade=CascadeType.ALL)
   @JoinColumn(name="user_fk")
   public User user;
}

@Entity
public class User {
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   public int id;

   @OneToMany(cascade = CascadeType.ALL,mappedBy="user")
   @org.hibernate.annotations.Cascade(value=org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
   public Collection<Permission> permissions = new LinkedList<Permission>();

}


   public static void main(String[] args) {

      // Start EntityManagerFactory
      EntityManagerFactory emf = Persistence
            .createEntityManagerFactory("test");

      // First unit of work
      EntityManager em = emf.createEntityManager();
      EntityTransaction tx = em.getTransaction();
      tx.begin();
      User u = new User();
      
      for (int i = 1; i < 5; i++) {
         Permission  p = new Permission();
         p.user = u;
         p.name = i;
         u.permissions.add(p);
      }
      u = em.merge(u);
      tx.commit();
      em.close();
      
      // second unit of work
      EntityManager em2 = emf.createEntityManager();
      EntityTransaction tx2 = em2.getTransaction();
      tx2.begin();

      for (Iterator iterator = u.permissions.iterator(); iterator.hasNext();) {
         Permission permission = (Permission) iterator.next();
         
         if(permission.id == 2){
            permission.user = null;
            iterator.remove();
         }
         
      }
      em2.merge(u);
      tx2.commit();
      em2.close();
      
      emf.close();
   }


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 21, 2008 9:07 am 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
I think you should not set cascade=CascadeType.ALL on the user member in class Permission. You wouldn't want to cascade a delete on a permission entity to a user entity, would you?

_________________
Carlo
-----------------------------------------------------------
please don't forget to rate if this post helped you


Top
 Profile  
 
 Post subject: There is not relation to the cascade
PostPosted: Thu Aug 21, 2008 9:18 am 
Beginner
Beginner

Joined: Sun May 07, 2006 2:44 pm
Posts: 33
i also try without it and its the same


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 posts ] 

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.