-->
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.  [ 6 posts ] 
Author Message
 Post subject: Filter and associations
PostPosted: Thu Apr 26, 2007 7:22 pm 
Newbie

Joined: Thu Apr 26, 2007 7:14 pm
Posts: 3
Hi,

I'm trying to use hibernate filters to filter data per user. Using this mapping file:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping>

<class
    name="MyClass"
    table="MyTable"
    lazy="true"
>

    <id
        name="idMyTable"
        type="java.lang.Integer"
        column="idMyTable"
    >
        <generator class="native" />
    </id>

    <!-- Associations -->
    <many-to-one
        name="serviceInstance"
        class="ServiceInstance"
        not-null="true"
    >
        <column name="idServiceInstance" />
    </many-to-one>

    <filter name="myClients" condition="(serviceInstance.idClient IN (:clients))"/>


</class>

<filter-def name="myClients">
    <filter-param name="clients" type="int"/>
</filter-def>

</hibernate-mapping>



I then use this code:

Code:
session.enableFilter("myClients").setParameterList("clients", user.getAllowedClients());


When I try to run a query, it replies something like:

Code:
Unknown column 'serviceInstance.idClient' in 'where clause'"


Shouldn't hibernate make the join? How can I do this?

Regards,
Gustavo Ambrozio


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 26, 2007 8:38 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
I'm not positive, but you can try this:
Code:
<filter name="myClients" condition="(this.serviceInstance.idClient IN (:clients))"/>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 27, 2007 4:10 am 
Regular
Regular

Joined: Mon Mar 26, 2007 12:38 am
Posts: 119
Hi,
>>> Shouldn't hibernate make the join? How can I do this?
Whether Hibernate should make a join is based on "fetch" strategy for the association and not on presence of filters.

This is what I understand about filters,
i) class level filters are bound to columns of the underlying table.
ii) collection filters are bound to columns of the peer table.

But, in this case, we are trying to refer peer table columns in class level filter. I think this is not possible.

---------------------------------------------------------
Rate the reply if you find it helpful


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 27, 2007 4:23 am 
Newbie

Joined: Thu Apr 05, 2007 7:26 am
Posts: 3
As I understand, you should write an SQL query in condition attribute. something like
idServiceInstance in (select id from *service_instance_table* where *idClientColumn* in (:clients))


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 27, 2007 9:17 am 
Newbie

Joined: Thu Apr 26, 2007 7:14 pm
Posts: 3
Ananasi wrote:
I'm not positive, but you can try this:
Code:
<filter name="myClients" condition="(this.serviceInstance.idClient IN (:clients))"/>


Didn't work:

Unknown column 'this.serviceInstance.idClient' in 'where clause'


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 27, 2007 9:35 am 
Newbie

Joined: Thu Apr 26, 2007 7:14 pm
Posts: 3
North wrote:
As I understand, you should write an SQL query in condition attribute. something like
idServiceInstance in (select id from *service_instance_table* where *idClientColumn* in (:clients))


WORKED!. Here's the working filter:

Code:
    <filter name="myClients" condition="(SELECT c.idClient FROM ServiceInstance c where c.idServiceInstance=idServiceInstance) IN (:clients)"/>



You have to use an alias insede the sub-query, otherwise hibernate will prefix every field with the outside table and the query won't work.

Thanks everyone!


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