-->
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.  [ 3 posts ] 
Author Message
 Post subject: Query by Example with associations
PostPosted: Fri Jun 10, 2005 6:19 pm 
Newbie

Joined: Fri Jun 10, 2005 5:52 pm
Posts: 2
Hi,

I'm not completely clear on this, but from the debugging I've done, and from looking at this forum, it appears that query by example doesn't pay attention to associations. So if I do:


Code:
MyFoo foo = new MyFoo();
MyBar bar = new MyBar();

foo.setID(1);
bar.setFoo(foo);

Example ex = Example.create(bar);
List bars = dbSession.createCriteria(bar.getClass()).add(ex).list();


where the mappings for MyFoo and MyBar are set up appropriately, with a many-to-one association between bar and foo, then I will to get all the "MyBars", not just the ones associated with the MyFoo with ID 1.

Is it true that this doesn't work, or am I just getting it wrong? If it doesn't work, I assume this is by design; in which case, how should I go about achieving what I want to here? I am eager to avoid having to hardcode a query on the particular association, because this seems to break some of the encapsulation that Hibernate offers - I would like to be able to take any object for which I have created mappings and pass it to Hibernate, saying "Find me everything that looks a bit like this", without having to write special-case code for every different entity I search for. I have noticed that someone has already tried to address this by creating a new AssociationExample (See this post for details)

Is this misguided? Is there a good reason why this hasn't been done in Hibernate already, or is it just that this functionality hasn't been implemented yet? Have I just misunderstood entirely, and is there a much better way of achieving the encapsulation I'm looking for here?

Thanks in advance,

Lucian


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 11, 2005 11:38 am 
Senior
Senior

Joined: Thu May 12, 2005 11:40 pm
Posts: 125
Location: Canada
It has been my experience that you can't do this. As an alternative, write a criteria query:

MyFoo foo = new MyFoo();
MyBar bar = new MyBar();

foo.setID(1);
bar.setFoo(foo);

Example ex = Example.create(bar);
List bars = dbSession.createCriteria(bar.getClass())
.add(ex)
.add(Restrictions.eq("foo", foo)
//other association restrictions here
.list();

The restriction is only included if it's not null, so while you do have to list them all explicitly, it's not that big of a deal because you don't have to create a different criteria for every possible combination of associations.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 11, 2005 6:02 pm 
Newbie

Joined: Fri Jun 10, 2005 5:52 pm
Posts: 2
Thanks for your reply; I suspected that this would probably be the answer. In my naively idealistic enthusiasm I had rather hoped that it would be possible to just pass Hibernate any object for which it had mappings and get it to "do the right thing". The enormous advantage of Query by Example from my point of view is that one doesn't need to write code like:
Code:
criteria.add(Restrictions.eq("foo", foo);

The problem with that sort of code is that you have to rewrite it for every object, and every association, despite the fact that the semantics of searching across associations are often very similar. Having played with things a bit more, however, I am beginning to understand the complexities of having a generic mechanism here: the behaviour when the associated "foo" is already persistent are very different to if is not, and a generic query mechanism will have to make quite a lot of assumptions. Once you start getting into many-to-many associations it all becomes even more complex, of course.... Still, I think there a quite a few applications in which having a persistence layer that allows you to do:
Code:
List matching = persistenceLayer.findObjects(Object prototype);

for any object that has an appropriate mapping file, and have it "just work", whether the criteria are actually on the prototype itself or its associated objects, would be an incredibly powerful model that would take the encapsulation of persistence to a whole new level....


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