Problem with finders, and proxy wrapping of getPrimaryKey()
doogie Jan 9, 2002 10:14 PM
/*
* Code block 1(the bean, which implements a custom finder,
* findByTemplate
*/
public class CommunityBean
extends CommunityImpl
implements javax.ejb.EntityBean
{
....
public Collection ejbFindByTemplate(Object template)
throws RemoteException, FinderException
{
Vector results = new Vector();
EJBCommunity community = null;
Community communityTemplate = (Community)template;
CommunityHome communityHome = (CommunityHome)ctx.getEJBHome();
try {
System.err.println( "looking for '" + communityTemplate.getId() + "'" );
community = communityHome.findByPrimaryKey( communityTemplate.getId() );
System.err.println( "found existing community, key = '" + community.getPrimaryKey() + "'" );
results.addElement( community );
} catch ( RemoteException e ) {
e.printStackTrace();
throw e;
} catch ( FinderException e ) {
e.printStackTrace();
}
return results;
}
}
/*
* Code block 2, a generic import class. It iterates over a vector of
* templates, requesting existing objects, or if no existing object is
* found, creating a new one, and then applies the template values
* onto the existing or new object.
*/
...
public EJBObject importObject( Object object )
throws Exception
{
...
Object ref = jndiContext.lookup( jndiName );
TemplateFinder finder = (TemplateFinder)PortableRemoteObject.narrow( ref, TemplateFinder.class );
System.err.println( "calling findByTemplate" );
Object newObject = null;
Collection list = finder.findByTemplate( object );
System.err.println( list.size() );
if ( list.size() == 1 )
return (EJBObject)list.iterator().next();
/*
* Code block 3, the wrapper that calls code block 2.
*/
...
EJBObject ejb = importObject( object );
...
System.err.println( "key for object was '" + ejb.getPrimaryKey() + "'(" + key.getClass().getName() + ")" );
=============================
Code block 3 calls code block 2, which calls code block 1. Please look at
the following debug print statements(and follow along in the code above),
and tell me if you see the problem.
===
[21:09:17,022,Default] importing com.kbhome.web.ejb.community.CommunityImpl@41f227,
[21:09:17,053,Default] calling findByTemplate
[21:09:17,054,Default] looking for '00600600'
[21:09:17,055,Default] found existing community, key = '00600600'
[21:09:17,078,Default] 1
[21:09:17,078,Default]
[21:09:17,132,Default] key for object was 'CommunityBean:00600600'($Proxy20)
===
Note out the inner println, prints the primary key as '00600600', but the
outer println, has the primary key munged.
The problem is that the proxy class is prepending the bean name to the
primary key. In fact, the actual call to getPrimaryKey() returns a proxy,
and NOT the key class(which is java.lang.String).
This problem occurs on JBoss 2.4.4beta.