Hibernate is trying to load collections it isn't supposed to when using bean
validation event listener
------------------------------------------------------------------------------------------------------
Key: HHH-4462
URL:
http://opensource.atlassian.com/projects/hibernate/browse/HHH-4462
Project: Hibernate Core
Issue Type: Bug
Affects Versions: 3.3.2
Environment: Also says the same thing in 3.5.0.Beta-1
Reporter: Ken Egervari
I'm getting a weird error that only occurs when the bean validation event listener is
being activated. If I remove the constraint annotation, I don't have a problem - the
test passes. This is the exception I get:
ERROR AssertionFailure:45 - an assertion failure occured (this may indicate a bug in
Hibernate, but is more likely due to unsafe use of the session)
org.hibernate.AssertionFailure: collection [jawbs.domain.jobseeker.JobSeeker.watchedJobs]
was not processed by flush()
at org.hibernate.engine.CollectionEntry.postFlush(CollectionEntry.java:228)
at
org.hibernate.event.def.AbstractFlushingEventListener.postFlush(AbstractFlushingEventListener.java:356)
at
org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1028)
at jawbs.DatabaseTests.flush(DatabaseTests.java:89)
Here is the code that causes the exception - at the call to
sessionFactory.getCurrentSession().flush() (which just happens to be flush() in this test
method).
@Test
public void jobSeekersCanSaveAPreviousResumeWithTheSameName() {
JobSeekerResume resume = ( JobSeekerResume ) resumeDao.find( 1 );
resume.setDescription( "new desc." );
resumeDao.save( resume );
flush();
jdbcMap = simpleJdbcTemplate.queryForMap(
"select * from resume where resume_id = ?", resume.getId() );
assertEquals( resume.getDescription(), jdbcMap.get( "DESCRIPTION" ) );
}
I don't think the mappings are really that important. Basically,
"JobSeekerResume" contains a "JobSeeker", and JobSeeker contains many
collections. Every time I run the test, I randomly get this error for any of the
collections within the JobSeeker object. In the exception above, it just happened to be
the "watchedJobs" collection property. It changes every time I run the test.
Now, the reason this problem is happening is that my constraint also hits the database.
The constraint asks the sessionFactory to open a session, start a transaction, execute a
query, close the transaction and then finally close the session. I've also tried it
with a StatelessSession instead, but the same problem persists. Here is the query:
select count(*) from JobSeekerResume where name = :name and id != :id and jobSeeker.id =
:jobSeekerId
Here is the constraint code:
public boolean isValid( DomainObject domainObject, ConstraintValidatorContext context )
{
BeanWrapper beanWrapper = new BeanWrapperImpl( domainObject );
SessionFactory sessionFactory =
( SessionFactory ) ApplicationContextProvider.getBean( "sessionFactory" );
if( enabled && sessionFactory != null ) {
logger.debug( "Enabled - Validating constraint with: " );
logger.debug( hql );
StatelessSession session = sessionFactory.openStatelessSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery(
HqlParser.removePeriodsFromParameterNames( hql )
);
for( String parameterName : HqlParser.getParameterNames( hql ) ) {
query.setParameter(
HqlParser.removePeriodsFromParameterName( parameterName ),
beanWrapper.getPropertyValue( parameterName )
);
}
boolean result = (Long) query.uniqueResult() == 0;
logger.debug( "isValid is returning: " + result );
tx.commit();
session.close();
return result;
}
return true;
}
Now, this query is 100% fine in isolation... and by using logging information, everything
is working - the query, the sessions are getting closed, etc. as well.
It is curious though that if I disable this constraint, the test passes. With this
constraint... it does not.
Is this a problem with my constraint? With the bean validation event listener?
I'd appreciate any help... and if it is a bug, that it gets fixed. Thank you.
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://opensource.atlassian.com/projects/hibernate/secure/Administrators....
-
For more information on JIRA, see:
http://www.atlassian.com/software/jira