java - Wicket/Hibernate: get duplicated records in list from db -
i´m using wicket , hibernate. got 2 objects category
, group
. group
can have several categorys
, category
can have several groups
.
my problem (its pretty hard me explain in english): seems in list database equal objects size of categorys store grouping (while in database 1 group).
example:
categorys 1, 2, 3, 4
group test
test got category 1 , 2. in panel grouping test shows twice. if add together category 3 grouping test been showed 3 times.
this how info of database:
public list<t> getall( class theclass) { list<t> entity = null; transaction trns = null; session session = sessionfactory.opensession(); seek { trns = session.begintransaction(); entity = session.createcriteria(theclass).list(); session.gettransaction().commit(); } grab (runtimeexception e) { e.printstacktrace(); }finally { session.flush(); session.close(); } homecoming entity; }
inside panel list of groups this:
list<group> grouplist = new arraylist<group>(); grouplist = groupdao.getall(group.class);
if debug through panel , hold on @ page in grouplist
same object equal size of categorys stored group. within database still 1 row.
group entity:
@entity @table(name = "group_user") public class grouping implements serializable{ @id @generatedvalue @column(name = "group_id") private int groupid; @manytomany(cascade = {cascadetype.merge}, fetch = fetchtype.eager) @jointable(name="group_to_category", joincolumns={@joincolumn(name="group_id")}, inversejoincolumns={@joincolumn(name="category_id")}) private set<category> categorys = new hashset<category>(); //constructor.. getter , setter.. }
category entity:
@entity @table(name = "category") public class category implements serializable{ @id @generatedvalue @column(name = "category_id") private int categoryid; @manytomany(mappedby="categorys", fetch = fetchtype.eager) private set<group> groups = new hashset<group>(); //constructor.. getter , setter.. }
this caused utilize of eager fetching, bad thought anyway.
you should seek alter mapping lazy fetching of collections if @ possible. solve issue , new issues introduces can improve handled other means such "open session in view". can see give-and-take of in this question.
if have fetch must done eagerly, however, can right issue using resulttransformer consolidates duplicates follows:
public list<t> getall( class theclass) { list<t> entity = null; transaction trns = null; session session = sessionfactory.opensession(); seek { trns = session.begintransaction(); criteria criteria = session.createcriteria(theclass); criteria.setresulttransformer(criteria.distinct_root_entity) entity = criteria.list(); session.gettransaction().commit(); } grab (runtimeexception e) { e.printstacktrace(); }finally { session.flush(); session.close(); } homecoming entity; }
java hibernate wicket
No comments:
Post a Comment