引用本地bean home接口时ejb 2.0中的空指针异常

时间:2010-10-05 23:04:09

标签: ejb ejb-2.x

我只是尝试实体bean的例子。我使用的是JBOSS 4.0.0,EJB 2.0,MyEclipse 8.6。使用JBOSS 4.0.0和EJB 2.0的原因只是为了学习。我正在读第一本书。如果我犯了任何错误,请帮助我。我使用ant编写了一个构建文件,将其部署在JBOSS的deploy目录中。不知道我哪里出错了,但却在犯错误。我收到了这个错误:

显示java.lang.NullPointerException 在org.jboss.ejb.plugins.local.LocalHomeProxy.invoke(LocalHomeProxy.java:118) 在$ Proxy0.findByPrimaryKey(未知来源) 在AccesesPackage.DirectorMovie.go(DirectorMovie.java:24) 在AccesesPackage.DirectorMovie.main(DirectorMovie.java:13)

提前谢谢。

我的ejb-jar.xml是:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd"> 
<ejb-jar>
<display-name>EJB1</display-name>
<enterprise-beans>

<session>
<display-name>AdviceBean</display-name>
<ejb-name>AdviceBean</ejb-name>
<home>headfirst.AdviceHome</home>
<remote>headfirst.Advice</remote>
<ejb-class>headfirst.AdviceBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Bean</transaction-type>
</session>

<session>
<display-name>AdviceStatefullBean</display-name>
<ejb-name>AdviceStatefullBean</ejb-name>
<home>headfirstStatefull.AdviceHomeStatefull</home>
<remote>headfirstStatefull.AdviceStatefull</remote>
<ejb-class>headfirstStatefull.AdviceStatefullBean</ejb-class>
<session-type>Stateful</session-type>
<transaction-type>Bean</transaction-type>
</session>

<entity>
<display-name>MovieBean</display-name>
<ejb-name>MovieBean</ejb-name>
<local-home>movie.MovieHome</local-home>
<local>movie.Movie</local>
<ejb-class>movie.MovieBean</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.String</prim-key-class>
<reentrant>False</reentrant>
<cmp-version>2.x</cmp-version>
<abstract-schema-name>moviebean</abstract-schema-name>
<cmp-field><field-name>movieId</field-name></cmp-field>
<cmp-field><field-name>title</field-name></cmp-field>
<cmp-field><field-name>genre</field-name></cmp-field>
<cmp-field><field-name>directorId</field-name></cmp-field>
<cmp-field><field-name>year</field-name></cmp-field>
<primkey-field>movieId</primkey-field>
<security-identity>
<use-caller-identity></use-caller-identity>
</security-identity>

<query>
<query-method>
<method-name>findByPrimaryKey</method-name>
<method-params>
<method-param>java.lang.String</method-param>
</method-params>
</query-method>
<ejb-ql>SELECT OBJECT(m) FROM moviebean m WHERE m.movieId = ?1</ejb-ql>
</query>

</entity>

<entity>
<display-name>DirectorBean</display-name>
<ejb-name>DirectorBean</ejb-name>
<local-home>director.DirectorHome</local-home>
<local>director.Director</local>
<ejb-class>director.DirectorBean</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.String</prim-key-class>
<reentrant>False</reentrant>
<cmp-version>2.x</cmp-version>
<abstract-schema-name>directorbean</abstract-schema-name>
<cmp-field><field-name>directorId</field-name></cmp-field>
<cmp-field><field-name>oscarWinner</field-name></cmp-field>
<cmp-field><field-name>degrees</field-name></cmp-field>
<cmp-field><field-name>name</field-name></cmp-field>
<primkey-field>directorId</primkey-field>
<security-identity>
<use-caller-identity></use-caller-identity>
</security-identity>

<query>
<query-method>
<method-name>findByPrimaryKey</method-name>
<method-params>
<method-param>java.lang.String</method-param>
</method-params>
</query-method>
<ejb-ql>SELECT OBJECT(d) FROM directorbean d WHERE d.directorId = ?1</ejb-ql>
</query>


<query>
<query-method>
<method-name>ejbSelectGetAllMovies</method-name>
<method-params>
<method-param>java.lang.String</method-param>
</method-params>
</query-method>
<ejb-ql>SELECT OBJECT(m) FROM moviebean m WHERE m.directorId = ?1</ejb-ql>
</query>

