-->
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.  [ 2 posts ] 
Author Message
 Post subject: findByExample problem
PostPosted: Mon Jul 30, 2007 11:45 am 
Beginner
Beginner

Joined: Wed Jul 25, 2007 6:32 am
Posts: 22
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 ^^


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 30, 2007 3:36 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Query by example only works with basic property fields, not with entities. You'll have to add a new DAO method for this.


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