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