Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Delete from an 1-N relationship

843830Aug 10 2009 — edited Aug 13 2009
Hello,
I am still new to EJB3.0 however I try to get it running. My Problem right now is, that I have two entities. One is Company, the other one Employee. The Employee is stored in a List in the Company class. Furthermore I have Facade for the Company and I try to build a little client using JSPs. (The JSP is Quick'n'Dirty - refactorings are going to come).
My problem is, that when I delete an Employee from the Company, the Client acts like it is deleted, however if I check the database, the change hasn't been stored there. What do I have to do, that the changes made in the list, which is part of the company, will be passed to the database.
            //button  delete has been pressed 
            if(request.getParameter("delete") != null) 
            {  

                //since it is a list, the amount of elements in the list is passed to this JSP
                int empSize = Integer.parseInt(request.getParameter("empSize"));        
                
                //All list elements are checked if they are supposed to be deleted
                for(int i = 0; i < empSize; i++) 
                { 
                    //just the marked list element will be deleted
                    if(request.getParameter("check"+i) != null) 
                    { 
                        employees.remove(i); 
                    } 
                } 
                //the new list with the employees will be stored in the company instance
                company.setEmps(employees); 

                //the facade pattern merges thes new and the old company (or it doesn't)
                compFacadeRemote.edit(company); 

            }
And there is the Facade - nothing fancy here
@Stateless(mappedName="jms/CompanyFacade") 
public class companyFacade implements companyFacadeRemote { 
    @PersistenceContext 
    private EntityManager em; 

    public void create(Company company) { 
        em.persist(company); 
    } 

    public void edit(Company company) { 
        em.merge(company); 
        em.flush(); 
    }
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 10 2009
Added on Aug 10 2009
5 comments
137 views