-->
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.  [ 12 posts ] 
Author Message
 Post subject: "where" attribuge in mapping file being ignored
PostPosted: Wed Sep 03, 2003 6:34 pm 
Newbie

Joined: Thu Aug 28, 2003 2:22 pm
Posts: 5
I am trying to get the "where" attribute of the class descriptor in the mapping file to work in Hibernate2. I added this to my class descriptor:

Code:
<class ...
    where="material_id = 3"
/>


I turned on SQL display and watched as I tried to select on the material table. No dice. The where clause didn't show up.

I removed my mapping file, to see if it was working on the right one, and it barfed, as it should have.

I changed the where clause to "allyourbasebelongtous" to see if it would barf on the bad SQL, but nothing happened.

I restarted the JBoss server each time.

Am I doing something wrong? Does this feature need to be "turned on", or is it not yet implemented?

Thanks,
Ken


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 03, 2003 9:04 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Which *exact* version of Hibernate are you using?


And note that this is only used in a *query*, not in load(), for example.


Top
 Profile  
 
 Post subject: version
PostPosted: Thu Sep 04, 2003 1:08 am 
Newbie

Joined: Thu Aug 28, 2003 2:22 pm
Posts: 5
I believe it's 2.0.2 - I downloaded it two weeks ago, and it was the newest version available at that time that wasn't a beta release.

The situation in which I'm using it is a many-to-many relationship. There is a bundle object which is related to a set of materials, through a bundle_materials correlation table. When I iterate through the collection of materials, I would expect the where clause to be executed on them. Is it perhaps calling a load() on each individual item in the collection, therefore not invoking the where clause?

Thanks,
Ken


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 04, 2003 1:20 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
The where clause is not used at all during association navigation. I suspect you might be able to argue that it should be used for navigating many-to-many associations. If you think so, sibmit a feature request to JIRA and I'll give it some thought.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 29, 2005 11:33 am 
Beginner
Beginner

Joined: Sat Sep 17, 2005 6:50 am
Posts: 23
And is there any possibility to define where-constraints for <many-to-one> association? Maybe with the help of <filter> ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 29, 2005 11:41 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
The constraint for a many-to-one is called "foreign key". Everything else wouldn't be a many-to-one.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 29, 2005 12:36 pm 
Beginner
Beginner

Joined: Sat Sep 17, 2005 6:50 am
Posts: 23
christian wrote:
The constraint for a many-to-one is called "foreign key". Everything else wouldn't be a many-to-one.

Could you please give a hint for such a task:
- I have articles, each article belongs exactly to one category
- Each category has a flag "hidden", which means, it is not accessable
- I want to list all accessable articles:

Code:



Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 29, 2005 12:48 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
select a from Articles a join a.category c where c.hidden = false


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 29, 2005 2:59 pm 
Newbie

Joined: Wed Sep 08, 2004 1:23 pm
Posts: 5
christian wrote:
The constraint for a many-to-one is called "foreign key". Everything else wouldn't be a many-to-one.


I would agree with this statement, however having the ability to append arbitrary sql to all queries (including association and primary key queries) would still be useful as a mechanism for creating "dynamic views" on audit history tables.

The ability not just to save audit history but also to load historical versions into domain objects is a comon requirement in the world of derivatives trading systems. Every system I know of that has solved this problem uses a proprietary ORM layer, since standard solutions do not exist.

Although with Hibernate it is possible to use views and triggers to save audit history, as far as I am aware there is no way to load a version as of a particular date in history. However I believe it should be possible with some modifications.

The assumption is that the entities being versioned have both Version and LastUpdated properties, and that two parallel sets of tables exist in the database (one holding the latest version of each entity, and one holding all versions). The history tables are maintained via triggers on the primary tables.

To query on the history tables, the user would create a new SessionFactory with the desired as-of date. When configuring this factory each versioned entity is mapped to its history table, with some additional filter sql to select the last version as-of the specified date.

So if a normal query looks like:

select <...> from Foo f where f.Id=42

the corresponding history query would look like:

select <...> from Foo_history f where f.Id=42 inner join
(select Id, max(Version) as Version from Foo_history
where LastUpdated<='12/31/2004' group by Id) as fh
on f.Id=fh.Id and f.Version=fh.Version

It seems to me that anytime you step into the world of audit history, you end up going outside the bounds of strict relational algebra. However, in the real world (if financial derivatives can be considered the real world) the ability to extend the ORM mappings to audit history is an important requirement.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 29, 2005 3:35 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
This stuff can be solved using Hibernate3 filters.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 29, 2005 3:55 pm 
Newbie

Joined: Wed Sep 08, 2004 1:23 pm
Posts: 5
gavin wrote:
This stuff can be solved using Hibernate3 filters.


Interesting. I haven't looked much into Hibernate events as I am using NHibernate. If anybody has an example of using events in this way I would certainly be interested in learning more.

In any case, thanks for the response.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 06, 2005 6:31 am 
Beginner
Beginner

Joined: Sat Sep 17, 2005 6:50 am
Posts: 23
gavin, thanks for the hint, conc hibernate filters. Still I suffer difficulties with filtering many-to-one associated entities.

I have articles, each article belongs exactly to one category
- Each category has a flag "active", which means, it is not available
- I want to list all available articles
- I don't want the "active" field to be mapped

Mapping for article:
Code:
<hibernate-mapping default-lazy="false">
   <class
      name="ArticleImpl"
      table="articles" mutable="false"
   >
      <id name="id" type="int">
         <generator class="native"/>
      </id>

      <property name="code" type="java.lang.String"/>

      <many-to-one
         name="catalog" column="catalogId" outer-join="true"
         class="CatalogImpl"
      />

      <!-- other properties -->

      <filter name="activeOnlyCatalog" condition="catalogs.active = 1"/>

   </class>

   <filter-def name="activeOnlyCatalog"/>

</hibernate-mapping>

Mapping for catalog:
Code:
<hibernate-mapping>
   <class
      name="CatalogImpl"
      table="catalogs" mutable="false"
   >
      <id name="id" column="id" type="int">
         <generator class="native"/>
      </id>

      <property name="title" type="java.lang.String"/>

      <!-- other properties -->
   </class>
</hibernate-mapping>

Code:
Code:
   session = getHibenateSessionFactory().openSession();
   session.setFlushMode(FlushMode.NEVER);
   session.enableFilter("activeOnlyCatalog");
   Collection result = session.createCriteria(ArticleImpl.class).list();

Resulting SQL:
Code:
INFO  [STDOUT] Hibernate: select this_.id as id1_, ... from articles this_ left outer join catalogs catal2_ on this_.catalogId=catal2_.id where catalogs.active = 1
ERROR [JDBCExceptionReporter] Unknown table 'catalogs' in where clause

I have also tried not to define the table name:
Code:
<filter name="activeOnlyCatalog" condition="active = 1"/>

INFO  [STDOUT] Hibernate: select this_.id as id1_, ... from articles this_ left outer join catalogs catal2_ on this_.catalogId=catal2_.id where this_.active = 1

and to replace the table name with property name (catalog.active = 1), class name (CatalogImpl.active = 1) -- nothing helped (the table alias is not substituted).

Please, give a hint, how a filter can be properly defined.


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