【问题标题】:want to add two different tables(classes) in one hibernate criteria想在一个休眠条件中添加两个不同的表(类)
【发布时间】:2011-09-03 20:35:32
【问题描述】:

我有这个代码

ArrayList city = '安妮斯顿'; Criteria crit = session.createCriteria(CandidateResumeInfo.class); crit.add(Restrictions.eq("resumeSearchable", 1));

现在我想添加以下条件

crit.add(Restrictions.in("cities", city));

但问题是城市列不在 CandidateResumeInfo.class 中,它在 CandidateInfo 类中。

知道如何在上述条件中添加此条件,如何在上述条件中添加CandidateInfo 类。

我想我需要加入或链接这两个表,但是如何以及实体类中是否会有任何变化?

这是我的两门课

@实体 @Table(name="candidateinfo") 公共类 CandidateInfo 实现 java.io.Serializable { 私人 int id; 私人字符串名; 私人字符串姓氏; 私人字符串城市; 私有字符串 stateProvince; 私人字符串 zip; 私有字符串国家; 私有集候选视频 = new HashSet(); 私人字符串 yearsOfExperience; 私人字符串登录名; 私人字符串密码; 私有字符串地址; 私人字符串电子邮件地址; 私人 int 密码ResetQuestionId; 私人字符串密码重置答案; 私有字符串中间名; 私人字符串 homeEveningPhone; 私人字符串 workDayPhone; 私人布尔视频已提交; 私人布尔简历已提交; 私人字符串手机; 私有字符串可用性=null; 私人字符串工作状态=空; 私有字符串desiredSalary=null; 私有字符串 currentJobLevel=null; 私有字符串 currentJobTitle=null; 私有字符串 targetJobTitle=null; 私人字符串替代目标工作标题1 =空; 私人字符串替代目标工作标题2=空; 私有字符串 targetJobType=null; 私人字符串事件类型=空; 私有字符串 joinDate = null; 私有字符串 lastLoginDate = null; //私有SkillsBean技能信息; 私有集合技能 = new HashSet(); 私人集合候选人简历=新哈希集(); 私有集 targetJobCategoriesId = new HashSet(); 私人设置 targetJobLocationsId = new HashSet(); 公共候选人信息(){ } @Column(name="userid") 公共 int getId() { 返回这个.id; } @Column(name="登录名") 公共字符串 getLoginName() { 返回登录名; } 公共无效setLoginName(字符串登录名){ this.loginName = 登录名; } @Column(name="密码") 公共字符串 getPassword() { 返回密码; } 公共无效setPassword(字符串密码){ this.password = 密码; } @Column(name="地址") 公共字符串 getAddress() { 退货地址; } 公共无效setAddress(字符串地址){ this.address = 地址; } ..................................................... ………………………………………………………………………………………… @实体 @Table(name="candidateresumeinfo") 公共类 CandidateResumeInfo 实现 Serializable{ 私人 int resumeId; 私人 int 候选人_用户 ID; 私人字符串恢复文件位置; 私人 int resumeSearchable; 私人日期 lastUpdateDate; 私人字符串恢复标题; 私有字符串恢复文本; 私人弦乐技巧; 私有 int 行计数; @Column(name="resumeSearchable") 公共 int isResumeSearchable() { 返回简历可搜索; } 公共无效 setResumeSearchable(int resumeSearchable) { this.resumeSearchable = resumeSearchable; } @ID @GeneratedValue @Column(name="resumeid") 公共 int getResumeId() { 返回简历ID; } 公共无效 setResumeId(int resumeId) { this.resumeId = resumeId; } @Column(name="candidate_userid") 公共 int getCandidate_userId() { 返回候选人_用户ID; } 公共无效 setCandidate_userId(int Candidate_userId) { this.candidate_userId = Candidate_userId; } @Column(name="resumelocation") 公共字符串 getResumeFileLocation() { 返回恢复文件位置; } 公共无效 setResumeFileLocation(字符串恢复文件位置){ this.resumeFileLocation = resumeFileLocation; } @Column(name="resumetitle") 公共字符串 getResumeTitle() { 返回简历标题; } 公共无效setResumeTitle(字符串恢复标题){ this.resumeTitle = 恢复标题; } @Column(name="resumetext") 公共字符串 getResumeText() { 返回简历文本; } 公共无效 setResumeText(字符串恢复文本){ this.resumeText = resumeText; } 公共无效 setLastUpdateDate(日期 lastUpdateDate){ this.lastUpdateDate = 最后更新日期; } @Column(name="lastUpdateDate") 公共日期 getLastUpdateDate() { 返回最后更新日期; } @Column(name="技能") 公共字符串getSkills(){ 回归技能; } 公共无效 setSkills(字符串技能){ this.skills = 技能; } @短暂的 公共 int getRowCount() { 返回行数; } 公共无效setRowCount(整数计数){ this.rowCount = 计数; }

【问题讨论】:

    标签: hibernate hibernate-criteria


    【解决方案1】:

    你需要添加类似的关系

    @ManyToOne(cascade = CascadeType.ALL)
        public CandidateResumeInfo getCandidateResumeInfo () {
        return this.candidateResumeInfo ;
    }
    

    【讨论】:

    • 谢谢,我需要在 CandidateInfo 类中添加此语句吗?我还需要在 CandidateResumeInfo 类中写任何语句吗?以及我上面在我的问题中编写的代码,我是否需要在那里写任何其他内容,我将在其中定义此城市列所属的位置谢谢
    【解决方案2】:

    拥有从CandidateResumeInfo 到CandidateInfo 的多对一关联。

    在CandidateResumeInfo类中添加新成员说,

    @ManyToOne()  
    @JoinColumn(name = "candidate_userId")  
    private CandidateInfo candidateInfo;  
    
    // Also add getters and setters  
    

    现在在查询中,添加一个新的别名,

    crit.createAlias("candidateInfo", "candidateInfo");  
    crit.add(Restrictions.in("candidateInfo.city", city));  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多