dao, session bean, xdoclet

Discussions

EJB design: dao, session bean, xdoclet

  1. dao, session bean, xdoclet (3 messages)

    Hi,

    I created a session bean, and trying to access data access object, the dao is created via xdoclet tags, when I am running the client the output just prints out the messages in the bean and does not enter the dao at all.

    JBoss 3.2.1/MySql 4.x/Eclipse 3.0 integrated with lomboz.

    I would appreciate if someone could help me. What am I missing.
    I added the following tag in the xdoclet.xml.

    dao pattern="{0}" destdir="/"

    The parameter for destdir is the same as ejdoclet's destdir which is not coming up right here.

    My dao got generated fine. I have attached the code. In the output I see only.

    Entering StoreAccessBean
    Leaving StoreAccessBean


    Regards
    Arup

    /***************** code session bean********************/

    package zen.com.store.session;

    import javax.ejb.SessionBean;
    import javax.ejb.SessionContext;




    /**
     * @ejb.bean name="StoreAccess"
     * jndi-name="StoreAccessBean"
     * type="Stateless"
     *
     * @ejb.dao
     * class="zen.com.store.session.StoreAccessDAO"
     * impl-class="zen.com.store.util.dao.StoreAccessDAOImpl"
     *
     * @ejb.resource-ref res-ref-name="jdbc/MySqlDS"
     * res-type="javax.sql.Datasource"
     * res-auth="Container"
     *
     * @jboss.resource-ref res-ref-name="jdbc/MySqlDS"
     * jndi-name="java:/MySqlDS"
     *
     **/

    public abstract class StoreAccessBean implements SessionBean {


    protected SessionContext ctx;


    /**
     * @ejb.interface-method
     * view-type="remote"
     * @dao.call name="loginUser"
     *
     **/
    public String loginUser(String username, String password){
    System.out.println("Entering StoreAccessBean");

    //String userID = StoreAccessDAOImpl.loginUser("ANDY", "PASSWD");
    //System.out.println(userID);
    System.out.println("Leaving StoreAccessBean");

    // -start
    // -end
    return null;
    } //loginUser

    /**
     * Sets the param context
     * @param javax.ejb.SessionContext the new ctx value
    **/
    public void setSessionContext(SessionContext ctx){
    this.ctx = ctx;
    } //setSessionContext
    /**
     * Unsets the SessionContext
     *
     */

    public void unsetSessionContext(){
    this.ctx = null;
    }// unsetSessionContext



    }


    /**************** dao interface code **********/

    package zen.com.store.util.dao;

    import javax.naming.InitialContext;
    import javax.sql.DataSource;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;

    /**
    * @author asarkar
    *
    * To change the template for this generated type comment go to
    * Window - Preferences - Java - Code Generation - Code and Comments
    */
    public class StoreAccessDAOImpl {

    private DataSource jdbcFactory;

    public void init(){
    System.out.println("Entering StoreAccessDAOImpl.init()");
    InitialContext c = null;
    if(jdbcFactory == null){
    try{
    c = new InitialContext();
    //jdbcFactory = (DataSource)c.lookup("java:comp/env/jdbc/MySqlDS");
    jdbcFactory = (DataSource)c.lookup("java:/jdbc/MySqlDS");

    }catch(Exception e){
    System.out.println("Error in StoreAccessDAOImpl.init()" + e.toString());
    }
    }
    System.out.println("Leaving StoreAccessDAOImpl.init()");

    }


    /**
    *
    */
    public String loginUser(String username, String password){

    System.out.println("Entering StoreAccessDAOImpl.loginUser()");
    Connection conn=null;
    PreparedStatement ps=null;
    ResultSet rs=null;
    String userID = null;
    try{
    conn = jdbcFactory.getConnection();
    String queryString = "select userid from storeaccess where username = ? and password = ?";
    ps = conn.prepareStatement(queryString);
    ps.setString(1, username);
    ps.setString(2, password);
    rs = ps.executeQuery();
    boolean result = rs.next();
    if(result){
    userID = rs.getString(1);
    System.out.println("User Id : " + userID);
    }

    }catch(SQLException e){
    System.out.println("Error in StoreAccessBean.loginUser()" + e.toString());
    }finally{
    try{
    rs.close();
    ps.close();
    conn.close();

    }catch(Exception e){
    System.out.println("Final Error in StoreAccessBean.loginUser()" + e.toString());
    }
    }
    System.out.println("Leaving StoreAccessDAOImpl.loginUser()");
    return userID;
    } //loginUser

    }

    Threaded Messages (3)

  2. dao, session bean, xdoclet[ Go to top ]

    uncomment the commented lines in your EJB and the modification (create an instance of DAO as loginUser is instance method) as given below.

    public String loginUser(String username, String password){
    System.out.println("Entering StoreAccessBean");

    String userID = new StoreAccessDAOImpl().loginUser("ANDY", "PASSWD");
    System.out.println(userID);
    System.out.println("Leaving StoreAccessBean");
    return null;
    } //loginUser

    Hope this helps
  3. dao, session bean, xdoclet[ Go to top ]

    I cannot do it in the StoreAccessBean as you suggested, eclipse is throwing an error because I do not have any reference to the DAO package. Since I am using Lomboz to generate the EJB classes the following is the code for my StoreAccess.

    When I was using eclipse 2.0 with Hepersonic SQL then it worked fine, but now that I am using Eclipse 3.0 and MySql do u think I am missing something here.

    /**************** code ***************************/

    /*
     * Generated by XDoclet - Do not edit!
     */
    package zen.com.store.session;

    /**
     * Session layer for StoreAccess.
     * @lomboz generated
     */
    public class StoreAccessSession
       extends zen.com.store.session.StoreAccessBean
       implements javax.ejb.SessionBean
    {
       public void ejbActivate()
       {
       }

       public void ejbPassivate()
       {
       }

       public void setSessionContext(javax.ejb.SessionContext ctx)
       {
          super.setSessionContext(ctx);
       }

       public void unsetSessionContext()
       {
          super.unsetSessionContext();
       }

       public void ejbRemove()
       {
       }

       public void ejbCreate() throws javax.ejb.CreateException
       {
       }

          private static zen.com.store.session.StoreAccessDAO dao = null;

       protected static synchronized zen.com.store.session.StoreAccessDAO getDao()
       {
          if (dao != null) {
             return dao;
          } else {

             dao = (zen.com.store.session.StoreAccessDAO) new zen.com.store.util.dao.StoreAccessDAOImpl();

             dao.init();
             return dao;
          }
       }

        public java.lang.String loginUser(java.lang.String username,java.lang.String password)
        {

            super.loginUser(username,password);

            return getDao().loginUser(username,password);

        }

    }
  4. dao, session bean, xdoclet[ Go to top ]

    Hi,
      A suggestion is to use <dao pattern="{0}"/>. Actually, are you sure you copied from the tutorial correctly? Seems like you may have used an "o" (oh) instead of a zero. I couldn't get anything working with the destDir thing, so I just got rid of it and now I have no problems.

      Another thing is do you have the jdbc drivers for mySql? I have no idea how to setup lomboz and jboss for mysql, since I'm using Oracle, but general flow of what I did:

    1) have a server definition in the com.objectlearn.jdt.j2ee if its not there by default. I had to do lots of google searchs to find one for Oracle
    2) Make sure in the server definition file the path to your server is correct (database)
    3) setup jboss to use mySQL. There's a pdf for doing it in 3.2.x, which is what I used
    4) check the output of the jboss server to see where the exceptions occur, if any

      Hopefully that all works.