-->
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.  [ 1 post ] 
Author Message
 Post subject: strange conclusions on merge detached collection
PostPosted: Fri Aug 22, 2008 12:17 pm 
Beginner
Beginner

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

working with hibernate core versin - 3.3.6GA

i tried to remove element (element number -2) from detached collection and merge the container.

there was strange behaviour with differents annotation.

in bidirectional i update both sides.

case 1- bidirectional without delete orphans:
dosnt delete.

case 2- bidrectional with delete orphan and in the many side cascade all:
delete the collection and the container

case 3- bidrectional with delete orphan and cascade in the many side:
delete element 2 from the database - the expected behaviour.

code:

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

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

}

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

   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();
         }
         
      }
      u = em2.merge(u);
      tx2.commit();
      em2.close();
      
      emf.close();
   }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 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.