java - hibernate h2 not generating relations -
i'm trying generate hibernate mapping h2 database. have 2 tables test, called users , users_groups. like:
users table: user_id integer pk login varchar password varchar user_group_id integer fk users_groups user_group_id integer pk name varchar
and problem hibernate generate entities that:
@entity public class users { private int userid; private int usergroupid; @id @column(name = "user_id", nullable = false) public int getuserid() { return userid; } public void setuserid(int userid) { this.userid = userid; } @basic @column(name = "user_group_id", nullable = false) public int getusergroupid() { return usergroupid; } public void setusergroupid(int usergroupid) { this.usergroupid = usergroupid; } @entity @table(name = "users_groups", schema = "public", catalog = "database") public class usersgroups { private int usergroupid; @id @column(name = "user_group_id", nullable = false) public int getusergroupid() { return usergroupid; } public void setusergroupid(int usergroupid) { this.usergroupid = usergroupid; }
so no relation annotations generated, @onetomany or @manytomany etc. doing wrong? help. p.s. want generate mapping users class field of usergroup type
if classes auto generated check relation in database between 2 tables , make sure choose right schema mapping wrong example :-
1-the auto generated classes mapping missing columns, class user
doesn't contain password , login columns , class usersgroups
doesn't contain name column.
2- class user
doesn't have @table
annotation
they should :-
class usergroups
@entity @table(name = "users_groups", schema = "public", catalog = "database") public class usersgroups implements java.io.serializable { private int usergroupid; private string name; private set<users> users = new hashset<users>(0); public usersgroups() { } @id @generatedvalue(strategy = identity) //this make id auto increment @column(name = "user_group_id", nullable = false) public int getusergroupid() { return usergroupid; } public void setusergroupid(int usergroupid) { this.usergroupid = usergroupid; } // if name column not unique / nullable remove values annotation @column(name = "name", unique = true, nullable = false, length = 10) public string getname() { return this.name; } public void setname(string name) { this.name= name; } @onetomany(fetch = fetchtype.eager, mappedby = "users_groups") public set<users> getusers() { return this.users; } public void setusers(set<users> users) { this.users= users; } }
class users
@entity @table(name = "users", schema ="public" , catalog ="database") public class users implements java.io.serializable { private integer userid; private usersgroups usersgroups; private string password; private string login; public users() { } @id @generatedvalue(strategy = identity) @column(name = "user_id", unique = true, nullable = false) public integer getuserid() { return this.userid; } public void setuserid(integer userid) { this.userid = userid; } @manytoone(fetch = fetchtype.eager) @joincolumn(name = "user_group_id", nullable = false) public usersgroups getusersgroups() { return this.usersgroups; } public void setusersgroups(usersgroups usersgroups) { this.usersgroups = usersgroups; } @column(name = "password",length = 10) public string getpassword() { return this.password; } public void setpassword(string password) { this.password = password; } @column(name = "login",length = 10) public string getlogin() { return this.login; } public void setlogin(string login) { this.login = login; } }
check full example 1 many mapping
Comments
Post a Comment