posts - 0, comments - 77, trackbacks - 0, articles - 356
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          EJB初學日記(6)

          Posted on 2007-09-18 21:31 semovy 閱讀(203) 評論(0)  編輯  收藏 所屬分類: EJB
          實體BEAN的關聯

          今天自己寫了一個例子,有關實體BEAN的關聯問題,我寫的這個例子是一對多的情況,一是人,多是狗,一個人可以養多條狗,每條狗都必須有一個主人,在刪除主人的時候,狗就沒有存在的必要了,所以狗也要跟著刪除,可是刪除狗的時候,主人卻可以在.一開始我實現的時候,刪除狗的操作是錯誤的,我以為直接把Person里面的Dog刪掉,然后更新Person就可以了,誰知不是這樣的,你在增加了Person中的狗的時候,你更新Person就可以把狗加進來了,可是當你把Person里面的狗刪除掉時,再更新Person,數據庫里面的狗卻不會少.而要你顯示的調用em.remove()去刪除你想刪除的狗,并且你對這狗不能設置為級聯刪除,否則你刪除狗的時候把人也刪掉了,這當然不是我們希望看到的.

          下面附上代碼

          package com.hadeslee.session;   
             
           
          import com.hadeslee.entity.Person;   
           
          import java.util.List;   
           
          import javax.ejb.Remote;   
             
             
           
          /**  
            * This is the business interface for PersonDAO enterprise bean.  
           
          */  
           @Remote  
           
          public interface PersonDAORemote {   
              
          public void insertPerson(Person p);   
              
          public List getAllPersons();   
              
          public Person findPerson(long id);   
              
          public void deletePerson(long id);   
           }   

           package com.hadeslee.session;   
             
           
          import com.hadeslee.entity.Dog;   
           
          import com.hadeslee.entity.Person;   
           
          import java.util.List;   
           
          import javax.ejb.Remote;   
             
             
           
          /**  
            * This is the business interface for PersonToDogs     enterprise bean.  
            
          */  
           @Remote  
          public interface PersonToDogsRemote extends PersonDAORemote{   
               
          public void addDog(Person p,Dog dog);   
               
          public Person removeDog(Person p,Dog dog);   
               
          public List getDogs(Person p);   
               
          public Person getOwner(Dog dog);   
           }   

             /*  
             * PersonDAOBean.java  
             *  
             * Created on 2006年12月13日, 下午3:53  
             *  
             * To change this template, choose Tools | Template Manager  
             * and open the template in the editor.  
             
          */  
              
            
          package com.hadeslee.session;   
              
            
          import com.hadeslee.entity.Person;   
          import java.util.List;   
           
          import javax.ejb.Stateful;   
          import javax.persistence.Query;   
           
          import javax.persistence.EntityManager;   
           
          import javax.persistence.PersistenceContext;   
             
           
          /**  
            *  
            * 
          @author lbf  
            
          */  
           @Stateful  
          public class PersonDAOBean implements com.hadeslee.session.PersonDAORemote {   
             @PersistenceContext  
              
          protected EntityManager em;   
            
          /** Creates a new instance of PersonDAOBean */  
              
          public PersonDAOBean() {   
              }   
            
              
          public void insertPerson(Person p) {   
                 em.persist(p);   
                 System.out.println(
          "保存成功");   
             }   
           
              
          public List getAllPersons() {   
                Query query
          =em.createQuery("select p from Person p order by p.id");   
                    
          return query.getResultList();   
              }   
             
               
          public Person findPerson(long id) {   
                  
          return em.find(Person.class,id);   
                 }   
                 
          public void deletePerson(long id){   
                   em.remove(em.find(Person.
          class,id));   
               }   
               
          }  

             1.  /*  
             2.  * PersonToDogsBean.java  
             3.  *  
             4.  * Created on 2006年12月13日, 下午4:50  
             5.  *  
             6.  * To change this template, choose Tools | Template Manager  
             7.  * and open the template in the editor.  
             8.  
          */  
             
          9.   
            
          10package com.hadeslee.session;   
            
          11.   
            
          12import com.hadeslee.entity.Dog;   
            
          13import com.hadeslee.entity.Person;   
            
          14import java.util.List;   
            
          15import javax.ejb.Stateless;   
            
          16import javax.persistence.Query;   
            
          17.   
            
          18/**  
            19.  *  
            20.  * 
          @author lbf  
            21.  
          */  
            
          22. @Stateless  
            
          23public class PersonToDogsBean extends PersonDAOBean implements com.hadeslee.session.PersonToDogsRemote {   
            
          24.        
            
          25.     /** Creates a new instance of PersonToDogsBean */  
            
          26.     public PersonToDogsBean() {   
            
          27.     }   
            
          28.   
            
          29.     public void addDog(Person p, Dog dog) {   
            
          30.         p.addDog(dog);   
            
          31.         em.merge(p);   
            
          32.     }   
            
          33.   
            
          34.     public Person removeDog(Person p, Dog dog) {   
            
          35.         //em.remove(dog);   
            36.         p.removeDog(dog);   
            
          37.         System.out.println("此時P的大小是:"+p.getDogs().size());   
            
          38.         em.merge(p);   
            
          39.         return p;   
            
          40.     }   
            
          41.   
            
          42.     public List getDogs(Person p) {   
            
          43.         return p.getDogs();   
            
          44.     }   
            
          45.   
            
          46.     public Person getOwner(Dog dog) {   
            
          47.         Query query=em.createQuery("select p from Person p where p.id=?1");   
            
          48.         query.setParameter(1,dog.getOwner().getId());   
            
          49.         return (Person)query.getSingleResult();   
            
          50.     }   
            
          51.        
            
          52. } 
          目前的代碼是不能正常刪除的,要正常刪除狗的話,必須把em.remove() 那句話還原,然把再把em.merge()那句話注釋起來才行還有一個現象,那就是我在對遠程對象進行了操作以后,并不會反映到我的本地對象,比如我調用了刪除人里面的一條狗,在遠程或者在數據庫里面確實是刪掉了,可是我在調用人的時候,里面還是有那么的狗,這樣就會產生不同步的現象,為了消除這種現象,我把接口寫了一個,改為進行了刪除操作的時候,它會再次返回Person對象,這個時候就可以保證遠程對象和本地對象的一致性了. 增加代碼沒寫,所以每次增加了狗后,調用Person對象時,依然是增加前一樣的,也就是說,當我持有了遠程對象發過來的本地對象后,如果我不對其進行操作,無論你怎么調用遠程方法,此方法會對其有影響的,JVM都不會返回一個最新的給你,而你用的永遠是第一次傳過來的那個以象,所以,在遠程對象傳輸中,必須要注意同步性,要么就每次都從遠程去取最新的對象,要么就先對本地對象做同樣的操作,比如前面的addDog動作,先在本地執行一遍,然后遠程執行遠程的,這樣就可以同步了,不過我覺得如果是網絡允許的情況下,還是每次對對象操作后都能返回最新的對象好一些,免得手工去保持和遠程對象的同步,這樣不可靠.
           一對多目前只看到了這么多,接下來要看一下多對多的情況.
          主站蜘蛛池模板: 容城县| 沙雅县| 石台县| 南雄市| 嘉黎县| 延寿县| 华宁县| 龙州县| 白水县| 成安县| 西昌市| 巴彦淖尔市| 彭泽县| 额尔古纳市| 无为县| 肃南| 新龙县| 武陟县| 鄯善县| 临武县| 澎湖县| 连州市| 图片| 罗田县| 元氏县| 龙泉市| 西林县| 涞源县| 临江市| 广德县| 青田县| 常山县| 新密市| 偃师市| 平度市| 公主岭市| 彭泽县| 临安市| 赞皇县| 延川县| 濮阳市|