oracle - Hibernate Composite Table and Key Issues -
i trying hibernate work composite table. there users table, roles table, , composite table called userroles connects both. query getting user roles eagerly outputs below. keep getting stackoverflow errors, or null exception. question why columns product 2 userid , roleid outputs in oracle?
select this_.id id0_1_, this_.datecreated datecrea2_0_1_, this_.email email0_1_, this_.enabled enabled0_1_, this_.firstname firstname0_1_, this_.lastname lastname0_1_, this_.password password0_1_, this_.salt salt0_1_, this_.username username0_1_, userroles2_.userid userid3_, userroles2_.roleid roleid3_, userroles2_.roleid roleid3_0_, userroles2_.userid userid3_0_ cisco.users this_ inner join cisco.userroles userroles2_ on this_.id=userroles2_.userid this_.username='mike'
my classes below.
user.java
@entity @table(name = "users", schema = "cisco") @suppresswarnings("serial") public class user implements serializable { /** * attribute id. */ private long id; /** * attribute username. */ private string username; /** * attribute password. */ private string password; /** * attribute enabled. */ private long enabled; /** * attribute salt. */ private string salt; /** * attribute first name. */ private string firstname; /** * attribute last name. */ private string lastname; /** * attribute email. */ private string email; /** * attribute email. */ private date datecreated; /** * @return id */ @basic @id @column(name = "id") public long getid() { return id; } /** * @param username new value id */ public void setid(long id) { this.id = id; } /** * @return username */ @basic @column(name = "username", length = 50) public string getusername() { return username; } /** * @param username new value username */ public void setusername(string username) { this.username = username; } /** * @return password */ @basic @column(name = "password", length = 50) public string getpassword() { return password; } /** * @param password new value password */ public void setpassword(string password) { this.password = password; } /** * @return enabled */ @basic @column(name = "enabled") public long getenabled() { return enabled; } /** * @param enabled new value enabled */ public void setenabled(long enabled) { this.enabled = enabled; } /** * @return salt */ @basic @column(name = "salt", length = 25) public string getsalt() { return salt; } /** * @param salt new value salt */ public void setsalt(string salt) { this.salt = salt; } /** * @return first name */ @basic @column(name = "firstname", length = 100) public string getfirstname() { return firstname; } /** * @param first name new value first name */ public void setfirstname(string firstname) { this.firstname = firstname; } /** * @return last name */ @basic @column(name = "lastname", length = 100) public string getlastname() { return lastname; } /** * @param last name new value last name */ public void setlastname(string lastname) { this.lastname = lastname; } /** * @return email */ @basic @column(name = "email", length = 50) public string getemail() { return email; } /** * @param email new value email */ public void setemail(string email) { this.email = email; } /** * @return datecreated */ @basic @column(name = "datecreated") public date getdatecreated() { return datecreated; } /** * @param datecreated new value datecreated */ public void setdatecreated(date datecreated) { this.datecreated = datecreated; } private list<userrole> userroles; /** * list of user roles */ @onetomany(fetch=fetchtype.eager, mappedby="userrolepk.user") public list<userrole> getuserroles() { return this.userroles; } /** * set list of user roles */ public void setuserroles(list<userrole> userroles) { this.userroles = userroles; } }
role.java
@entity @table(name = "roles", schema = "cisco") @suppresswarnings("serial") public class role implements serializable { /** * attribute id. */ private long id; /** * attribute name. */ private string name; /** * list of userroles */ private list<userrole> userroles = null; /** * @return id */ @basic @id @column(name = "id") public long getid() { return id; } /** * @param id new value id */ public void setid(long id) { this.id = id; } /** * @return name */ @basic @column(name = "name", length = 20) public string getname() { return name; } /** * @param name new value name */ public void setname(string name) { this.name = name; } /** * list of userroles */ @onetomany(fetch=fetchtype.lazy, mappedby="userrolepk.role") public list<userrole> getuserroles() { return this.userroles; } /** * set list of userroles */ public void setuserroles(list<userrole> userroles) { this.userroles = userroles; } }
userrole.java
@entity @table(name = "userroles", schema = "cisco") @suppresswarnings("serial") public class userrole implements serializable { /** * primary key */ private userrolepk userrolepk; /** * primary key */ @basic @id public userrolepk getuserrolepk() { return this.userrolepk; } /** * set primary key */ public void setuserrolepk(userrolepk userrolepk) { this.userrolepk = userrolepk; } @suppresswarnings("serial") @embeddable public static class userrolepk implements serializable { /** * attribute users */ private user user; /** * attribute roles */ private role role; /** * users */ @manytoone(fetch=fetchtype.lazy) @joincolumn(name = "userid") public user getuser() { return this.user; } /** * set users */ public void setuser(user user) { this.user = user; } /** * roles */ @manytoone(fetch=fetchtype.lazy) @joincolumn(name = "roleid") public role getrole() { return this.role; } /** * set roles */ public void setrole(role roles) { this.role = role; } /** * calculate hashcode */ @override public int hashcode() { //todo : implement method return super.hashcode(); } /** * equals method */ @override public boolean equals(object object) { //todo : implement method return super.equals(object); } } }
the call gets data is
public list<t> findbycriteria(criteria criteria, list<criterion> criterions, ilist list) { if(criterions != null) { for(criterion c : criterions) { criteria.add(c); } } criteria.setprojection(projections.rowcount()); list.settotal(((integer)criteria.uniqueresult()).intvalue()); logger.debug("count:" + list.gettotal()); if(list.gettotal() > 0) { criteria.setprojection(null); criteria.setresulttransformer(criteria.root_entity); criteria.setfirstresult((list.getpage() - 1) * list.getpagesize()).setmaxresults(list.getpagesize()); if(list.getsortby() != null && list.getsortby().length() > 0) { order order = null; if(list.getorderby().equalsignorecase("asc")) order = order.asc(list.getsortby()); else order = order.desc(list.getsortby()); criteria.addorder(order); } return criteria.list(); } return new arraylist<t>();
}
i'm not sure cause of strange query, wonder why map join table entity instead of using @manytomany
?
@entity @table(name = "users", schema = "cisco") @suppresswarnings("serial") public class user implements serializable { ... @manytomany @jointable(name = "userroles", joincolumns = @joincolumn(name = "userid"), inversejoincolumns = @joincolumn(name = "roleid") ) public list<role> getroles() { ... } public void setroles(list<role> roles) { ... } } @entity @table(name = "roles", schema = "cisco") @suppresswarnings("serial") public class role implements serializable { ... // i'm not sure if need relationship bidirectional, // if need here side @manytomany(mappedby = "roles") public list<user> getusers() { ... } public void setusers(list<user> users) { ... } }
see also:
Comments
Post a Comment