Friday, 15 April 2011

java - Whats the best way to forma list of objects from different tables using Hibernate and spring -



java - Whats the best way to forma list of objects from different tables using Hibernate and spring -

i trying create list of objects contains different types of logs db. build list such order made (a date created field that's in each of tables).

so way getting each list (4 different kinds of logs) , cycle through them , set them in new list comparing them , getting recent 1 each time? seems take long time if list long may or may not be.

is there improve way hibernate set tables , have work me? tables don't share keys or normal join?

any suggestions helpful.

contactlog has columns (id, memberid, phonenumber, email, address, datechanged)

salarylog has columns (id, memberid, salary, datechanged)

relationslog has columns (id, memberid, relationid, datechanged)

personalinfolog has columns (id, memberid, height, weight, eyecolor, haircolor, datechanged)

the purpose of these logs indicate anytime changes info , im trying provide user audit page show changes these different objects.

i suggest using union, if i'm not mistaken, hql not back upwards union, you'll have utilize native query , result transformer. here's sample:

public class log { private long id; private long memberid; private string logtype; private date datechanged; // getters & setters here } public class logservice { @persistencecontext private entitymanager em; public list<log> getlogs(){ final session sess = em.unwrap(session.class); final sqlquery query = session.createsqlquery( "select id,memberid,'contact' logtype, datechanged contactlog"+ "union select id,memberid,'salary' logtype,datechanged salarylog"+ "union select id,memberid,'relations' logtype,datechanged relationslog"+ "union select id,memberid,'personal info' logtype,datechanged personalinfolog "+ "order datechanged desc"; ); query.setresulttransformer(transformers.aliastobean(log.class)); homecoming query.list(); } }

notice mutual columns selected (because must select same number of columns each table when using union).

if want view total details of log, utilize id load specific log , log type know table have total information.

or

you modify query concat changed info 1 column tables (which means adding new field in log.class).

public class log { private long id; private long memberid; private string logtype; private date datechanged; private string changedinfo; // getters & setters here } "select id,memberid,'contact' logtype, datechanged, phonenumber||','||email||','||address changedinfo contactlog"+ "union select id,memberid,'salary' logtype,datechanged,salary changedinfo salarylog"+ "union select id,memberid,'relations' logtype,datechanged,relationid changedinfo relationslog"+ "union select id,memberid,'personal info' logtype,datechanged, height||','|| weight||','|| eyecolor||','|| haircolor changedinfo personalinfolog "+ "order datechanged desc

java spring hibernate spring-mvc

No comments:

Post a Comment