java - Hibernate Criteria searching request with One to Many relationship -


i've tried like:

session = hibernateutil.getsessionfactory().opensession(); criteria cr = session.createcriteria(car.class); cr.createalias("vendor", "vendor");   cr.add( restrictions.eq("vendor.name", input));  results = (list<car>) cr.list(); 

and like:

session = hibernateutil.getsessionfactory().opensession(); criteria cr = session.createcriteria(car.class); cr.createcriteria("vendor").add(restrictions.eq("name", input));  results = (list<car>) cr.list(); 

both realizations return data, not specified search query.

in car class i've got relationship:

@manytoone(fetch=fetchtype.lazy) @joincolumn(name="id_vendor", nullable=false) public vendor getvendor() {     return this.vendor; } 

and i've got thename column @ vendor class @ i'm trying search.

so how possibly such search request?

thanks.

you need assign criteria original object.

the reason cars because line of code adding vendor query creates new criteria object not using.

change:

session = hibernateutil.getsessionfactory().opensession(); criteria cr = session.createcriteria(car.class); cr.createcriteria("vendor").add(restrictions.eq("name", input));  //does nothing  results = (list<car>) cr.list(); 

into this:

session = hibernateutil.getsessionfactory().opensession(); criteria cr = session.createcriteria(car.class); cr = cr.createcriteria("vendor").add(restrictions.eq("name", input));  results = (list<car>) cr.list(); 

and should work.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -