i using hibernate in eclipse.
and have 3 1 many relation tables.
one [company] --> many [officer], , 1 [officer] --> many [task].
they have unique id (companyid, officerid, taskid).
currently know how find tasks belong officer , know how find officers belong company.
here code snippet:
public static arraylist<officer> getofficersbycompany(company company){ arraylist<officer> officers = new arraylist<officer>(); detachedcriteria detachedcriteria = detachedcriteria.forclass(officer.class); detachedcriteria.add(restrictions.eq(key.company, company)); detachedcriteria.add(restrictions.eq(key.objstatus, value.actived)); list<object> list = hibernateutil.detachedcriteriareturnlist(detachedcriteria); for(object o : list){ officers.add((officer) o); } return officers; } and below detachedcriteriareturnlist method in hibernateutil class.
public static list<object> detachedcriteriareturnlist(detachedcriteria dc){ session session = getsessionfactory().opensession(); session.begintransaction(); criteria criteria = dc.getexecutablecriteria(session); list<object> list = criteria.list(); session.gettransaction().commit(); session.close(); return list; } however, if try all task belongs company, how should implement code. have tried using: detachedcriteria.add(restrictions.alleq(officers));
public static arraylist<task> gettasksbyofficers(map<string, object> officers){ arraylist<task> tasks = new arraylist<task>(); detachedcriteria detachedcriteria = detachedcriteria.forclass(task.class); detachedcriteria.add(restrictions.alleq(officers)); list<object> list = hibernateutil.detachedcriteriareturnlist(detachedcriteria); for(object o : list){ tasks.add((task) o); } return tasks; } but realise map stores unique key , value pair, if try use second officer's id first 1 replaced.
or there other ways perform selection faster , more efficient?
one [company] --> many [officer], , 1 [officer] --> many [task].
in hql should pretty straight forward (i gave using criteria api, did not allow joining same object class twice):
obviously, did not test queries, should work...
select task task join task.officer o join o.company c c.name = 'xxx' or
select t company c join c.officers o join o.tasks t c.name = 'xxx' note: experience criteria limited, have said. looking @ code, have 2 comments. maybe should not use map of officers list officers.values() alleq(). second remark: if didn't mistake, hibernate magically finds reference property want alleq with, otherwise, alleq misses property want compare to.
update: ok, promised, checked javadocs you: https://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/criteria.html
a join in hql equivalent of criteria added criteria or alias:
detachedcriteria = detachedcriteria.forclass(task.class) .createcriteria("officer") // officer property of task .createcriteria("company") // company property of officer .add(restriction.eq(key.company, compkey); // company list<object> list = detachedcriteria .getexecutablecriteria(hibernatesession) .list(); this should going...
note on hql:
to run query, query hibernate session hql , call list() or executeupdate()on it. taskhqlstring hql-statement above. replace companykey in query named parameter :companykey:
string taskhqlstring = "select task " + " task join task.officer o " + " join o.company c " + " c.name = :companykey"; list<task> list = (list<task>)hibernatesession .createquery(taskhqlstring) .setparameter("companykey", companykeyvalue) .list();
Comments
Post a Comment