4 Replies Latest reply on Jul 24, 2003 4:05 PM by Todd Lisonbee

    how does the @jboss:finder-query tag work?

    Todd Lisonbee Newbie

      hello,

      i've been trying to figure out how to generate a finder method using the LIKE clause with wild cards around a parameter i pass in,

      i would have thought something like this would work:

      * @ejb:finder signature="java.util.Collection findByFoo( java.lang.String pFoo )"
      * query ="SELECT OBJECT(o) FROM Customer o WHERE o.foo LIKE '%?1%'"

      i read something that said you can't pass in parameters to the like clause in EJB-QL. (Can anyone affirm this?)

      i've seen the following tags

      @jboss:finder-query
      @jboss:query
      @jboss:jboss-ql (this one isn't in the xdoclet tag reference?)

      and i am curious, how do you use them (they haven't had any effect that i've noticed with things i've tried)? and can one of these tags help me do what I want to do?

      Also, any explanation of what jaws.xml, ejb-jar.xml, and jbosscmp-jdbc.xml are for exactly would also be appreciated.


      Regards,

      Todd Lisonbee

      --------------------------------------------------------------------------------
      Stuff I've seen would leave me to believe you could over ride the query for findByFoo with the jboss:finder-query, I don't think I am getting it though:
      * @ejb:finder signature="java.util.Collection findByFoo( java.lang.String pFoo )"
      * query ="SELECT OBJECT(o) FROM Customer o"
      * @jboss:finder-query name="findByFoo"
      * query="foo LIKE \"%\"{0}\"%\" "


      my one query that works (works without the LIKE clause):
      ----------------------------------------------------------------
      * @ejb:finder signature="java.util.Collection findByPan( java.lang.String pPan )"
      * query="SELECT OBJECT(o) FROM Customer o WHERE o.pan = ?1"

        • 1. Re: how does the @jboss:finder-query tag work?
          Vishal Sharma Expert

          ejb-jar.xml is where deployment descriptors are written for EJB.
          web.xml has deployment descriptor for web components, ex servlet name, url-pptern etc.

          jaws.xml is used by JAWS, which is the O/R mapper, used by JBoss to manage CMP entity beans.
          What you can do with standardjaws.xml / jaws.xml:

          Specify a datasource and the type-mappings to use with it
          Set a bunch of options concerning jaws behavior
          Specify how JAWS should build/use your tables
          Define finders to access you entity beans

          For your question on EJB QL type, why dont you look into EJP specs.
          I hope this helps...

          • 2. Re: how does the @jboss:finder-query tag work?
            Ed Ross Newbie

            This may be a bit late for you, but I was having the same problem. I was under the impression the @jboss.finder-query should have put an entry into the jbosscmp-jdbc.xml file for the finder. If it does, I could find the magic to make it happen.

            Here's what I did, Defined a normal finder (but left the query blank in the xdoc tag) then I defined a @jboss.query (NOTE NOT FINDER-QUERY). This puts an entry in the ejb-jar.xml for the finder and an entry in the jbosscmp-jdbc.xml for the query. Seems to work great.

            • 3. Re: how does the @jboss:finder-query tag work?
              Vishal Sharma Expert

              @ejb:finder signature="test.interfaces.Manager findByName( java.lang.String pSurname, java.lang.String pLastName )"

              @jboss:finder-query name="findByName" query="First_Name = {0} AND Last_Name = {1}"

              After this message posted by edross, I had a look at Jboss Quick start guide to see, if there is something like this you can do.
              And this is what is give on pg-31.
              It seems to me you can override yr query in jboss tags after leaving the query in ejb tags.
              Hopefully this works for you.

              Cheers...


              • 4. Re: how does the @jboss:finder-query tag work?
                Todd Lisonbee Newbie



                Thanks for the replies,
                they helped in understanding some things,

                i eventually solved my problem...

                here is my solution:

                my main problem was wanting to do something like this

                * @ejb:finder signature="java.util.Collection findByFoo( java.lang.String pFoo )"
                * query ="SELECT OBJECT(o) FROM Customer o WHERE o.foo LIKE '%?1%'"

                I never figured out a way to use LIKE with the % symbols though.
                EntityBeans seem faily limited in what you can do with queries and inserts, updates are nice though. (I'm not sure how other app servers compare on these points).

                What I ended up doing was breaking away from the J2EE model, by-passing the entity beans and just getting a JDBC connection and executing a query.

                There is probably some better/more official JBoss way to do this (I still have no idea what that might be) but this worked great as a Quick and Easy get the job done approach.

                I ended up using this approach to solve a number of problems in my application.

                Here it is:

                import javax.sql.DataSource;
                import javax.naming.InitialContext;
                import javax.naming.NamingException;
                import javax.naming.Context;
                import java.sql.PreparedStatement;
                import java.sql.Connection;
                import java.sql.SQLException;
                import java.sql.ResultSet;
                import java.util.Vector;


                //do all of the following in some method() in a stateless session bean

                DataSource dataSource = null;
                Connection conn = null;
                Vector returnVector = new Vector();

                //get the database connection
                try
                {
                Context ctx = new InitialContext ();
                dataSource = (DataSource) ctx.lookup ("java:/PostgresDS");
                conn = dataSource.getConnection ();
                }
                catch (SQLException e) { System.out.println(e); }
                catch (NamingException e) { System.out.println("Naming Exception => " + e); }


                try
                {

                String queryString = "Select customer_id, f_name, l_name, address, city, state, zip, phone" +
                " from customer " +
                " where l_name Like ? ";

                pstmt.setString(1, "%" + lastName + "%" );

                ResultSet rs = pstmt.executeQuery();

                while (rs.next())
                {
                CustomerData record_data = new CustomerData();

                record_data.setCustomerId( rs.getInt("customer_id") );
                record_data.setCardFirstName(rs.getString("f_name") );
                record_data.setCardLastName(rs.getString("l_name") );
                record_data.setAddress(rs.getString("address") );
                record_data.setCity(rs.getString("city") );
                record_data.setState(rs.getString("state") );
                record_data.setZip(rs.getString("zip") );
                record_data.setPhone(rs.getString("phone") );

                returnVector.add( record_data );
                }

                pstmt.close();
                conn.close();

                }
                catch (SQLException e)
                {
                System.out.println("SqlException => " + e);

                }


                return returnVector;

                -----------------------------------------------------------

                I hope this helps someone else with a similar problem.

                Todd Lisonbee