Ok, let's look at the following example. I got two entities (stored in seperate
database tables):
- Boss
- Worker
Every worker has a boss (or vice versa: every boss has several workers *g*)
Sample data:
Boss Miranda and her employees Charlotte, Carrie and Samantha
Boss Egon and his employees Raymond, Luis and Peter
Now when looking at the detail page of a boss I want to see his/her workers in
a list.
I tried via Hibernate's QUERY BY EXAMPLE. I generate a dummy worker and set its
boss property to the currently viewd boss. Then I pass this dummy worker to the
WorkerDAO. But instead of giving me all the workers that are associated with
this boss, the DAO returns me ALL workers:
BossForm.java:
--------------
....
public List getWorkers() {
Worker exampleWorker = new Worker();
exampleWorker.setBoss(boss); //where boss is the currently viewed boss
return workerManager.getWorkers(exampleWorker);
}
WorkerManager.java: delegates to WorkerDaoHibernate.java
-------------------
WorkerDaoHibernate.java:
------------------------
....
public List getWorkers(final Worker worker) {
if (worker = null)
balabla
else { //this is where we go
HibernateCallback callback = new HibernateCallback() {
public Object doInHibernate(Session session) {
Example ex = Example.create(worker).ignoreCase().enableLike
(MatchMode.ANYWHERE);
return session.createCriteria(Worker.class).add(ex).addOrder
(Order.asc("name")).list();
}
};
return (List) getHibernateTemplate().execute(callback);
}
}
So, what's wrong with that? When I switch Hibernate debugging on, I see the
following query:
SELECT blablabla FROM worker this_ WHERE (1=1) ORDER BY this_.name ASC
Errrr.... WHERE (1=1)? Come on ^^
|