</entity>

</enterprise-beans>


<relationships>
<ejb-relation>
<ejb-relationship-role>
<ejb-relationship-role-name>Director-directs-many-movies</ejb-relationship-role-name>
<multiplicity>One</multiplicity>
<relationship-role-source>
<ejb-name>DirectorBean</ejb-name>
</relationship-role-source>
<cmr-field>
<cmr-field-name>movies</cmr-field-name>
<cmr-field-type>java.util.Collection</cmr-field-type>
</cmr-field>
</ejb-relationship-role>

<ejb-relationship-role>
<ejb-relationship-role-name>Movie-has-one-director-only</ejb-relationship-role-name>
<multiplicity>Many</multiplicity>
<cascade-delete />
<relationship-role-source>
<ejb-name>MovieBean</ejb-name>
</relationship-role-source>
</ejb-relationship-role>
</ejb-relation>
</relationships>
</ejb-jar>

我的jboss.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS 3.2//EN" "http://www.jboss.org/j2ee/dtd/jboss_3_2.dtd">
<jboss>
<enterprise-beans>
<session>
<ejb-name>AdviceBean</ejb-name>
<jndi-name>Advisor</jndi-name>
</session>
<session>
<ejb-name>AdviceStatefullBean</ejb-name>
<jndi-name>AdvisorStatefull</jndi-name>
</session>
<entity>
<ejb-name>MovieBean</ejb-name>
<local-jndi-name>movies</local-jndi-name>
</entity>
<entity>
<ejb-name>DirectorBean</ejb-name>
<local-jndi-name>directors</local-jndi-name>
</entity>
</enterprise-beans>
</jboss>

