java - How to add property class by it's id with Hibernate? -
have stucked bit , think - need help.
suppose have class message, child forum. have one-to-many relation in annotation. this:
@entity @table(name="message", catalog="catalog") public class message implements serializable { @id @generatedvalue(strategy = identity) @column(name="idmessage", unique = true) private int idmessage; @column(name="title", columndefinition="text") private string title; @column(name="message", columndefinition="text") private string message; @manytoone @joincolumn(name="idforum") private forum forum; }
problem:
i don't want retrieve forum object it's id, attach message , save message. have started create wrapper, like:
public class messagedto { private int idmessage; private string title; private string message; private int idforum; }
and use whenever saving/updating. have strong feeling doing wrong.
simple guiding, in direction answer, great help.
thank in advance!
in hibernate, don't need fetch object if exists. create new forum id (you should have already). example:
message message = new message(); ... ... forum forum = new forum(); forum.setid(yourid); message.setforum(forum); messagerepository.save(message);
hibernate should save ok.
Comments
Post a Comment