-->
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.  [ 5 posts ] 
Author Message
 Post subject: problems with FindByExample
PostPosted: Fri Aug 01, 2008 1:53 am 
Newbie

Joined: Thu Jul 31, 2008 3:35 pm
Posts: 2
hi, i have two tables, similar to the classic Products and Departments, where a field in the first table is a foreign key for the other table.
the tables are generated using myeclipse 6.5 tools.
I need to make a list of all the products for the same departments, and i'm trying using the findbyexamples method this way

ProductsDAO prodDao = new ProductsDAO();
DepartmentsDAO depDao = new DepartmentsDAO();

Departments exampleDep = depDao.findById(departmentId);

Products example = new Products();
example.setDepartments(exampleDep);

List<Products> record = prodDao.findByExample(example);

but i have a problem: whatever value i use for departmentid, all the records are returned, but the data are stored correctly.
how can i solve this problem?

best regards,
luca morelli


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 01, 2008 2:58 am 
Newbie

Joined: Wed Jul 30, 2008 7:48 am
Posts: 10
Location: NOIDA
can you write your query string used in prodDao.findByExample(example).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 01, 2008 3:34 am 
Newbie

Joined: Thu Jul 31, 2008 3:35 pm
Posts: 2
public List findByExample(Departments instance) {
log.debug("finding Proucts instance by example");
try {
List results = getSession().createCriteria("Departments").add(
Example.create(instance)).list();
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}

this is the method generated by myeclipse


Top
 Profile  
 
 Post subject: Re: problems with FindByExample
PostPosted: Fri Aug 01, 2008 4:40 am 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
g-loc wrote:
hi, i have two tables, similar to the classic Products and Departments, where a field in the first table is a foreign key for the other table.
[..]
but i have a problem: whatever value i use for departmentid, all the records are returned, but the data are stored correctly.
how can i solve this problem?


Don't use example queries, because it says in the documentation that it ignores any references:

Quote:
Version properties, identifiers and associations are ignored.


right here:

http://www.hibernate.org/hib_docs/core/ ... a-examples

But it should work with a eq Restriction.

_________________
Please rate useful posts.


Schauderhaft: Softwaredevelopment, Projectmanagement, Qualitymanagement and all things "schauderhaft"


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 01, 2008 6:09 am 
Newbie

Joined: Wed Jul 30, 2008 7:48 am
Posts: 10
Location: NOIDA
Don't use criteria.
You have two pojos as Products and Departments.
In Products pojo you have the reference of Departments table like below :-

public class Products
{
Departments department = null;
// any more attributes
// you have the setters getters for all the attributes including department
}




Try to execute below function:-
public List findByExample(Departments instance) {


Query query = getSession().createQuery(" from Products where Products.getDepartmet.getDepartmentId()=?");
query.setParameter(1,new Long(instance.getDepartment().getDepartmentid()));
List results = query.list();
Iterator itr = results.iterator();
Products prod = null;
while(itr.hasNext())
{
prod = (Products) itr.next();
// here you will get the desired o/p
}
}
}

if still you have facing problem, then let me know..


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