Bom dia pessoal,
Preciso de ajuda para acessar um SessionBean pelo módulo client.
Seguinte estou pesquisando sobre EJB 3.0 para ser utilizado no desenvolvimento de um ERP em java obviamente. O mesmo deve possuir clients Swing e Web.
Estou utilizando
IDE: netBeans 5.5.
Banco de dados: PostgreSQL 8.1
AS: Application Server PE 9
EJB: 3.0
Bom, iniciei procurando desenvolver alguma aplicação de teste. Para isso, criei algumas tabelas no banco.
Iniciei o desenvolvimento dos meus EJB pelos Entity Bean:
Segue um exemplo do EntityBean para persistir dados na tabela PAIS.
import java.io.Serializable;
import java.math.BigDecimal;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
/**
* Entity class Pais
*
* @author Administrador
*/
@Entity
@Table(name = "pais")
@NamedQueries( {
@NamedQuery(name = "Pais.findByIdPais", query = "SELECT p FROM Pais p WHERE p.idPais = :idPais"),
@NamedQuery(name = "Pais.findByDescricao", query = "SELECT p FROM Pais p WHERE p.descricao = :descricao"),
@NamedQuery(name = "Pais.findBySigla", query = "SELECT p FROM Pais p WHERE p.sigla = :sigla")
})
public class Pais implements Serializable {
@Id @GeneratedValue
@Column(name = "id_pais", nullable = false)
private BigDecimal idPais;
@Column(name = "descricao", nullable = false)
private String descricao;
@Column(name = "sigla", nullable = false)
private String sigla;
/** Creates a new instance of Pais */
public Pais() {
}
/**
* Creates a new instance of Pais with the specified values.
* @param idPais the idPais of the Pais
*/
public Pais(BigDecimal idPais) {
this.idPais = idPais;
}
/**
* Creates a new instance of Pais with the specified values.
* @param idPais the idPais of the Pais
* @param descricao the descricao of the Pais
* @param sigla the sigla of the Pais
*/
public Pais(BigDecimal idPais, String descricao, String sigla) {
this.idPais = idPais;
this.descricao = descricao;
this.sigla = sigla;
}
/**
* Gets the idPais of this Pais.
* @return the idPais
*/
public BigDecimal getIdPais() {
return this.idPais;
}
/**
* Sets the idPais of this Pais to the specified value.
* @param idPais the new idPais
*/
public void setIdPais(BigDecimal idPais) {
this.idPais = idPais;
}
/**
* Gets the descricao of this Pais.
* @return the descricao
*/
public String getDescricao() {
return this.descricao;
}
/**
* Sets the descricao of this Pais to the specified value.
* @param descricao the new descricao
*/
public void setDescricao(String descricao) {
this.descricao = descricao;
}
/**
* Gets the sigla of this Pais.
* @return the sigla
*/
public String getSigla() {
return this.sigla;
}
/**
* Sets the sigla of this Pais to the specified value.
* @param sigla the new sigla
*/
public void setSigla(String sigla) {
this.sigla = sigla;
}
/**
* Returns a hash code value for the object. This implementation computes
* a hash code value based on the id fields in this object.
* @return a hash code value for this object.
*/
@Override
public int hashCode() {
int hash = 0;
hash += (this.idPais != null ? this.idPais.hashCode() : 0);
return hash;
}
/**
* Determines whether another object is equal to this Pais. The result is
* <code>true</code> if and only if the argument is not null and is a Pais object that
* has the same id field values as this object.
* @param object the reference object with which to compare
* @return <code>true</code> if this object is the same as the argument;
* <code>false</code> otherwise.
*/
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Pais)) {
return false;
}
Pais other = (Pais)object;
if (this.idPais != other.idPais && (this.idPais == null || !this.idPais.equals(other.idPais))) return false;
return true;
}
/**
* Returns a string representation of the object. This implementation constructs
* that representation based on the id fields.
* @return a string representation of the object.
*/
@Override
public String toString() {
return "dev.com.destro.gerenciador.domain.Pais[idPais=" + idPais + "]";
}
}
import java.math.BigDecimal;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.LockModeType;
import javax.persistence.PersistenceContext;
import javax.swing.DefaultComboBoxModel;
/**
*
* @author Administrador
*/
@Stateless(mappedName="PaisFacade")
public class PaisFacade implements PaisFacadeLocal, PaisFacadeRemote {
@PersistenceContext
private EntityManager em;
/** Creates a new instance of PaisFacade */
public PaisFacade() {
}
public void create(Pais pais) {
em.persist(pais);
}
public void edit(Pais pais) {
em.merge(pais);
}
public void destroy(Pais pais) {
em.merge(pais);
em.remove(pais);
}
public Pais find(Object pk) {
return (Pais) em.find(Pais.class, pk);
}
public List findAll() {
return em.createQuery("select object(o) from Pais as o").getResultList();
}
public void insert(String descricao, String sigla) {
Pais pais = new Pais();
pais.setDescricao(descricao);
pais.setSigla(sigla);
create(pais);
}
public void update(BigDecimal id, String descricao, String sigla) {
Pais pais = find(id);
pais.setDescricao(descricao);
pais.setSigla(sigla);
edit(pais);
}
public void delete(BigDecimal id) {
Pais pais = find(id);
destroy(pais);
}
}
import java.math.BigDecimal;
import java.util.List;
import javax.ejb.Remote;
import javax.swing.DefaultComboBoxModel;
/**
*
* @author Administrador
*/
@Remote
public interface PaisFacadeRemote {
void create(Pais pais);
void edit(Pais pais);
void destroy(Pais pais);
Pais find(Object pk);
List findAll();
void insert(String descricao, String sigla);
void update(BigDecimal id, String descricao, String sigla);
void delete(BigDecimal id);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 9.0 EJB 3.0//EN" "http://www.sun.com/software/appserver/dtds/sun-ejb-jar_3_0-0.dtd">
<sun-ejb-jar>
<enterprise-beans>
<ejb>
<ejb-name>PaisFacade</ejb-name>
<jndi-name>PaisFacade</jndi-name>
</ejb>
</enterprise-beans>
</sun-ejb-jar>
Bom agora vou mostrar a classe do módulo cliente que tenta acessar o SessionBean.
import...
/**
*
* @author Administrador
*/
public class PaisForm extends JDialog {
@EJB
private PaisFacadeRemote bean;
/** Creates new form PaisForm */
public PaisForm(Frame parent, boolean modal) {
super(parent, modal);
initComponents();
bean = lookupPaisFacade();
atualizar();
}
//... outros métodos, muitos deles utilizam o objeto bean
private PaisFacadeRemote lookupPaisFacade() {
try {
Context c = new InitialContext();
return (PaisFacadeRemote) c.lookup("PaisFacade");
}
catch(NamingException ne) {
JOptionPane.showMessageDialog(this, ne.getExplanation());
Logger.getLogger(getClass().getName()).log(Level.SEVERE,"exception caught" ,ne);
throw new RuntimeException(ne);
}
}
}
Bom, acontece que sempre que eu tento chamar algum método que utilize algum método do objeto bean (ex. bean.findAll()) é gerada a seguinte exception:
Exception in thread "AWT-EventQueue-0" javax.ejb.EJBException: nested exception is: java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.RemoteException
java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.RemoteException
at com.sun.corba.ee.impl.javax.rmi.CORBA.Util.mapSystemException(Util.java:188)
at com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.privateInvoke(StubInvocationHandlerImpl.java:172)
at com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.invoke(StubInvocationHandlerImpl.java:119)
at com.sun.corba.ee.impl.presentation.rmi.bcel.BCELStubBase.invoke(BCELStubBase.java:197)
at dev.com.destro.gerenciador.domain.__PaisFacadeRemote_Remote_DynamicStub.getAll(__PaisFacadeRemote_Remote_DynamicStub.java)
at dev.com.destro.gerenciador.domain._PaisFacadeRemote_Wrapper.getAll(dev.com.destro.gerenciador.domain._PaisFacadeRemote_Wrapper.java)
.
.
.
etc.
Se alguém puder me ajudar ficarei muito grato.
Atenciosamente,
Leandro Nimet