Java Hibernate Criteria Many-To-One -
i trying use hibernate many-to-one relationship, shown below:
i have service table , each service, have programid.
public class service { ... @manytoone @joincolumn(name="program_lv_id", referencedcolumnname = "id") private program program; }
in bd, have records:
service table
id = 1, programid = 2
id = 2, programid = 2
id = 3, programid = 3
program table
id = 2, name = "program2"
id = 3, name = "program3"
i trying like:
public list<service> getservicesforprogram(long id) { criteria criteria = getsession().createcriteria(service.class, "s"); criteria.createalias("s.program", "p"); criteria.add(restrictions.eq("p.id", id)); return (list<service>)criteria.list(); }
when pass 2 method, 4 records instead of 2. somehow results duplicated , services ids 1 , 2 twice.
can me why duplication happening?
all appreciated.
thanks.
your query in plain sql like:
select * service p, program s.id = 1;
it combines both records service , program id = 1 in service table.
and hence if there 2 records in program table(where primary key 1 , 2), , 2 in service(where foreign key 1), fetch 4 rows[2 x 2].
to fetch records, can use distinct criteria as:
criteria cr = getsession().createcriteria(service.class); cr.setresulttransformer(criteria.distinct_root_entity);
Comments
Post a Comment