-->
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: Filtering a Set based on a subset
PostPosted: Thu Apr 26, 2007 7:21 pm 
Newbie

Joined: Tue Apr 11, 2006 5:55 am
Posts: 4
Hi,

I'm having a bit of a problem filtering out data in my query.

The scanarion is something like this... Think of the Employee example from the documentation, this is the mapping for it.

Quote:
Code:
<filter-def name="effectiveDate">
    <filter-param name="asOfDate" type="date"/>
</filter-def>

<class name="Employee" ...>
...
    <many-to-one name="department" column="dept_id" class="Department"/>
    <property name="effectiveStartDate" type="date" column="eff_start_dt"/>
    <property name="effectiveEndDate" type="date" column="eff_end_dt"/>
...
    <!--
        Note that this assumes non-terminal records have an eff_end_dt set to
        a max db date for simplicity-sake
    -->
    <filter name="effectiveDate"
            condition=":asOfDate BETWEEN eff_start_dt and eff_end_dt"/>
</class>

<class name="Department" ...>
...
    <set name="employees" lazy="true">
        <key column="dept_id"/>
        <one-to-many class="Employee"/>
        <filter name="effectiveDate"
                condition=":asOfDate BETWEEN eff_start_dt and eff_end_dt"/>
    </set>
</class>



Now what I want to do is to create a 'Team' where all employees that belong to a Department would be present.

So when i create the Set of employees in my Team mapping, i want to filter out all the employees which do NOT have the specified department.
to do this I created a filter that looks like this

Code:
  <filter name="departmentFilter" condition=" EMPLOYEE_ID in (select distinct mapping.EMPLOYEE_ID from DEPARTMENT_MAPPING mapping where :departmentCriteria = mapping.department_id and EMPLOYEE_ID = mapping.EMPLOYEE_ID)" />


I'm not sure whether this is valid syntax for a filter, but the result is that no items are excluded.

O can I add a Criteria to the session? I cant figure out how to to add a 'contains' Restriction??

I havent been able to see any examples of doing this with filters.

My last resort would have to create a custom HQL query in my Java code, but i'm not really too keen on that.

Appreciate any help I can get on this.

// Johan
[b]Hibernate version: 3.1.2


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 27, 2007 3:00 am 
Regular
Regular

Joined: Mon Jan 22, 2007 10:32 am
Posts: 101
Hi,

It is not very clear from your post that what actually "Team" is? If it is a new class I think you can solve the issue simply by having a reference of Department object in Team object and then from department object you can get hold of all employees for that department.

I feel if you can provide more details, you will get an answer soon.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 27, 2007 3:50 am 
Newbie

Joined: Tue Apr 11, 2006 5:55 am
Posts: 4
Well, this is just an axample, i've really got a quite complex structure of related information that would take too long to dechipher, and it would just be a distraction from the core point...

I'll post my entire structure below (its only got the relevant parts to solve the problem 0% clutter ;) )

The problem is this, I want to put all employees that belong to a department, lets say Sales, into one team and all management into another. But since employees can belong to several departments (A Sales manager) serveral of them will have > 1 Department.

To ensure dataintegrity when an employee is loaded into a team the Employee object still has to maintain all its Departments. Otherwise an update would have some interesing side-effects. So i cannot just filter out any departments that does not match.

For the same reason I could not just put a department in the Team, as there should be Employees with >1 departmens present.

What i'd like to find out is if this filter has any real chance of working ;

Code:
      <filter name="departmentFilter"
            condition=" EMPLOYEE_ID in (select distinct dept.EMPLOYEE_ID from DEPARTMENT dept where :filterAttr = channel.DEPARTMENT_ID and EMPLOYEE_ID = dept.EMPLOYEE_ID)" />


Or if i have to write a custom query to do this. That would be no problem, but i'd really like to manage this within the hbm file and only expose a set of filters that developers can use.


This is my mapping, note the filter in the Employee Class..

Code:
<hibernate-mapping>
<!-- 'Team' object contains a list of employees filtered out on a departments   -->
   <class name="com.org.Team" table="TEAM" lazy="false" dynamic-update="true">
      <cache usage="read-write" />
      <id column="id" name="ID" type="long" unsaved-value="0">
         <generator class="increment"></generator>
      </id>
      <!--
         This set should contain a set of employees, filtered based on a given department.
         An employee that exist in the list has All their departments present.
      -->
      <set name="emloyees" inverse="true" cascade="all-delete-orphan" lazy="false">
         <key column="EMPLOYEE_ID" />
         <one-to-many class="com.org.Employee" />
      </set>

   </class>
   <!--
      'Employee' object
    -->
   <class name="com.org.Employee" table="employee" lazy="false" dynamic-update="true">
      <id column="EMPLOYEE_ID" name="id" type="long">
         <generator class="assigned"></generator>
      </id>
      <set name="departments" inverse="true"
         cascade="all-delete-orphan" lazy="false">
         <key column="DEPARTMENT_ID" />
         <one-to-many class="com.org.Department" />
      </set>
      <!--
         Filter out any Employee that does NOT have the desired department
      -->
      <filter name="departmentFilter"
            condition=" EMPLOYEE_ID in (select distinct dept.EMPLOYEE_ID from DEPARTMENT dept where :filterAttr = dept.DEPARTMENT_ID and EMPLOYEE_ID = dept.EMPLOYEE_ID)" />
   </class>

   <!--
      'Department' class, 
    -->
   <class name="com.org.Department" table="DEPARTMENT" lazy="false" dynamic-update="true">
      <cache usage="read-write" />
      <id column="DEPARTMENT_ID" name="cartItemChannelId">
         <generator class="assigned" />
      </id>
      <many-to-one name="employee" column="EMPLOYEE_ID" not-null="true" lazy="false" />
      <!--
         Applies no filters to departments, an employee will always have all their departments present.
       -->
   </class>

   <filter-def name="departmentFilter">
      <filter-param name="filterAttr" type="integer" />
   </filter-def>
</hibernate-mapping>



//Johan


Top
 Profile  
 
 Post subject: Resolved
PostPosted: Fri Apr 27, 2007 6:29 am 
Newbie

Joined: Tue Apr 11, 2006 5:55 am
Posts: 4
Ah,turns out the solution was closer than i realised..

The filter was just misplaced...
This filter is valid,
<filter name="departmentFilter"
condition=" EMPLOYEE_ID in (select distinct dept.EMPLOYEE_ID from DEPARTMENT dept where :filterAttr = channel.DEPARTMENT_ID and EMPLOYEE_ID = dept.EMPLOYEE_ID)" />


but it should go in the <Set> declaration of the Team mapping,
i.e.

Code:
    <set name="emloyees" inverse="true" cascade="all-delete-orphan" lazy="false">
         <key column="EMPLOYEE_ID" />
         <one-to-many class="com.org.Employee" />
<filter name="departmentFilter"
            condition=" EMPLOYEE_ID in (select distinct dept.EMPLOYEE_ID from DEPARTMENT dept where :filterAttr = channel.DEPARTMENT_ID and EMPLOYEE_ID = dept.EMPLOYEE_ID)" />
      </set>
.


//Johan


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.