Я просто пробую примеры объектных компонентов. Я использую JBOSS 4.0.0, EJB 2.0, MyEclipse 8.6. Причина использования JBOSS 4.0.0 и EJB 2.0 - просто для обучения. Я читаю первую книгу. В процессе помогите мне, если я допустил ошибки. Я написал файл сборки с помощью ant, чтобы развернуть его в каталоге развертывания JBOSS. Не знаю, где я ошибся, но борюсь с ошибкой. Я получаю эту ошибку:
java.lang.NullPointerException в org.jboss.ejb.plugins.local.LocalHomeProxy.invoke (LocalHomeProxy.java:118) в $ Proxy0.findByPrimaryKey (неизвестный источник) в AccesesPackage.DirectorMovie.ceava:Movage.java: .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<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS 3.2//EN" "http://www.jboss.org/j2ee/dtd/jbossprivate 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.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>
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/jbossprivate 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.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>
Пакет «Мой директор»:
Директор Бин:
руководитель пакета;
import java.rmi.RemoteException; import javax.ejb. ; импортировать 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; импорт 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(); } }
}
Директор Дом:
руководитель пакета;
import javax.ejb. ; импортировать java.util.;
открытый интерфейс DirectorHome расширяет EJBLocalHome {
public Director create(String directorId, boolean isOscar, String degrees, String name) throws CreateException; public Director findByPrimaryKey(String directorId) throws FinderException;
}
Интерфейс компонента директора:
руководитель пакета; import javax.ejb. ; импортировать java.util.; Public Interface Director расширяет EJBLocalObject {
public void changeOscarStatus(String st); public String directorName(); public Collection getAllMovies();
}
Пакет "Мой фильм":
Movie Bean:
пакетный фильм;
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();}
} 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{ }return name;
}
Дом фильма:
пакетный фильм;
import javax.ejb. ; импортировать java.util.;
открытый интерфейс MovieHome расширяет EJBLocalHome {
public Movie create(String movieId, String title, String year, String genre, String directorId) throws CreateException; public Movie findByPrimaryKey(String Key) throws FinderException;
}
Интерфейс компонента фильма:
пакетный фильм;
import javax.ejb. *;
открытый интерфейс Movie расширяет EJBLocalObject {
public String getMovieTitle(); public String getMovieDirectorName(String directorId); public String getMovieDirectorId();
}
Наконец мой клиент:
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();
}
}
}