HHH-12875 HHH-12882 : Class level where="..." clause hbm.xml mappings is not enforced on collections of that class; add parentheses when where clauses get combined in a conjunction (hbm and annotations)
HHH-12882 : correct assertions in ParentChildTest
(cherry picked from commit fa604bdc36
)
This commit is contained in:
parent
0b05fc7e1b
commit
36c2d6dea4
|
@ -1518,7 +1518,6 @@ public class ModelBinder {
|
|||
binding.getSynchronizedTables().add( name );
|
||||
}
|
||||
|
||||
binding.setWhere( source.getWhere() );
|
||||
binding.setLoaderName( source.getCustomLoaderName() );
|
||||
if ( source.getCustomSqlInsert() != null ) {
|
||||
binding.setCustomSQLInsert(
|
||||
|
@ -3466,9 +3465,15 @@ public class ModelBinder {
|
|||
|
||||
final PersistentClass referencedEntityBinding = mappingDocument.getMetadataCollector()
|
||||
.getEntityBinding( elementSource.getReferencedEntityName() );
|
||||
collectionBinding.setWhere(
|
||||
StringHelper.getNonEmptyOrConjunctionIfBothNonEmpty(
|
||||
referencedEntityBinding.getWhere(),
|
||||
getPluralAttributeSource().getWhere()
|
||||
)
|
||||
);
|
||||
|
||||
elementBinding.setReferencedEntityName( referencedEntityBinding.getEntityName() );
|
||||
elementBinding.setAssociatedClass( referencedEntityBinding );
|
||||
|
||||
elementBinding.setIgnoreNotFound( elementSource.isIgnoreNotFound() );
|
||||
}
|
||||
else if ( getPluralAttributeSource().getElementSource() instanceof PluralAttributeElementSourceManyToMany ) {
|
||||
|
@ -3568,7 +3573,10 @@ public class ModelBinder {
|
|||
}
|
||||
);
|
||||
|
||||
elementBinding.setLazy( elementSource.getFetchCharacteristics().getFetchTiming() != FetchTiming.IMMEDIATE );
|
||||
elementBinding.setLazy(
|
||||
elementSource.getFetchCharacteristics()
|
||||
.getFetchTiming() != FetchTiming.IMMEDIATE
|
||||
);
|
||||
elementBinding.setFetchMode(
|
||||
elementSource.getFetchCharacteristics().getFetchStyle() == FetchStyle.SELECT
|
||||
? FetchMode.SELECT
|
||||
|
@ -3588,20 +3596,16 @@ public class ModelBinder {
|
|||
|
||||
getCollectionBinding().setElement( elementBinding );
|
||||
|
||||
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() );
|
||||
|
||||
final PersistentClass referencedEntityBinding = mappingDocument.getMetadataCollector().getEntityBinding(
|
||||
elementSource.getReferencedEntityName()
|
||||
);
|
||||
getCollectionBinding().setWhere( getPluralAttributeSource().getWhere() );
|
||||
getCollectionBinding().setManyToManyWhere(
|
||||
StringHelper.getNonEmptyOrConjunctionIfBothNonEmpty(
|
||||
referencedEntityBinding.getWhere(),
|
||||
elementSource.getWhere()
|
||||
)
|
||||
);
|
||||
getCollectionBinding().setManyToManyOrdering( elementSource.getOrder() );
|
||||
|
||||
if ( !CollectionHelper.isEmpty( elementSource.getFilterSources() )
|
||||
|
|
|
@ -945,36 +945,27 @@ public abstract class CollectionBinder {
|
|||
}
|
||||
}
|
||||
|
||||
StringBuilder whereBuffer = new StringBuilder();
|
||||
String whereOnClassClause = null;
|
||||
if ( property.getElementClass() != null ) {
|
||||
Where whereOnClass = property.getElementClass().getAnnotation( Where.class );
|
||||
if ( whereOnClass != null ) {
|
||||
String clause = whereOnClass.clause();
|
||||
if ( StringHelper.isNotEmpty( clause ) ) {
|
||||
whereBuffer.append( clause );
|
||||
}
|
||||
whereOnClassClause = whereOnClass.clause();
|
||||
}
|
||||
}
|
||||
Where whereOnCollection = property.getAnnotation( Where.class );
|
||||
String whereOnCollectionClause = null;
|
||||
if ( whereOnCollection != null ) {
|
||||
String clause = whereOnCollection.clause();
|
||||
if ( StringHelper.isNotEmpty( clause ) ) {
|
||||
if ( whereBuffer.length() > 0 ) {
|
||||
whereBuffer.append( StringHelper.WHITESPACE );
|
||||
whereBuffer.append( Junction.Nature.AND.getOperator() );
|
||||
whereBuffer.append( StringHelper.WHITESPACE );
|
||||
}
|
||||
whereBuffer.append( clause );
|
||||
}
|
||||
whereOnCollectionClause = whereOnCollection.clause();
|
||||
}
|
||||
if ( whereBuffer.length() > 0 ) {
|
||||
String whereClause = whereBuffer.toString();
|
||||
if ( hasAssociationTable ) {
|
||||
collection.setManyToManyWhere( whereClause );
|
||||
}
|
||||
else {
|
||||
collection.setWhere( whereClause );
|
||||
}
|
||||
final String whereClause = StringHelper.getNonEmptyOrConjunctionIfBothNonEmpty(
|
||||
whereOnClassClause,
|
||||
whereOnCollectionClause
|
||||
);
|
||||
if ( hasAssociationTable ) {
|
||||
collection.setManyToManyWhere( whereClause );
|
||||
}
|
||||
else {
|
||||
collection.setWhere( whereClause );
|
||||
}
|
||||
|
||||
WhereJoinTable whereJoinTable = property.getAnnotation( WhereJoinTable.class );
|
||||
|
|
|
@ -856,4 +856,38 @@ public final class StringHelper {
|
|||
public interface Renderer<T> {
|
||||
String render(T value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param firstExpression the first expression
|
||||
* @param secondExpression the second expression
|
||||
* @return if {@code firstExpression} and {@code secondExpression} are both non-empty,
|
||||
* then "( " + {@code firstExpression} + " ) and ( " + {@code secondExpression} + " )" is returned;
|
||||
* if {@code firstExpression} is non-empty and {@code secondExpression} is empty,
|
||||
* then {@code firstExpression} is returned;
|
||||
* if {@code firstExpression} is empty and {@code secondExpression} is non-empty,
|
||||
* then {@code secondExpression} is returned;
|
||||
* if both {@code firstExpression} and {@code secondExpression} are empty, then null is returned.
|
||||
*/
|
||||
public static String getNonEmptyOrConjunctionIfBothNonEmpty( String firstExpression, String secondExpression ) {
|
||||
final boolean isFirstExpressionNonEmpty = StringHelper.isNotEmpty( firstExpression );
|
||||
final boolean isSecondExpressionNonEmpty = StringHelper.isNotEmpty( secondExpression );
|
||||
if ( isFirstExpressionNonEmpty && isSecondExpressionNonEmpty ) {
|
||||
final StringBuilder buffer = new StringBuilder();
|
||||
buffer.append( "( " )
|
||||
.append( firstExpression )
|
||||
.append( " ) and ( ")
|
||||
.append( secondExpression )
|
||||
.append( " )" );
|
||||
return buffer.toString();
|
||||
}
|
||||
else if ( isFirstExpressionNonEmpty ) {
|
||||
return firstExpression;
|
||||
}
|
||||
else if ( isSecondExpressionNonEmpty ) {
|
||||
return secondExpression;
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -478,12 +478,16 @@ 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.parts").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.parts").list().size()==1 );
|
||||
baz = (Baz) s.createCriteria(Baz.class).uniqueResult();
|
||||
assertTrue( s.createFilter( baz.getParts(), "" ).list().size()==2 );
|
||||
// only Part entities that satisfy the where condition on Part mapping should be included in the collection
|
||||
assertTrue( s.createFilter( baz.getParts(), "" ).list().size()==1 );
|
||||
//assertTrue( baz.getParts().size()==1 );
|
||||
s.delete( s.get( Part.class, p1.getId() ));
|
||||
s.delete( s.get( Part.class, p2.getId() ));
|
||||
s.delete( s.get( Part.class, p1.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.createSQLQuery( "delete from Part where id = :id" ).setParameter( "id", p2.getId() ).executeUpdate();
|
||||
s.delete(baz);
|
||||
t.commit();
|
||||
s.close();
|
||||
|
|
Loading…
Reference in New Issue