-->
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.  [ 13 posts ] 
Author Message
 Post subject: How to remove element from collection
PostPosted: Sun Jul 01, 2007 4:06 pm 
Regular
Regular

Joined: Wed May 02, 2007 2:42 pm
Posts: 101
Department has a collection of employee.

There is not cascade.

I get department by id and than run with iterator on the collection and remove , but the element wasnt remove.

I have a bidirectional.

I know about the inverse , but i have to have inverse and i want to remove the element or set the FK to null in case i call to iterator.remove.

Thank you


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 02, 2007 6:03 pm 
Beginner
Beginner

Joined: Thu Feb 22, 2007 6:08 am
Posts: 35
Try this code if you are using EntityManager:

Code:
EntityManagerFactory emf = HibernateUtil.getEntityManagerFactory();
EntityManager em = emf.createEntityManager();
EntityTransaction transaction = em.getTransaction();

transaction.begin();
Iterator<Employee> employeesIt = department.getEmployees().iterator();
while(employeesIt .hasNext()) {
    Employee employee = employeesIt .next();
    em.remove(employee );                   
}
transaction.commit();
em.close();


if you are not using EntityManager, instead of EntityManagerFactory, EntityManager, EntityTransaction use SessionFactory, Session, and Transaction

dont forget to rate if it helped

--ms


Top
 Profile  
 
 Post subject: I work with attach object and i dont want to call explicity
PostPosted: Mon Jul 02, 2007 10:01 pm 
Regular
Regular

Joined: Wed May 02, 2007 2:42 pm
Posts: 101
to the entity manager.

If you work with attach and cascade and you add employee to the list the employee automaticly will added,

but if you removes it (by iterator ) from the list of attach object it dsont remove and i dont know how can i make sure that iterator.remove on attach will remove it.

Thank you


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 03, 2007 5:00 am 
Beginner
Beginner

Joined: Thu Feb 22, 2007 6:08 am
Posts: 35
Can you show me the code for the definition of Department and employee and the code to remove the employee?


Top
 Profile  
 
 Post subject: Code:
PostPosted: Tue Jul 03, 2007 6:10 am 
Regular
Regular

Joined: Wed May 02, 2007 2:42 pm
Posts: 101
Code:
@Entity
@Table(name = "HOTELS")
public class Hotel extends ManagedElement implements Serializable {

    private String name;
    private String address;


    private List<Room> rooms = new LinkedList<Room>();

    private Hotel() {
    }

    public Hotel(String name, String address) {
        this.name = name;
        this.address = address;
    }


    @Column(name="NAME", length=40)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Hotel(" + name + "," + address + ")";
    }

   @OneToMany(cascade = { CascadeType.PERSIST})
   public List<Room> getRooms() {
      return rooms;
   }

   public void setRooms(List<Room> rooms) {
      this.rooms = rooms;
   }
}

@Entity
public class Room extends ManagedElement implements Serializable{

   private int floor;

   private int roomNumber;


   public Room() {
   }
}


REMOVE:
      hotel =  hotelDao.findHotelById(id);
      Iterator itr  = hotel.getRooms().iterator();
      Room r3  = (Room) itr.next();
      itr.remove();


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 03, 2007 6:24 am 
Beginner
Beginner

Joined: Thu Feb 22, 2007 6:08 am
Posts: 35
i will check it out later in home, but once the cascade is just on the Persist time, i think you have to garantee that you will remove the Employee from the DB. You are just removing it from the memory. To the entity manager the record still exists.

try to do this. put on a list the elements that you want to remove on the server side and remove it from entity manager one by one:

Code:
EntityManagerFactory emf = HibernateUtil.getEntityManagerFactory();
EntityManager em = emf.createEntityManager();
EntityTransaction transaction = em.getTransaction();

transaction.begin();
Iterator<Employee> employeesIt = department.getEmployees().iterator();
while(employeesIt .hasNext()) {
    Employee employee = employeesIt .next();
    em.remove(employee );                   
}
transaction.commit();
em.close();


the [b]em.remove(employee );[/] instructions is removing it from the db. you will see when you try to get them employees from the hotel, that ones will be no more.

--ms
**dont forget to rate if it helped **


Top
 Profile  
 
 Post subject: Also cascade.merge or casacde.merge will not help
PostPosted: Tue Jul 03, 2007 6:48 am 
Regular
Regular

Joined: Wed May 02, 2007 2:42 pm
Posts: 101
If you will add an element to the collection when you are in attach mode it will automaticly persist.
No need to call explicity to the EM.

I expect to the same behaviour for remove.

I dont have an entity manager in the class.

and i dont want that any class that call to the DAO will need to have EM.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 03, 2007 7:03 am 
Beginner
Beginner

Joined: Thu Feb 22, 2007 6:08 am
Posts: 35
if you dont want to do that you cannot have the CascadeType just to Persist. try to difine the rooms like this

Code:
@OneToMany(cascade = { CascadeType.ALL})
   public List<Room> getRooms() {
      return rooms;
   }


--ms

**dont forget to rate if it helped **


Top
 Profile  
 
 Post subject: I also try with cascade.all
PostPosted: Tue Jul 03, 2007 9:48 am 
Regular
Regular

Joined: Wed May 02, 2007 2:42 pm
Posts: 101
Dosnt work.

you can test it.

add work.

remove with iterator dosnt work!


Top
 Profile  
 
 Post subject: I also try with cascade.all
PostPosted: Tue Jul 03, 2007 9:56 am 
Regular
Regular

Joined: Wed May 02, 2007 2:42 pm
Posts: 101
Dosnt work.

you can test it.

add work.

remove with iterator dosnt work!


Top
 Profile  
 
 Post subject: I also try with cascade.all
PostPosted: Tue Jul 03, 2007 10:01 am 
Regular
Regular

Joined: Wed May 02, 2007 2:42 pm
Posts: 101
Dosnt work.

you can test it.

add work.

remove with iterator dosnt work!


Top
 Profile  
 
 Post subject: removing via iterator is not sufficient
PostPosted: Thu Jul 05, 2007 1:45 pm 
Newbie

Joined: Tue Apr 24, 2007 2:19 am
Posts: 3
It says in the EJB 3.0 book (the kangaroo book, page 131) that you have to use the EntityManager to remove the collection member entity as well.


Top
 Profile  
 
 Post subject: The solution is to add cascade - delete orphan
PostPosted: Thu Jul 05, 2007 5:57 pm 
Regular
Regular

Joined: Wed May 02, 2007 2:42 pm
Posts: 101
Thank


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