java - Hibernate @ManyToOne @JoinColumn is always null -
i'm trying implement one-to-many relation between 2 tables using hibernate. here code:
@entity public class board { @id @column(name = "board_id") @generatedvalue private long id; @column private string owner; @column private string title; @column private string refresh; @column private timestamp createdate; @column private timestamp modifydate; @onetomany(mappedby="board", cascade=cascadetype.all) private list<item> items; public long getid() { return id; } public void setid(long id) { this.id = id; } public string getowner() { return owner; } public void setowner(string owner) { this.owner = owner; } public string gettitle() { return title; } public void settitle(string title) { this.title = title; } public string getrefresh() { return refresh; } public void setrefresh(string refresh) { this.refresh = refresh; } public timestamp getcreatedate() { return createdate; } public void setcreatedate(timestamp createdate) { this.createdate = createdate; } public timestamp getmodifydate() { return modifydate; } public void setmodifydate(timestamp modifydate) { this.modifydate = modifydate; } public list<item> getitems() { return items; } public void setitems(list<item> items) { this.items = items; } }
and second table:
@entity public class item { public enum type { link, image, presentation; } public enum javascript { enable, disable; } @id @column @generatedvalue private long id; @manytoone(fetch = fetchtype.eager) @joincolumn(name = "board_id") private board board; @column private type type; @column(length = 10000) private string link; @column private string image; @column private string presentation; @column private string time; @column private javascript javascript; @column private string first; @column private string last; @transient private multipartfile imagefile; @transient private multipartfile presentationfile; public long getid() { return id; } public void setid(long id) { this.id = id; } public board getboard() { return board; } public void setboard(board board) { this.board = board; } public type gettype() { return type; } public void settype(type type) { this.type = type; } public string getlink() { return link; } public void setlink(string link) { this.link = link; } public string getimage() { return image; } public void setimage(string image) { this.image = image; } public string getpresentation() { return presentation; } public void setpresentation(string presentation) { this.presentation = presentation; } public string gettime() { return time; } public void settime(string time) { this.time = time; } public javascript getjavascript() { return javascript; } public void setjavascript(javascript javascript) { this.javascript = javascript; } public string getfirst() { return first; } public void setfirst(string first) { this.first = first; } public string getlast() { return last; } public void setlast(string last) { this.last = last; } public multipartfile getimagefile() { return imagefile; } public void setimagefile(multipartfile imagefile) { this.imagefile = imagefile; } public multipartfile getpresentationfile() { return presentationfile; } public void setpresentationfile(multipartfile presentationfile) { this.presentationfile = presentationfile; } }
but can't working. board_id null in item table. hibernate output looks strange:
hibernate: insert board (board_id, createdate, modifydate, owner, refresh, title) values (null, ?, ?, ?, ?, ?) hibernate: insert item (id, board_id, first, image, javascript, last, link, presentation, time, type) values (null, ?, ?, ?, ?, ?, ?, ?, ?, ?)
any ideas?
to expand on comment @antoniossss, need set relation on both sides before persisting.
// create owning entity first board board = new board(); // create child entities item item1 = new item(); item1.setboard(board); // set relation on item side board.getitems().add(item1); // set relation on board side
also note considered practice initialize collection fields immediately, board#getitems()
never returns null
.
Comments
Post a Comment