HHH-12875 : Class level where="..." clause in hbm.xml mappings is not enforced on collections of that class

This commit is contained in:
Gail Badner 2018-08-01 15:01:25 -07:00
parent eef8974877
commit 10826d8f3a
2 changed files with 25 additions and 4 deletions

View File

@ -3494,7 +3494,20 @@ public class ModelBinder {
getCollectionBinding().setElement( elementBinding );
getCollectionBinding().setManyToManyWhere( elementSource.getWhere() );
final StringBuilder whereBuffer = new StringBuilder();
final PersistentClass referencedEntityBinding = mappingDocument.getMetadataCollector()
.getEntityBinding( elementSource.getReferencedEntityName() );
if ( StringHelper.isNotEmpty( referencedEntityBinding.getWhere() ) ) {
whereBuffer.append( referencedEntityBinding.getWhere() );
}
if ( StringHelper.isNotEmpty( elementSource.getWhere() ) ) {
if ( whereBuffer.length() > 0 ) {
whereBuffer.append( " and " );
}
whereBuffer.append( elementSource.getWhere() );
}
getCollectionBinding().setManyToManyWhere( whereBuffer.toString() );
getCollectionBinding().setManyToManyOrdering( elementSource.getOrder() );
if ( !CollectionHelper.isEmpty( elementSource.getFilterSources() )

View File

@ -514,12 +514,20 @@ public class ParentChildTest extends LegacyTestCase {
assertTrue( s.createCriteria(Part.class).list().size()==1 ); //there is a where condition on Part mapping
assertTrue( s.createCriteria(Part.class).add( Restrictions.eq( "id", p1.getId() ) ).list().size()==1 );
assertTrue( s.createQuery("from Part").list().size()==1 );
assertTrue( s.createQuery("from Baz baz join baz.moreParts").list().size()==2 );
// only Part entities that satisfy the where condition on Part mapping should be included in the collection
assertTrue( s.createQuery("from Baz baz join baz.moreParts").list().size()==1 );
baz = (Baz) s.createCriteria(Baz.class).uniqueResult();
assertTrue( s.createFilter( baz.getMoreParts(), "" ).list().size()==2 );
// only Part entities that satisfy the where condition on Part mapping should be included in the collection
assertTrue( s.createFilter( baz.getMoreParts(), "" ).list().size()==1 );
//assertTrue( baz.getParts().size()==1 );
s.delete( s.get( Part.class, p1.getId() ));
s.delete( s.get( Part.class, p2.getId() ));
// p2.description does not satisfy the condition on Part mapping, so it's not possible to retrieve it
// using Session#get; instead just delete using a native query
s.createNativeQuery( "delete from baz_moreParts where baz = :baz AND part = :part" )
.setParameter( "baz", baz.getCode() )
.setParameter( "part", p2.getId() )
.executeUpdate();
s.createNativeQuery( "delete from Part where id = :id" ).setParameter( "id", p2.getId() ).executeUpdate();
s.delete(baz);
t.commit();
s.close();