-->
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.  [ 5 posts ] 
Author Message
 Post subject: Named filter usage with collection and wrong SQL
PostPosted: Thu May 18, 2006 12:37 pm 
Newbie

Joined: Thu May 18, 2006 12:23 pm
Posts: 6
I have the following example code:

Code:
---- package-info.java -----
@org.hibernate.annotations.FilterDef(name = "filter1", defaultCondition = "5 > myInteger")
package test.hibernate;

-------------
@Entity
public class A {
...
    @OneToMany(cascade = CascadeType.ALL)
    @Filter(name = "filter1")
    public Set<C> getCs() {
...
}

--------------
@Entity
public class C {
...
    public int getMyInteger() {
...
}

------ test code --------
...
        AnnotationConfiguration config = new AnnotationConfiguration();
        config.setProperties(props);
        config.addPackage("test.hibernate");
        config.addAnnotatedClass(A.class);
        config.addAnnotatedClass(C.class);
...
        session.enableFilter("filter1");
        A a = (A) session.load(A.class, 1);
        System.out.println(a.getCs().size());
...


The problem is that fetching of "cs" collection with filter enabled generates wrong SQL:
Code:
    select
        cs0_.A_ID as A1_1_,
        cs0_.cs_ID as cs2_1_,
        c1_.ID as ID2_0_,
        c1_.myInteger as myInteger2_0_,
        c1_.myString as myString2_0_
    from
        A_C cs0_
    left outer join
        C c1_
            on cs0_.cs_ID=c1_.ID
    where
        5 > cs0_.myInteger
        and cs0_.A_ID=?


That is definitely wrong "cs0_.myInteger ", it should be "c1_.myInteger".
I tried HSQL and MySQL 4.1, Hibernate 3.1 + Annotations 3.1beta8 and Hibernate 3.2CR2 + Annotations 3.2.0CR1. Problem is still there.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 18, 2006 6:38 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
This is not yet supported in Hibernate Annotations http://opensource.atlassian.com/projects/hibernate/browse/ANN-346

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 19, 2006 7:15 am 
Newbie

Joined: Thu May 18, 2006 12:23 pm
Posts: 6
Ok, it's clear, thank you.
I've tried to create quick fix. It seems as working but I'm not sure that it covers all possible mappings and, from other hand, doesn't create side effects...
Code:
...
        AnnotationConfiguration config = new AnnotationConfiguration();
        config.setProperties(props);
        config.addPackage("test.hibernate");
        config.addAnnotatedClass(A.class);
        config.addAnnotatedClass(B.class);
        config.addAnnotatedClass(C.class);

        // This is bug workaround
        config.buildMappings();
        Iterator iter = config.getCollectionMappings();
        while (iter.hasNext()) {
            Collection col = (Collection) iter.next();
            if (!col.isOneToMany()) {
                col.getManyToManyFilterMap().putAll(col.getFilterMap());
                col.getFilterMap().clear();
            }
        }

        SessionFactory sessionFactory = config.buildSessionFactory();
...


If it is possible to check for you please inform me about issues that might be present.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 19, 2006 12:41 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
use collection.addManyToManyFilter() instead
That sort of a fix, but the think is that some people actually need a filter capability on the association table itself, so I need to keep the current feature while adding the new one

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 22, 2006 5:16 am 
Newbie

Joined: Thu May 18, 2006 12:23 pm
Posts: 6
Yes, sure, you're right. For maps and lists it would be useful sometimes to filter by index information itself...

BTW: I think my fix would break composite element collections. It should be more strict check:
Code:
if (col.getElement() instanceof ManyToOne) {
...

instead of:
Code:
if (!col.isOneToMany()) {
...

AFAIK Hibernate itself (at least 3.1) behaves pretty the same way: for all collections mapped with <many-to-many> and <filter> inside collection tag it adds "many to many filters" and just "filters" for other collections. So, this functionality to have different kind of filters mapped is new for Hibernate Core too.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.