spring mvc - how can I use hibernate to get OneToMany reference datas -
i want make ordermeal web project in using springmvc+spring+hibernate. seens got problems @ printing datas on jsp pages.
when debug project(i'm sorry can't post debug picture t_t)
i can not figure out why property "mealorders" empty... have insert many datas tables.
here entitys , hql query
user class
@entity @table(name="userbaseinfo") public class user implements serializable { private static final long serialversionuid = 1l; private long userid;//id private string username; private string loginname; private string userpassword; private string cleartextpassword; private string postion; private string telephone; private string email; private string userlocation; private date createtime; private date lastedittime; private date passwordedittime; private int onlinetime; private string lastloginip; private int logincount; private int userstate; private set<mealorder> mealorders = new hashset<mealorder>(); @id @generatedvalue @column(name = "id") public long getuserid() { return userid; } ...i hide useless propertys @onetomany(targetentity=mealorder.class, fetch=fetchtype.lazy, cascade={cascadetype.all}, mappedby = "user") public set<mealorder> getmealorders() { return mealorders; } public void setmealorders(set<mealorder> mealorders) { this.mealorders = mealorders; }
mealorder class
@entity @table(name="mealorder") public class mealorder implements serializable { private static final long serialversionuid = 1l; private long mealorderid; private int orderstate; //private long userid; private string username; private string postion; private string telephone; private string userlocation; private date ordertime; private string restaurantname; private int restaurantid; private string supplytimetype; private date beginsupplytime; private date endsupplytime; private date beginsupplydate; private date endsupplydate; private int mealmenuid; private string mealmenuname; private string mealpackagename; private int mealpackageid; private float mealpackageprice; private user user; @id @generatedvalue @column(name = "id") public long getmealorderid() { return mealorderid; } public void setmealorderid(long mealorderid) { this.mealorderid = mealorderid; } ...i hide useless propertys @manytoone(fetch=fetchtype.lazy,cascade = cascadetype.all) @joincolumn(name = "userid") public user getuser() { return user; } public void setuser(user user) { this.user = user; }
and hql query below
@suppresswarnings("unchecked") @override public list<user> findallusers() { session session = sessionfactory.opensession(); org.hibernate.query query = session.createquery("from user u left join u.mealorders"); return (list<user>) query.list(); }
and jsp page(it can not print datas)
<c:foreach items="${users}" var="user"> <tr> <td>${user.username}</td> <td>${user.userid}</td> </tr> </c:foreach>
how can print datas on jsp page?
- you can make fetch type eager force user retrieve it's children (not desired performance-wise in case don't need data).
- you can "get" children while transaction still open result in query db.
- you can change query "left join fetch" force object creation.
Comments
Post a Comment