java - org.hibernate.MappingException: Repeated column in mapping for entity -
i have 2 domain-models: "userbean" , "loginbean". it's one-to-many relationship, user has many record of login. loginbean.userid foreign key of userbean.id . here ddl of database:
create table `users` ( `id` int(11) not null, `username` varchar(255) default null, `password` varchar(255) default null, `register_date` datetime default null, primary key (`id`) )
create table `login` ( `id` int(11) not null, `user_id` int(11) not null, `login_date` datetime not null, `login_result` char(1) not null, primary key (`id`), key `user_id` (`user_id`), constraint `login_ibfk_1` foreign key (`user_id`) references `users` (`id`) )
@entity @table(name = "login") public class loginbean { @id @column(name = "id", nullable = false) @genericgenerator(name = "ddd", strategy = "increment") @generatedvalue(generator = "ddd") private integer id; //pk // fk --> user.id @column(name = "user_id", nullable = false) private integer userid; @column(name = "login_date", nullable = false) private date logindate; @column(name = "login_result", nullable = false) private boolean loginresult; @manytoone @elementcollection(targetclass = fordream.hibernate.bean.userbean.class) private userbean user; public integer getid() { return id; } public void setid(integer id) { this.id = id; } public integer getuserid() { return userid; } public void setuserid(integer userid) { this.userid = userid; } public date getlogindate() { return logindate; } public void setlogindate(date logindate) { this.logindate = logindate; } public boolean getloginresult() { return loginresult; } public void setloginresult(boolean loginresult) { this.loginresult = loginresult; } public userbean getuser() { return user; } public void setuser(userbean user) { this.user = user; } }
and:
@entity @table(name = "users") public class userbean { @id @genericgenerator(name = "abc", strategy = "increment") @generatedvalue(generator = "abc") private integer id; //pk @column(name = "username") private string username; @column(name = "password") private string password; @column(name = "register_date") private date register_date; @onetomany(mappedby = "user", cascade = { cascadetype.merge }) private set loginset; public set getloginset() { return loginset; } public void setloginset(set loginset) { this.loginset = loginset; } public integer getid() { return id; } public void setid(integer id) { this.id = id; } public string getusername() { return username; } public void setusername(string username) { this.username = username; } public string getpassword() { return password; } public void setpassword(string password) { this.password = password; } public date getregister_date() { return register_date; } public void setregister_date(date register_date) { this.register_date = register_date; } }
when run application, message :repeated column in mapping entity: loginbean column: user_id (should mapped insert="false" update="false")
checked every place, never defined "user_id" twice in "loginbean". that's why? !
your mapping bidirectional association. userbean parent , loginbean child. have 2 mappings user_id table
@column(name = "user_id", nullable = false) private integer userid;
and
@manytoone @elementcollection(targetclass = fordream.hibernate.bean.userbean.class) private userbean user;
with userid , user.id trying map user_id. totally removing userid field think had tried.
the explanation: @manytoone tag says go , @ class marked manytoone association mappings. , when goes userbean, finds loginbean associated userbean "user" property of loginbean. hibernate try , map userbean's id loginbeans field marked fk(in db) user table.
Comments
Post a Comment