问题描述
有外键关系的几个表,要做类似这样的查询:select hbiNId from hrBranchInfo where hbi_n_prior=? and hbiVname=? and hbiCState=1;问题可能出现在红色标注的字段。下面是代码,大家看看问题出在了哪。 报错:org.hibernate.hql.ast.QuerySyntaxException: hrBranchInfo is not mapped. [select hbiNId from hrBranchInfo where hbi_n_prior=? and hbiVname=? and hbiCState=1] 映射:hibernate-mapping> <class name="com.pojo.HrBranchInfo" table="hr_branch_info" schema="public"> <id name="hbiNId" type="java.lang.Integer"> <column name="hbi_n_id" /> <generator class="sequence" /> </id> <many-to-one name="hrEmpBase" class="com.pojo.HrEmpBase" fetch="select"> <column name="hbi_n_operator" /> </many-to-one> <many-to-one name="hrBranchInfo" class="com.pojo.HrBranchInfo" fetch="select"> <column name="hbi_n_prior" not-null="false" /> </many-to-one> <property name="hbiVName" type="java.lang.String"> <column name="hbi_v_name" length="40" not-null="true" /> </property> <property name="hbiCState" type="java.lang.String"> <column name="hbi_c_state" length="1" not-null="true" /> </property> <property name="hbiVDesc" type="java.lang.String"> <column name="hbi_v_desc" length="200" /> </property> <property name="hbiCTime" type="java.lang.String"> <column name="hbi_c_time" length="14" /> </property> <property name="hbiNSortindex" type="java.lang.String"> <column name="hbi_n_sortindex" length="10" /> </property> <property name="hbiVTemp1" type="java.lang.String"> <column name="hbi_v_temp1" length="100" /> </property> <property name="hbiVTemp2" type="java.lang.String"> <column name="hbi_v_temp2" length="100" /> </property> <property name="hbiVTemp3" type="java.lang.String"> <column name="hbi_v_temp3" length="100" /> </property> <set name="hrEmpBases" inverse="true"> <key> <column name="hbi_n_id" not-null="true" /> </key> <one-to-many class="com.pojo.HrEmpBase" /> </set> <set name="hrRelationBranduties" inverse="true"> <key> <column name="hbi_n_id" not-null="true" /> </key> <one-to-many class="com.pojo.HrRelationBranduty" /> </set> <set name="hrBranchInfos" inverse="true"> <key> <column name="hbi_n_prior" not-null="true" /> </key> <one-to-many class="com.pojo.HrBranchInfo" /> </set> </class></hibernate-mapping> dao://检查部门名称有效性public boolean check(HrBranchInfo hbi){String queryString = "select hbiNId from hrBranchInfo where hbi_n_prior=? and hbiVname=? and hbiCState=1";try {Query query = getSession().createQuery(queryString);List lst = query.list();if(lst.size() > 0){return true;}else {return false;}} catch (HibernateException e) {// TODO Auto-generated catch blocke.printStackTrace();}return false;} 实体类。package com.pojo;import java.util.HashSet;import java.util.Set;/** * HrBranchInfo entity. @author MyEclipse Persistence Tools */public class HrBranchInfo implements java.io.Serializable {// Fieldsprivate Integer hbiNId;private HrEmpBase hrEmpBase;private HrBranchInfo hrBranchInfo;private String hbiVName;private String hbiCState;private String hbiVDesc;private String hbiCTime;private String hbiNSortindex;private String hbiVTemp1;private String hbiVTemp2;private String hbiVTemp3;private Set hrRelationBranduties = new HashSet(0);private Set hrBranchInfos = new HashSet(0);// Constructors/** default constructor */public HrBranchInfo() {}/** minimal constructor */public HrBranchInfo(HrBranchInfo hrBranchInfo, String hbiVName,String hbiCState) {this.hrBranchInfo = hrBranchInfo;this.hbiVName = hbiVName;this.hbiCState = hbiCState;}/** full constructor */public HrBranchInfo(HrEmpBase hrEmpBase, HrBranchInfo hrBranchInfo,String hbiVName, String hbiCState, String hbiVDesc,String hbiCTime, String hbiNSortindex, String hbiVTemp1,String hbiVTemp2, String hbiVTemp3, Set hrRelationBranduties,Set hrBranchInfos) {this.hrEmpBase = hrEmpBase;this.hrBranchInfo = hrBranchInfo;this.hbiVName = hbiVName;this.hbiCState = hbiCState;this.hbiVDesc = hbiVDesc;this.hbiCTime = hbiCTime;this.hbiNSortindex = hbiNSortindex;this.hbiVTemp1 = hbiVTemp1;this.hbiVTemp2 = hbiVTemp2;this.hbiVTemp3 = hbiVTemp3;this.hrRelationBranduties = hrRelationBranduties;this.hrBranchInfos = hrBranchInfos;}// Property accessorspublic HrBranchInfo(String hbiVName, Set hrBranchInfos, String hbiCState,String hbiVDesc,String hbiCTime, String hbiNSortindex) {super();this.hbiVName = hbiVName;this.hrBranchInfos = hrBranchInfos;this.hbiCState = hbiCState;this.hbiVDesc = hbiVDesc;this.hbiCState = hbiCTime;this.hbiNSortindex = hbiNSortindex;}public Integer getHbiNId() {return this.hbiNId;}public void setHbiNId(Integer hbiNId) {this.hbiNId = hbiNId;}public HrEmpBase getHrEmpBase() {return this.hrEmpBase;}public void setHrEmpBase(HrEmpBase hrEmpBase) {this.hrEmpBase = hrEmpBase;}public HrBranchInfo getHrBranchInfo() {return this.hrBranchInfo;}public void setHrBranchInfo(HrBranchInfo hrBranchInfo) {this.hrBranchInfo = hrBranchInfo;}public String getHbiVName() {return this.hbiVName;}public void setHbiVName(String hbiVName) {this.hbiVName = hbiVName;}public String getHbiCState() {return this.hbiCState;}public void setHbiCState(String hbiCState) {this.hbiCState = hbiCState;}public String getHbiVDesc() {return this.hbiVDesc;}public void setHbiVDesc(String hbiVDesc) {this.hbiVDesc = hbiVDesc;}public String getHbiCTime() {return this.hbiCTime;}public void setHbiCTime(String hbiCTime) {this.hbiCTime = hbiCTime;}public String getHbiNSortindex() {return this.hbiNSortindex;}public void setHbiNSortindex(String hbiNSortindex) {this.hbiNSortindex = hbiNSortindex;}public String getHbiVTemp1() {return this.hbiVTemp1;}public void setHbiVTemp1(String hbiVTemp1) {this.hbiVTemp1 = hbiVTemp1;}public String getHbiVTemp2() {return this.hbiVTemp2;}public void setHbiVTemp2(String hbiVTemp2) {this.hbiVTemp2 = hbiVTemp2;}public String getHbiVTemp3() {return this.hbiVTemp3;}public void setHbiVTemp3(String hbiVTemp3) {this.hbiVTemp3 = hbiVTemp3;}public Set getHrRelationBranduties() {return this.hrRelationBranduties;}public void setHrRelationBranduties(Set hrRelationBranduties) {this.hrRelationBranduties = hrRelationBranduties;}public Set getHrBranchInfos() {return this.hrBranchInfos;}public void setHrBranchInfos(Set hrBranchInfos) {this.hrBranchInfos = hrBranchInfos;}} 请大家帮下忙,谢谢! 问题补充:<div class="quote_title">redstarofsleep 写道</div><div class="quote_div"><pre name="code" class="java">select hbiNId from hrBranchInfo where hrBranchInfos=? and hbiVname=? and hbiCState=1;</pre><br />改成这样试试</div><br />改了,还是说:hrBranchInfo is not mapped
解决方案
select hbiNId from hrBranchInfo where hrBranchInfos=? and hbiVname=? and hbiCState=1;改成这样试试
解决方案二:
# <set name="hrBranchInfos" inverse="true"> # <key> # <column name="hbi_n_prior" not-null="true" /> # </key> # <one-to-many class="com.pojo.HrBranchInfo" /> # </set>class="com.pojo.HrBranchInfo" 自己和自己一对多吗?
解决方案三:
select hbiNId from com.pojo.HrBranchInfo where hrBranchInfos=? and hbiVname=? and hbiCState=1;hrBranchInfo 改为类名com.pojo.HrBranchInfo(最好带着包名),where 后面的条件用类中的字段名,不要用数据库中的字段名
解决方案四:
你是不是要对hrBranchInfo类做一个自关联?hbi_n_prior这个是自关联的外键的话,你应该用hrBranchInfo.hbi_n_prior=?。我不知道你具体逻辑是什么。你可以这样试试。
解决方案五:
HrBranchInfo.java里面好像没有hbi_n_prior属性,不能直接用hbi_n_prior=?,所以出错了。