• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

find methods

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I have a find method someting like this in CMP:
Question 1:
findByPersonNames(String [] names)
If names array is null, I should get all Persons.
If names array has one name, then I sould get one Person
If names array has two names, then I sould get two Persons, and so on.
Question 2:
I have the following three find methods:
findByName(String name)
findByAge(int age)
findByNameAndAge(String name, int age)
Can I combine the above three method into one, like
findByCombinedCondition(String name, int age)
If age is 0, this should work like the first method
If name is null, this should work like the second method
If age and name are present, this should work like the third method
 
Ranch Hand
Posts: 1066
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would recommend BMP... :roll:
 
Deepa Malhotra
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well. I already have the application in CMP. Is there any way to get these requirements implemented.
 
author
Posts: 3902
10
Redhat Quarkus Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Write a method in a POJO or in a Session bean that implements the method signature you want and the business logic you want. There is no way to do this in the CMP.
Kyle
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
package com.ejb;
import java.sql.Connection;
import java.util.ArrayList;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* It's just a fun,i only resolve a question for a net fiend.
* I am a chinese, i learn ejb 2 years,if my anwers you can't resolve
* your question communicate with me.
* @author xunn([email protected])
* 2003-12-16 9:41
*/
public class Finder{
public static Connection conn ;
private static StringBuffer queryNames =
new StringBuffer("select * from ");
private static String tableName = "mytable";
/**
*construstor now create connection
*/
public Finder(){
if(conn == null){
//create a Connection ,DriverFactory is
//my Connection factory can crete an database connection.
conn = DriverFactory.getConnection();
}
}
/**
* construstor now create connection and
* give the class member tableName a real table name;
*/
public Finder(String tableName){
if(conn == null){
//create a Connection ,DriverFactory is my
// Connection factory can crete an database connection.
conn = DriverFactory.getConnection();
}
this.tableName = tableName;
}
/**
* use a StringBuffer to make sql,current return is ArrayList
* you can use return an Object
* has more set or get method.I use ArrayList
* ,It's a simple resolve method.
* findByPersonNames(String [] names)
* If names array is null, I should get all Persons.
* If names array has one name,
* then I sould get one Person
* If names array has two names,
* then I sould get two Persons, and so on.
* question resolve, ok?
*/
public ArrayList findByPersonNames(String[] names){
ArrayList ret = new ArrayList();
Statement stmt = null;
ResultSet rs = null;
queryNames.append(tableName);
if(names != null){
queryNames.append(" where ");
for(int i = 0;i<names.length;i++){
if(i == 0)
queryNames.append(" name=").append(names[i]);
else
queryNames.append(" or name=").append(names[i]);
}
}
try{
stmt = conn.createStatement();
rs = stmt.executeQuery(queryNames.toString());
while(rs.next()){
//you can use getString or other method to
//choose which you will be used
ret.add(rs.getObject(1));
}
}catch(SQLException sqle){
sqle.printStackTrace();
}finally{
try{
if(rs != null) rs.close();
if(stmt != null) stmt.close();
}catch(SQLException ex){
ex.printStackTrace();
}
}
return ret;
}
/**
*I have the following three find methods:
*findByName(String name)
*findByAge(int age)
*findByNameAndAge(String name, int age)
* you can use same way to resolve your question.
* good luck to you .
*/
}
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for your first question:
You haven't told us what application server you are using. In case you are using JBoss 3.x you can use DynamicQL(its JBossQL constructed inside the code). Here it is a sample code:
public abstract class UserBean implements EntityBean {
public abstract Set ejbSelectGeneric(String jbossQl, Object[] arguments)
throws FinderException;
public Set ejbHomeSelectUsers(Set names) throws FinderException {
// generate JBossQL query
StringBuffer jbossQl = new StringBuffer();
jbossQl.append("SELECT OBJECT(u) ");
jbossQl.append("FROM Users AS u ");
jbossQl.append("WHERE u.name IN (");
for(int i = 0; i < names.size(); i++) {
if(i > 0) {
jbossQl.append(", ");
}
jbossQl.append("?").append(i+1);
}
jbossQl.append(") ORDER BY u.name");
// pack arguments into an Object[]
Object[] args = states.toArray(new Object[states.size()]);
// call dynamic-ql query
return ejbSelectGeneric(jbossQl.toString(), args);
}
}
and inside your deployment descriptor (jbosscmp-jdbc.xml) you have to declare the ejbSelectGeneric like this:
<jbosscmp-jdbc>
<enterprise-beans>
<entity>
<ejb-name>GangsterEJB</ejb-name>
<query>
<query-method>
<method-name>ejbSelectGeneric</method-name>
<method-params>
<method-param>java.lang.String</method-param>
<method-param>java.lang.Object[]</method-param>
</method-params>
</query-method>
<dynamic-ql/>
</query>
</entity>
</enterprise-beans>
</jbosscmp-jdbc>

About your second question:
you should differentiate these cases inside the session facade or whatever you use in front of the entities. You should not load the CMPs more than you have to and the business logic should be included for instance inside a session bean which is called by the client and which calls the entities.
[ December 16, 2003: Message edited by: sergiu truta ]
[ December 16, 2003: Message edited by: sergiu truta ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic