java - JPA - Get collection from ManyToOne relationship -
i have table post , post_image
@entity @table(name = "post") @xmlrootelement @namedqueries({ @namedquery(name = "post.findall", query = "select p post p"), @namedquery(name = "post.findbypostid", query = "select p post p p.postid = :postid"), @namedquery(name = "post.findbytitle", query = "select p post p p.title = :title"), @namedquery(name = "post.findbycreateddatetime", query = "select p post p p.createddatetime = :createddatetime")}) public class post implements serializable { private static final long serialversionuid = 1l; @id @generatedvalue(strategy = generationtype.identity) @basic(optional = false) @notnull @column(name = "post_id") private integer postid; @basic(optional = false) @notnull @size(min = 1, max = 500) @column(name = "title") private string title; @basic(optional = false) @notnull @lob @size(min = 1, max = 65535) @column(name = "content") private string content; @column(name = "created_datetime") @temporal(temporaltype.timestamp) private date createddatetime; @joincolumn(name = "user_id", referencedcolumnname = "user_id") @manytoone(optional = false) private user userid; @joincolumn(name = "post_type_id", referencedcolumnname = "post_type_id") @manytoone(optional = false) private posttype posttypeid; public post() { date date = new date(); this.createddatetime =new date(date.gettime()); } public post(integer postid) { this.postid = postid; } public post(integer postid, string title, string content) { this.postid = postid; this.title = title; this.content = content; } public integer getpostid() { return postid; } public void setpostid(integer postid) { this.postid = postid; } public string gettitle() { return title; } public void settitle(string title) { this.title = title; } public string getcontent() { return content; } public void setcontent(string content) { this.content = content; } public date getcreateddatetime() { return createddatetime; } public void setcreateddatetime(date createddatetime) { this.createddatetime = createddatetime; } public user getuserid() { return userid; } public void setuserid(user userid) { this.userid = userid; } public posttype getposttypeid() { return posttypeid; } public void setposttypeid(posttype posttypeid) { this.posttypeid = posttypeid; } @override public int hashcode() { int hash = 0; hash += (postid != null ? postid.hashcode() : 0); return hash; } @override public boolean equals(object object) { // todo: warning - method won't work in case id fields not set if (!(object instanceof post)) { return false; } post other = (post) object; if ((this.postid == null && other.postid != null) || (this.postid != null && !this.postid.equals(other.postid))) { return false; } return true; } @override public string tostring() { return "entity.post[ postid=" + postid + " ]"; } } and
@entity @table(name = "post_image") @xmlrootelement @namedqueries({ @namedquery(name = "postimage.findall", query = "select p postimage p"), @namedquery(name = "postimage.findbypostimageid", query = "select p postimage p p.postimageid = :postimageid"), @namedquery(name = "postimage.findbypath", query = "select p postimage p p.path = :path"), @namedquery(name = "postimage.findbytitle", query = "select p postimage p p.title = :title")}) public class postimage implements serializable { private static final long serialversionuid = 1l; @id @basic(optional = false) @notnull @column(name = "post_image_id") private integer postimageid; @basic(optional = false) @notnull @size(min = 1, max = 500) @column(name = "path") private string path; @basic(optional = false) @notnull @size(min = 1, max = 500) @column(name = "title") private string title; @joincolumn(name = "post_id", referencedcolumnname = "post_id") @manytoone(optional = false) private post postid; public postimage() { } public postimage(integer postimageid) { this.postimageid = postimageid; } public postimage(integer postimageid, string path, string title) { this.postimageid = postimageid; this.path = path; this.title = title; } public integer getpostimageid() { return postimageid; } public void setpostimageid(integer postimageid) { this.postimageid = postimageid; } public string getpath() { return path; } public void setpath(string path) { this.path = path; } public string gettitle() { return title; } public void settitle(string title) { this.title = title; } public post getpostid() { return postid; } public void setpostid(post postid) { this.postid = postid; } @override public int hashcode() { int hash = 0; hash += (postimageid != null ? postimageid.hashcode() : 0); return hash; } @override public boolean equals(object object) { // todo: warning - method won't work in case id fields not set if (!(object instanceof postimage)) { return false; } postimage other = (postimage) object; if ((this.postimageid == null && other.postimageid != null) || (this.postimageid != null && !this.postimageid.equals(other.postimageid))) { return false; } return true; } @override public string tostring() { return "entity.postimage[ postimageid=" + postimageid + " ]"; } } i want collection of images particular post collection objpostimage = objpost.getpostimagecollection() manytoone relationship not provide functionality me how can convert 1 many or how can image collection post.? new java , suggestion appreciated thanx in advance...
you can add java.util.set of postimages in post object, , use hibernate mapping provide relationship. this site has great example of setting 1 many relationships.
so, example, want add following post class:
private set<postimage> postimages = new hashset<postimage>(); @onetomany(fetch = fetchtype.lazy, mappedby = "post") public set<postimage> getpostimages() { return this.postimages; } public void setpostimages(set<postimage> postimages) { this.postimages= postimages; } then, in postimage class, add reference post object:
private post post; @manytoone(fetch = fetchtype.lazy) @joincolumn(name = "post_id", nullable = false) public stock getpost() { return this.post; } public void setpost(post post) { this.post= post; } after adding that, able call getpostimages() method on post object.
Comments
Post a Comment