Using hibernate to get 2 objects which both are linked with a logical foreign key (reference key) -
i new hibernate , tables follows:
tb_user
- userid (pk)
- userfirstname
- userlastname
- userlogin
- userpwd
tb_order
- orderid (pk)
- userid (logical fk)
- orderdate
- orderremarks
my dao class's function:
public list<tborder> getuserordering(tborder tborder) throws exception{ criteria _criteria = getsession().createcriteria(tborder.class); if(tborder.getuserid() != null) _criteria.add(restrictions.eq("userid", tborder.getuserid())); return (list<tborder>) _criteria.list(); }
my entity class:
class tborder { long orderid; long userid; public tborder(){ } public tborder(long orderid, long userid){ this.orderid = orderid; this.userid = userid; } public long getorderid() { return orderid; } public void setorderid(long orderid) { this.orderid = orderid; } public long getuserid() { return userid; } public void setuserid(long userid) { this.userid = userid; } } class tbuser{ private long userid; private string userfirstname; private string userlastname; private string userlogin; private string userpwd; public tbuser() { } public tbuser(long userid, string userfirstname, string userlastname, string userlogin, string userpwd) { this.userid = userid; this.userfirstname = userfirstname; this.userlastname = userlastname; this.userlogin = userlogin; this.userpwd = userpwd; } public long getuserid() { return userid; } public void setuserid(long userid) { this.userid = userid; } public string getuserfirstname() { return userfirstname; } public void setuserfirstname(string userfirstname) { this.userfirstname = userfirstname; } public string getuserlastname() { return userlastname; } public void setuserlastname(string userlastname) { this.userlastname = userlastname; } public string getuserlogin() { return userlogin; } public void setuserlogin(string userlogin) { this.userlogin = userlogin; } public string getuserpwd() { return userpwd; } public void setuserpwd(string userpwd) { this.userpwd = userpwd; } }
it supposed tb_order.userid should set foreign key link tb_user, reasons, can't so.
however, here sitruation, want user's first name , last name has placed order id=1001, , order's date , remarks. normally, while googling while, tells me can try id.userid
tb_user object, however, din't id class generated hibernate , supposed be. so, please advice me best way both order , user information? thanks!
try modifying dao class's function this:
public list <tb_user> getuserordering(tborder tborder) throws exception{ criteria _criteria = getsession().createcriteria(tb_user.class); if(tborder.getuserid() != null) _criteria.add(restrictions.eq("userid", tborder.getuserid())); return (list<tb_user>) _criteria.list(); }
the list <tb_user>
get, iterate , list of tb_user objects can user's first name , last name using getter methods.
note: assuming tborder tborder object passing has required userid inside it.
Comments
Post a Comment