我的导演包:

  1. 导演Bean:

    包裹导演;

    import java.rmi.RemoteException; import javax.ejb。; import java.util。; 导入电影。*; import javax.naming.Context; import javax.naming.InitialContext; import javax.sql.ConnectionPoolDataSource; import javax.sql.PooledConnection; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import AccesesPackage.ConnectionPool;

    公共抽象类DirectorBean实现EntityBean {

    private EntityContext context;
    
    
    public void ejbActivate() throws EJBException{
        System.out.println("Inside director ejb activate method");
    }
    
    
    public void ejbLoad() throws EJBException {
        System.out.println("Inside director ejb load method");
    }
    
    
    public void ejbPassivate() throws EJBException{
        System.out.println("Inside director ejb passivate method");
    }
    
    
    public void ejbRemove() throws RemoveException, EJBException{
        System.out.println("Inside director ejb remove method");
    }
    
    
    public void ejbStore() throws EJBException{
        System.out.println("Inside director ejb store method");
    }
    
    
    public void setEntityContext(EntityContext arg0) throws EJBException{
        this.context = arg0;
        System.out.println("Inside director ejb set entity context method");
    }
    
    
    public void unsetEntityContext() throws EJBException{
        System.out.println("Inside director ejb unset entity context method");
    }
    
    
    public abstract void setDirectorId(String id);
    public abstract String getDirectorId();
    public abstract void setOscarWinner(boolean b);
    public abstract boolean getOscarWinner();
    public abstract void setDegrees(String m);
    public abstract String getDegrees();
    public abstract void setName(String name);
    public abstract String getName();
    public abstract Collection getMovies();
    public abstract void setMovies(Collection movies);
    
    
    public abstract Collection ejbSelectGetAllMovies(String directorId) throws FinderException;
    
    
    public String directorName(){
        return this.getName();
    }
    
    
    public java.lang.String ejbCreate(String directorId, boolean isOscar, String degrees, String name) throws CreateException {
        this.setDirectorId(directorId);
        this.setOscarWinner(isOscar);
        this.setDegrees(degrees);
        this.setName(name);
        return directorId;
    }
    
    
    public void ejbPostCreate(String directorId, boolean isOscar, String degrees, String name) throws CreateException {
    
    
    }
    
    
    public Collection getAllMovies() {
        Collection coll = null;
        try{
            coll = this.ejbSelectGetAllMovies(this.getDirectorId());
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return coll;
    }
    
    
    public void changeOscarStatus(String b){
        Connection con= null;
        try{
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/ejbpr","root","chaitanya");
        String sql1 = "UPDATE director SET degrees = ? where directorId = ?";
        PreparedStatement pstmt = conn.prepareStatement(sql1);
        pstmt.setString(1, b);
        pstmt.setString(2, this.getDirectorId());
        boolean update  = pstmt.execute();
        conn.close();
        System.out.println(update);
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
    

    }

  2. 导演主页:

    包裹导演;

    import javax.ejb。; import java.util。;

    public interface DirectorHome扩展了EJBLocalHome {

    public Director create(String directorId, boolean isOscar, String degrees, String name) throws CreateException;
    
    
    public Director findByPrimaryKey(String directorId) throws FinderException;
    

    }

  3. 导演组件界面:

    包裹导演; import javax.ejb。; import java.util。; public interface Director扩展了EJBLocalObject {

    public void changeOscarStatus(String st);
    
    
    public String directorName();
    
    
    public Collection getAllMovies();
    

    }

  4. 我的电影包:

    1. 电影豆:

      包电影;

      import java.rmi.RemoteException; import javax.ejb。; 进口总监。;

      import java.util。*;

      import javax.naming。*; import javax.rmi.PortableRemoteObject;

      公共抽象类MovieBean实现EntityBean {

      private EntityContext context = null;
      
      
      public void ejbActivate() throws EJBException {
          System.out.println("Inside movie ejb activate method");
      }
      
      
      public void ejbLoad() throws EJBException {
          System.out.println("Inside movie ejb load method");
      }
      
      
      public void ejbPassivate() throws EJBException {
          System.out.println("Inside movie ejb passivate method");
      }
      
      
      public void ejbRemove() throws RemoveException, EJBException {
          System.out.println("Inside movie ejb remove method");
      }
      
      
      public void ejbStore() throws EJBException, RemoteException {
          System.out.println("Inside movie ejb store method");
      }
      
      
      public void setEntityContext(EntityContext arg0) throws EJBException {
          this.context = arg0;
          System.out.println("Inside movie ejb set entity context method");
      }
      
      
      public void unsetEntityContext() throws EJBException {
          System.out.println("Inside movie ejb unset entity context method");
      }
      
      
      public String getMovieTitle(){
          return this.getTitle();
      }
      
      
      public String getMovieDirectorId(){
          return this.getDirectorId();
      }
      
      
      public String getMovieDirectorName(String directorId){
          String name= null;
          try{
          Properties properties = new Properties();
          properties.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
          properties.put("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
          properties.put("java.naming.provider.url","localhost:1099");
          Context ic = new InitialContext(properties);
          Object d = ic.lookup("directors");
          DirectorHome dir0 = (DirectorHome)PortableRemoteObject.narrow(d, DirectorHome.class);
          Director direct0 = dir0.findByPrimaryKey(directorId);
          name= direct0.directorName();
          }catch(Exception ex){ex.printStackTrace();}
      
      
      
      return name;
      
      } public abstract String getMovieId(); public abstract void setMovieId(String movieId); public abstract String getTitle(); public abstract void setTitle(String title); public abstract String getYear(); public abstract void setYear(String year); public abstract String getGenre(); public abstract void setGenre(String genre); public abstract void setDirectorId(String directorId); public abstract String getDirectorId(); public abstract void setDirector(Director director); public abstract Director getDirector(); public java.lang.String ejbCreate(String movieId, String title, String year, String genre, String directorId) throws CreateException { this.setMovieId(movieId); this.setTitle(title); this.setYear(year); this.setGenre(genre); this.setDirectorId(directorId); return movieId; } public void ejbPostCreate(String movieId, String title, String year, String genre, String directorId) throws CreateException{ }

      }

    2. 电影之家:

      包电影;

      import javax.ejb。; import java.util。;

      公共界面MovieHome扩展了EJBLocalHome {

      return name;
      

      }

    3. 电影组件界面:

      包电影;

      导入javax.ejb。*;

      public interface Movie扩展了EJBLocalObject {

      public Movie create(String movieId, String title, String year, String genre, String directorId) throws CreateException;
      
      
      public Movie findByPrimaryKey(String Key) throws FinderException;
      

      }

    4. 最后我的客户:

      public String getMovieTitle();
      
      
      public String getMovieDirectorName(String directorId);
      
      
      public String getMovieDirectorId();
      

1 个答案:

答案 0 :(得分:0)

我认为原因是你不应该在ejb-jar.xml中定义findByPrimaryKey

容器将使用您的部署描述符自动构建,findByPrimaryKey仅定义为Home接口。