posts - 0, comments - 77, trackbacks - 0, articles - 356
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

EJB初学日记(6)

Posted on 2007-09-18 21:31 semovy 阅读(189) 评论(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动作,先在本地执行一遍,然后远程执行远程的,这样就可以同步了,不过我觉得如果是网络允许的情况下,还是每次对对象操作后都能返回最新的对象好一些,免得手工去保持和远程对象的同步,这样不可靠.
 一对多目前只看到了这么多,接下来要看一下多对多的情况.

只有注册用户登录后才能发表评论。


网站导航: