Hello to all,
I have some EJB3.0 code, so with the mappings done via Annotations.
The code was working fine, but I had to add some Entity Listeners.
Those entity listeners are of @PostLoad type.
The implementation of the listener would lookup a local implementation of a session bean that would perform a simple query like this:
Code:
Query oQuery = moManager.createQuery( "SELECT COUNT(*) FROM tEntityCommonReport cr WHERE cr.policyId='" + iPKey + "'" );
Long oNumOfErrors = (Long)oQuery.getSingleResult();
The getSingeResult call will throw an exeption like:
HibernateException: Found two representations of same collection.
What I do not understand is why a simple COUNT query would have anything to do with my collections in the Entity Bean.
I am using EJB3.0 EntityBeans, deployed on JBoss4.0.4 GA.
The policy object that has attached the PostLoad listener has a Set of Actions and a transient error state that I want to populate with the custom listener.
I have searched the forum and the docs for this but all I have found related are those topics that use session.clear(), but I do not use that. (I dont know if EJB containter calls that behind the scenes).
The custom listener is listed bellow
Code:
@PostLoad void afterPolicyLoad()
{
String szPolicyObjectName = "policy";
System.out.println( szPolicyObjectName + " '" + getName() + "' has been loaded. Setting error state..." );
try
{
InitialContext oContext = new InitialContext();
ILocalTransact oLocalTransact = (ILocalTransact)oContext.lookup( "Application/BeanTransact/local" );
errorState = oLocalTransact.getPolicyObjectState( getId(), szPolicyObjectName + "Id" );
}
catch( Exception e )
{
errorState = E_STATE_CLEAR;
e.printStackTrace();
}
}
and the getPolicyObjectState method in the BeanTransact session bean is:
Code:
public int getPolicyObjectState(long iPKey, String szIdColumn )
{
Query oQuery = moManager.createQuery( "SELECT COUNT(*) FROM tEntityCommonReport cr WHERE cr." + szIdColumn + "='" + iPKey + "'" );
try
{
Long oNumOfErrors = (Long)oQuery.getSingleResult(); //thows THE EXCEPTION!!!!!!
if( oNumOfErrors != null )
{
return oNumOfErrors > 0 ? tEntityPolicy.E_STATE_ERROR : tEntityPolicy.E_STATE_CLEAR;
}
}
catch( Exception e )
{
e.printStackTrace();
}
return tEntityPolicy.E_STATE_CLEAR;
}
the moManager is injected with @PersistenceContext annotation.
Thank you for you time.
- Ilie