Juan M Wagner wrote:
Hi again.
Another question, I'm using an Entity Bean to make chances to certain registry of
a table (mysql and postgresql), but if I want to obtain bulk data with SELECT its better to
use the Session (Stateless) Bean ?
Juan,
In ejb2.0 you can have home business methods that return anything from the database. you can return a list of all customer's first names, a hash table perhaps of records, etc.
to answer you 2nd question, session beans only need ejb-jar.xml (and possibly the vendor's ejb-jar.xml if applicable).
I think that Entity Beans are only for point to exactly one row of the table...
That's why I do an Session Bean , because a want many records in a Hashtable.
Here's the files.
-- my home interface --
package libro;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
public interface LibroHome extends EJBHome { Libro create() throws CreateException, RemoteException; }
-- my remote interface -- package libro; import javax.ejb.EJBObject; import java.rmi.RemoteException; import java.util.Hashtable;
public interface Libro extends EJBObject {
// This Method must to return a Hashtable with the result of the query (Select *)
public Hashtable buscaPorTitulo (String titulo) throws RemoteException;
}
-- my BEAN ! -- package libro; import javax.ejb.*; import javax.naming.*; import java.sql.*; import javax.sql.DataSource; import java.util.*; import java.rmi.RemoteException;
public class LibroBean implements SessionBean { // must implemet the sessionbean
public EntityContext context;
public void ejbCreate() {};
public Hashtable buscaPorTitulo (String titulo) throws SQLException {
Connection con = null;
PreparedStatement ps = null;
ResultSet result = null;
try {
con = this.getConnection (); // Preparing the query statement
ps = con.prepareStatement ("select isbn, titulo from libro where titulo = ?");
ps.setString (1,titulo);
result = ps.executeQuery ();
Hashtable keys = new Hashtable (); Vector datos = new Vector(); while( result.next() ) { datos.addElement (result.getObject ("titulo")); keys.put(result.getString("isbn"),datos); } return keys; // Here's where we return the Hashtable
} catch (SQLException se) {throw new EJBException (se);} finally { try { result.close (); } catch (Exception e) {} try { ps.close (); } catch (Exception e) {} try { con.close (); } catch (Exception e) {} } }//ejbFindByTitulo
public void setSessionContext( SessionContext context ) throws EJBException, RemoteException { }
public void ejbActivate() throws EJBException, RemoteException { } public void ejbLoad() throws EJBException, RemoteException { } public void ejbPassivate() throws EJBException, RemoteException { } public void ejbRemove() throws EJBException, RemoteException { } public void ejbStore() throws EJBException, RemoteException { }
private Connection getConnection () throws SQLException {
try {
Context jndiCntx = new InitialContext ();
DataSource ds = (DataSource)jndiCntx.lookup ("java:comp/env/jdbc/libreria");
return ds.getConnection ();
} catch (NamingException ne) { throw new EJBException (ne); }
}
}
Ok, here is where I got my doubts... In ENTITY BEANS xml files we must to put information about the database. But if we use a Session Bean that is necesary ??? I think we dont.
To use this Session Bean only we need the ejb-jar.xml ??
-- my ejb-jar.xml -- <ejb-jar>
<enterprise-beans>
<session>
<description>
Este SESSION Bean representa al objeto Libro.
</description>
<ejb-name>LibroBean</ejb-name>
<home>libro.LibroHome</home>
<remote>libro.Libro</remote>
<ejb-class>libro.LibroBean</ejb-class>
<session-type>Stateless</session-type>
<resource-ref>
<description>DataSource para la base de datos de libros</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth> </resource-ref> </entity>
</enterprise-beans>
<assembly-descriptor> <method-permission> <role-name>everyone</role-name> <method> <ejb-name>LibroBean</ejb-name> <method-name>*</method-name> </method> </method-permission> <container-transaction> <method> <ejb-name>LibroBean</ejb-name> <method-name>*</method-name> </method> <trans-attribute>Supports</trans-attribute> </container-transaction> </assembly-descriptor>
</ejb-jar>
There is yet another way to connect to OpenEJB, which I think would be the best fit in your envrironment. Externalize OpenEJB configuration from the code using the OpenEJB Tomcat resource factory. Here's more info on that: http://www.openejb.org/tomcat.html#tomcat-resource-factory.
Also, make sure the latest OpenEJB is used - pre-1.0. Unfortunatelly, it needs to be built from the sources - it's not yet available anywhere.
