-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Filters on Many-To-One association
PostPosted: Thu Jun 14, 2007 8:15 am 
Newbie

Joined: Thu Jun 14, 2007 4:46 am
Posts: 1
Hello,

I use Hibernate 3.2.4.sp1. My database model extensively exploits effective date pattern to filter active/inactive records. As described in hibernate documentation the hibernate filters are to be used in such occasions. It works fine with ManyToOne relationships, but only when fetch type is set to EAGER.

If the LAZY fetch strategy is used the filtering does not work and causes the ‘More than one row with the given identifier was found’ exception. Is there any way to avoid this behavior without turning on the EAGER strategy (which is not suitable in my situation)? I could not find any answer on this and other forums.

Few simple adjustment in EntityJoinWalker and QueryParameters solved the problem, but I’m not entirely sure if those changes will not affect the rest of hibernate functionality. Below is the list of changes:

// EntityJoinWalker.java (rev.7652), line 40
Code:
StringBuffer whereCondition = whereString( getAlias(), uniqueKey, batchSize )
         .append( persister.filterFragment( getAlias(), enabledFilters ) );

// QueryParameters (rev. 9636), lines 396-427
Code:
         List queryParamsPositions = new ArrayList();
         while ( tokens.hasMoreTokens() ) {
            final String token = tokens.nextToken();
            if ( token.startsWith( ParserHelper.HQL_VARIABLE_PREFIX ) ) {
               String filterParameterName = token.substring( 1 );
               Object value = session.getFilterParameterValue( filterParameterName );
               Type type = session.getFilterParameterType( filterParameterName );
               if ( value != null && Collection.class.isAssignableFrom( value.getClass() ) ) {
                  Iterator itr = ( ( Collection ) value ).iterator();
                  while ( itr.hasNext() ) {
                     Object elementValue = itr.next();
                     result.append( '?' );
                     parameters.add( elementValue );
                     parameterTypes.add( type );
                     if ( itr.hasNext() ) {
                        result.append( ", " );
                     }
                  }
               }
               else {
                  result.append( '?' );
                  parameters.add( value );
                  parameterTypes.add( type );
               }
            }
            else {
                if ("?".equals(token)){
                    queryParamsPositions.add(parameters.size());
                }
               result.append( token );
            }
         }
         for (int i = 0 ; i < queryParamsPositions.size() ; i++) {
             int currIndex = (Integer) queryParamsPositions.get(i);
             parameters.add(currIndex,getPositionalParameterValues()[i]);
             parameterTypes.add(currIndex, getPositionalParameterTypes()[i]);
         }
         if (queryParamsPositions.size() == 0) {
             parameters.addAll(Arrays.asList( getPositionalParameterValues() ) );
             parameterTypes.addAll(Arrays.asList( getPositionalParameterTypes() ) );
         }
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 14, 2007 10:08 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Filters should _not work at all_ on a many-to-one, because that is conceptually impossible (the only thing a filter can do is return nothing, which violates the cardinality of the association, it would turn into a many-to-zero-or-one).

If you have a reproducible testcase (JUnit based on the Hibernate test suite preferred) of a filter example that filters a many-to-one, post it on JIRA. Do not post without a test case.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 15, 2007 6:52 am 
Newbie

Joined: Tue Jul 19, 2005 7:58 am
Posts: 3
[quote="christian"]Filters should _not work at all_ on a many-to-one, because that is conceptually impossible (the only thing a filter can do is return nothing, which violates the cardinality of the association, it would turn into a many-to-zero-or-one).
[/quote]

Are you so sure? I am sure that this is *conceptually justified* to use filters as a kind of global criteria or the contract of this functionality is misleadingly described in the Hibernate ref doc. Please keep in mind that:

a) there is nothing wrong in "breaking" cardinality of the association by the results of criteria queries - so what's wrong with filters that are said to be global filters?

b) as far as I understand the concept of filters they have been introduced to provide the functionality of easy navigation over temporal data (just like sample filter "effectiveDate" presented in the doc), but if you want to omit the filtering on many-to-one associations the whole idea of this use of filters just fails.

I can see two solutions:
1. Treat filters as global criteria and do not worry about the cardinality of associations (please!)
2. Change the contents of chapter 17.1 in the Hibernate doc and clearly state what are they provided for. But I am not sure if you will find some usable use case when filtering won't be populated over associations.


Top
 Profile  
 
 Post subject: Re: Filters on Many-To-One association
PostPosted: Wed Jun 02, 2010 1:54 pm 
Newbie

Joined: Tue Jun 01, 2010 1:32 pm
Posts: 2
Did you solved this problem??


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.