开发者

how to make onttomany relationship in spring hibernate

开发者 https://www.devze.com 2023-03-15 17:18 出处:网络
I am trying to relate two tables with spring / hibernate in MYSQLlike this @Table (name = candidatresumeinfo)

I am trying to relate two tables with spring / hibernate in MYSQL like this

    @Table (name = candidatresumeinfo)
    public class CandidateResumeInfo implements Serializable
      {
     List<SelectedResumes> selectedResumes;
       ......开发者_开发问答.......
         ..............

     @JoinColumn(name = "selectedresumeid")
 @OneToMany
public List<SelectedResumes> getSelectedResumes() {
    return selectedResumes;
}
public void setSelectedResumes(List<SelectedResumes> selectedResumes) {
    this.selectedResumes = selectedResumes;
}

Now ,i got the data in my list correctly( i checked in debug)but the call from server is getting failed which is saying cause:Nullpointer exception .

thanks


You can use OneToMany annotation only on Collections, so you should change the field to Set or List, because hibernate will return multiple result if you use OneToMany. I think you'd like to use ManyToOne annotation here.

  • ManyToOne means here that you have multiple CandidateResumeInfo for one SelectedResumes.
  • OneToMany means here that you have multiple SelectedResumes for one CandidateResumeInfo.

This annotation naming can be a bit strange for first time. Hope I helped.

Answer for your comment:

The best way is you declare the relationship both side. Here is the example:

CandidateResumeInfo.java:

@OneToMany(mappedBy="candidateResumeInfo")
List<SelectedResumes> selectedResumes;

SelectedResumes.java:

@ManyToOne
@JoinColumn(name="candidate_resume_info_id")
CandidateResumeInfo candidateResumeInfo;
0

精彩评论

暂无评论...
验证码 换一张
取 消