I want to use Bean entity BMP but i have:<br> Erreur consultation : java.rmi.ServerException: EJBException:; nested exception is: javax.ejb.EJBException: null; CausedByException is: null <br> this error and I don't know where is the problem!<br> Please Help me<br> I have a problem with the context.getPrimaryKey(), and if i replace with a code wich is in database there is no problem!<br>
I give you my code<br>
package agrProd;
import java.rmi.RemoteException;
import java.sql.Connection;
import java.util.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.FinderException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
<br>
publicclass PeriodiciteBean implements EntityBean {
public EntityContext ctx;
public Integer code;
public String libe;
public Integer nbjt;
public Character vali;
public String utim;
public Date modi;
public Integer getCODE_PERIODICITE() {return this.code;}
public String getLIBE_PERIODICITE() { return this.libe;}
public Integer getNBJT_PERIODICITE() {
return this.nbjt;
}
public Character getVALI_PERIODICITE() {
return this.vali;
}
public String getUTIM_PERIODICITE() {
return this.utim;
}
public Date getMODI_PERIODICITE() {
return this.modi;
}
publicvoid setCODE_PERIODICITE(Integer code) {
this.code=code;
}
publicvoid setLIBE_PERIODICITE(String libe) {
this.libe=libe;
}
publicvoid setNBJT_PERIODICITE(Integer nbjt) {
this.nbjt=nbjt;
}
publicvoid setVALI_PERIODICITE(Character vali) {
this.vali=vali;
}
publicvoid setUTIM_PERIODICITE(String utim) {
this.utim=utim;
}
publicvoid setMODI_PERIODICITE(Date modi) {
this.modi=modi;
}
publicvoid ejbRemove() throws javax.ejb.RemoveException {
System.out.println("PeriodiciteBean.ejbRemove(): begin");
Connection con = null;
try {con = getConnection();
PreparedStatement statemt =con.prepareStatement("DELETE FROM PERIODICITE WHERE CODE_PERIODICITE=?");
statemt.setInt(1, this.getCODE_PERIODICITE().intValue());
if (statemt.executeUpdate() != 1) {
thrownew EJBException("Echec de la suppression");
}
statemt.close();
} catch (SQLException sqle) {
thrownew EJBException(sqle);
} finally {
try {if (con != null)
{con.close();}
} catch (SQLException sqle) {}
}
}
publicvoid ejbStore() {
System.out.println("PeriodiciteBean.ejbStore(): begin");
Connection con = null;
try {
con = getConnection();
PreparedStatement statemt =con.prepareStatement(
"UPDATE PERIODICITE SET LIBE_PERIODICITE=?, NBJT_PERIODICITE=?, VALI_PERIODICITE=?, UTIM_PERIODICITE=?, MODI_PERIODICITE=? WHERE CODE_PERIODICITE=?");
statemt.setString(1, this.getLIBE_PERIODICITE());
statemt.setInt(2, this.getNBJT_PERIODICITE().intValue());
statemt.setString(3, this.getVALI_PERIODICITE().toString());
statemt.setString(4, this.getUTIM_PERIODICITE());
statemt.setDate(5, (java.sql.Date)this.getMODI_PERIODICITE());
statemt.setInt(6, this.getCODE_PERIODICITE().intValue());
if (statemt.executeUpdate() != 1) {
thrownew EJBException("Echec de la sauvegarde");
}
statemt.close();
} catch (SQLException sqle) {
thrownew EJBException(sqle);
} finally {
try {
if (con != null) {
con.close();
}
} catch (SQLException sqle) {
}
}
}
public EntityContext getEntityContext() {
return ctx;
}
publicvoid ejbLoad() {
code = ((Integer) ctx.getPrimaryKey()).intValue();
Connection con = null;
try {
con = getConnection();
PreparedStatement statemt =
con.prepareStatement("SELECT CODE_PERIODICITE,LIBE_PERIODICITE,NBJT_PERIODICITE,VALI_PERIODICITE,UTIM_PERIODICITE,MODI_PERIODICITE FROM PERIODICITE WHERE CODE_PERIODICITE=?");
statemt.setInt(1, code);
ResultSet rs = statemt.executeQuery();
if (!rs.next()) {
thrownew EJBException("Object pas trouv?");
}
this.setCODE_PERIODICITE(code);
this.setLIBE_PERIODICITE(rs.getString(2));
this.setNBJT_PERIODICITE(rs.getInt(3));
this.setVALI_PERIODICITE(new Character(rs.getString(4).charAt(0)));
this.setUTIM_PERIODICITE(rs.getString(5)); this.setMODI_PERIODICITE(rs.getDate(6));
rs.close();
statemt.close();
} catch (SQLException sqle) {
thrownew EJBException(sqle);
}
finally {
try {
if (con != null) {
con.close();
}
} catch (SQLException sqle) {
}
}
public PeriodiciteBean()
{
// constructor
}
public Integer ejbCreate()throws CreateException {return code;}
public Integer ejbCreate(PeriodiciteDetails perio) throws CreateException {
System.out.println("PeriodiciteBean.ejbCreazte(): begin");
this.code=perio.getCODE_PERIODICITE();
this.libe=perio.getLIBE_PERIODICITE();
this.nbjt=perio.getNBJT_PERIODICITE();
this.vali=perio.getVALI_PERIODICITE();
this.utim=perio.getUTIM_PERIODICITE();
this.modi=perio.getMODI_PERIODICITE();
Connection con = null;
try {
con = getConnection();
PreparedStatement statemt =con.prepareStatement(
"INSERT INTO PERIODICITE VALUES(?,?,?,?,?,?)");
statemt.setInt(1, perio.getCODE_PERIODICITE().intValue());
statemt.setString(2, perio.getLIBE_PERIODICITE());
statemt.setInt(3, perio.getNBJT_PERIODICITE().intValue());
statemt.setObject(4, perio.getVALI_PERIODICITE());
statemt.setString(5, perio.getUTIM_PERIODICITE());
statemt.setObject(6, perio.getMODI_PERIODICITE());
if (statemt.executeUpdate() != 1) {
thrownew CreateException("Echec de la cr?ation d'un Utilisateur");
}
}
catch (SQLException sqle) {
}
finally {
try {
if (con != null) {
con.close();
}
}
catch (SQLException sqle) {
}
}
return perio.getCODE_PERIODICITE();
}
private Connection getConnection() {
System.out.println("PeriodiciteBean.getConnection: begin");
try {
Context ctx = new InitialContext();
DataSource db = (DataSource) ctx.lookup("java:/OracleDS");
return db.getConnection();
} catch (NamingException ne) {
ne.printStackTrace();
thrownew EJBException(ne);
} catch (java.sql.SQLException sqle) {
sqle.printStackTrace();
thrownew EJBException(sqle);
}
}
publicvoid ejbPostCreate(PeriodiciteDetails perio)
throws CreateException {
System.out.println("PeriodiciteBean.PostCreate begin");
}
publicvoid ejbPostCreate()
throws CreateException {
}
public Integer ejbFindByPrimaryKey(Integer code) throws FinderException {
System.out.println("PeriodiciteBean.ejbFindByPrymariKey(): begin");
PeriodiciteDetails perio=null;
try {
Connection connection = getConnection();
PreparedStatement statement =
connection.prepareStatement(
"SELECT * FROM PERIODICITE WHERE CODE_PERIODICITE = ?");
statement.setInt(1, code.intValue());
ResultSet resultSet = statement.executeQuery();
if (!resultSet.next()) {
statement.close();
connection.close();
thrownew FinderException("Could not find: " + code);
}
statement.close();
connection.close();
return code;
} catch (SQLException e) {
thrownew EJBException("Could not find: " + code, e);
}
}
public Collection ejbFindAllPeriodicites(String critere, String ordre) throws FinderException {
System.out.println("PeriodiciteBean.ejbFindAll(): begin");
ArrayList periodicites = new ArrayList();
Connection con = null;
try {
con = getConnection();
PreparedStatement statement;
if (ordre.compareTo("0") == 0) {
statement =
con.prepareStatement(
"SELECT * FROM PERIODICITE ORDER BY " + critere + "");
} else {
statement =con.prepareStatement("SELECT * FROM PERIODICITE ORDER BY "+ critere+ " DESC");
}
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
periodicites.add(new PeriodiciteDetails(resultSet));
}
return (Collection) periodicites;
} catch (SQLException sqle) {
thrownew EJBException(sqle);
} finally {
try {
if (con != null) {
con.close();
}
} catch (Exception e) {
}
}
}
publicvoid setEntityContext(EntityContext ctx) {
this.ctx = ctx;
}
publicvoid unsetEntityContext() {
this.ctx=null;
}
publicvoid ejbActivate() {
}
publicvoid ejbPassivate() {
}
}
<br>
and it is my session<br>
<br>
publicclass GestionPeriodiciteBean implements SessionBean {
private SessionContext ctx;
publicvoid ajoutPeriodicite(PeriodiciteDetails nouvellePeriodicite)
throws CreateException {
PeriodiciteLocalHome home;
home = this.getPeriodiciteLocalHome();
home.create(nouvellePeriodicite);
}
<br>
public PeriodiciteDetails getPeriodicite(Integer code)
throws FinderException {
PeriodiciteLocalHome home;
home = this.getPeriodiciteLocalHome();
returnnull;
}
<br>
public java.util.Collection listePeriodicite(String critere,String ordre)
throws FinderException {
System.out.println("GestionBean.setEntity begin");
java.util.ArrayList periodicites = new java.util.ArrayList();
PeriodiciteLocalHome home;
home = this.getPeriodiciteLocalHome();
for (java.util.Iterator it = home.findAllPeriodicites(critere,ordre).iterator();
it.hasNext();
) {
PeriodiciteLocal periodicite = (PeriodiciteLocal) it.next();
periodicites.add(
new PeriodiciteDetails(
periodicite.getCODE_PERIODICITE(),
periodicite.getLIBE_PERIODICITE(),
periodicite.getNBJT_PERIODICITE(),
periodicite.getVALI_PERIODICITE(),
periodicite.getUTIM_PERIODICITE(),
periodicite.getMODI_PERIODICITE()));
}
return (java.util.Collection) periodicites;
}
<br>
public PeriodiciteLocalHome getPeriodiciteLocalHome() {
System.out.println("GestionBean.setEntity begin");
PeriodiciteLocalHome home;
try {
InitialContext ic = new InitialContext();
Object obj = ic.lookup("java:comp/env/agrProd/Periodicite");
return (PeriodiciteLocalHome) PortableRemoteObject.narrow(
obj,
PeriodiciteLocalHome.class);
}
catch (javax.naming.NamingException e) {
thrownew EJBException();
}
}<br>
publicvoid ejbCreate() {}
publicvoid ejbPostCreate() {}
publicvoid ejbRemove() {}
publicvoid ejbActivate() {}
publicvoid ejbPassivate() {}
publicvoid setSessionContext(SessionContext ctx) {
this.ctx = ctx;}
}