0
votes

I am just trying out examples of entity beans. I am using JBOSS 4.0.0, EJB 2.0, MyEclipse 8.6. Reason for using JBOSS 4.0.0 and EJB 2.0 is just for learning sake. I am reading head first book. In the process help me if I made any mistakes. I wrote a build file using ant to deploy this in deploy directory of JBOSS. Don't know where I went wrong, but struggling with error. I am getting this error:

java.lang.NullPointerException at org.jboss.ejb.plugins.local.LocalHomeProxy.invoke(LocalHomeProxy.java:118) at $Proxy0.findByPrimaryKey(Unknown Source) at AccesesPackage.DirectorMovie.go(DirectorMovie.java:24) at AccesesPackage.DirectorMovie.main(DirectorMovie.java:13)

Thank you in advance.

My ejb-jar.xml is:

<?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>

My jboss.xml file:

<?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>

My Director package:

  1. Director Bean:

    package director;

    import java.rmi.RemoteException; import javax.ejb.; import java.util.; import movie.*; 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;

    public abstract class DirectorBean implements 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. Director Home:

    package director;

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

    public interface DirectorHome extends EJBLocalHome {

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

    }

  3. Director component interface:

    package director; import javax.ejb.; import java.util.; public interface Director extends EJBLocalObject {

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

    }

My Movie package:

  1. Movie Bean:

    package movie;

    import java.rmi.RemoteException; import javax.ejb.; import director.;

    import java.util.*;

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

    public abstract class MovieBean implements 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. Movie Home:

    package movie;

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

    public interface MovieHome extends EJBLocalHome {

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

    }

  3. Movie component interface:

    package movie;

    import javax.ejb.*;

    public interface Movie extends EJBLocalObject {

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

    }

Finally my client:

package AccesesPackage;
import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;
import movie.*;
import director.*;
import javax.ejb.*;

import java.util.*;

public class DirectorMovie {

    //public static final String localJndiName=  "local/"+DirectorBean.class.getSimpleName();

    public static void main(String[] args){
        new DirectorMovie().go();
    }

    public void go(){
        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);
            DirectorHome dir0 = (DirectorHome)ic.lookup("directors");
            dir0.findByPrimaryKey("1");

            //DirectorHome dir0 = (DirectorHome)PortableRemoteObject.narrow(d, DirectorHome.class);
           // Director direct0 = dir0.create("1", true, "3", "Chaitanya");
            //Collection col = direct0.getAllMovies();
            //if(col.isEmpty()){
            //  System.out.println("The returned collection is empty");
            //}
            //else{
            //  Iterator ita = col.iterator();
            //  if (ita.hasNext()){
            //      Movie movieName = (Movie)ita.next();
            //      System.out.println(movieName.getMovieTitle());
            //  }
            //}
            //System.out.println(direct0.directorName());
            //MovieHome mov0 = (MovieHome)ic.lookup("movies");
            //MovieHome mov0 = (MovieHome)PortableRemoteObject.narrow(m, MovieHome.class);
            //Movie movie0 = mov0.create("M1", "Men In Black", "2001", "Action", "1");
            //System.out.println(movie0.getMovieTitle());
            //String mdId = movie0.getMovieDirectorId();
            //String directorName = movie0.getMovieDirectorName(mdId);
            //System.out.println(directorName);
            //System.out.println();
            }catch(Exception ex){
            ex.printStackTrace();
        }
    }
}
1
Hi above is the detailed description of the problem.user467387

1 Answers

0
votes

I think that the reason is you aren't supposed to define the findByPrimaryKey in the ejb-jar.xml.

The container will built it automatically using your deployment descriptor, the findByPrimaryKey is defined only the Home interface.