hibernate 外键问题

问题描述

有外键关系的几个表,要做类似这样的查询: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=?,所以出错了。

时间: 2024-11-06 12:21:51

hibernate 外键问题的相关文章

Hibernate外键许为NULL,JSP如何取值。

问题描述 前提:  1,Hibernate设置:      一个主表A,其中有一个字段外键关联一个基础信息表B,容许为NULL.延迟加载.  2,DAO      直接查询主表A,返回主表A的值对象.  3,系统使用框架:      SSH问题:  在JSP中,直接使用:${主表A.信息表B.外键字段的名称} 时,出错.请问如何解决. 解决方案 恩,在<%@page%>里面加isELIgnored='false'.先让jstl能用.而jstl遇到错误只是不显示而已,不会报错的.这就可以符合你的

hibernate 外键 插入数据问题

问题描述 <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" />

Java的Hibernate框架中的双向主键关联与双向外键关联_java

一.双向主键关联双向的主键关联其实是单向一对一主键关联的一种特殊情况,只不过要在关联对象的两端的映射文件中都要进行<one-to-one>的配置,另外还要在主映射的主键一端采用foreign外键关联属性. 这里同样使用Person和IdCard来讨论,一个人对应着一个唯一的身份证,而且一个身份证也唯一映射着一个人,所以这就产生了双向的关联关系,Person的主键同样也是IdCard的主键,分别是主键的同时也是外键,这种关联关系成为双向一对一映射,表现到关系模型中可如下图: 图中的两个表采用了主

系统学习hibernate之六:一对一外键关联映射双向关联

hibernate一对一唯一外键关联映射(双向关联Person<---->IdCard) 一对一唯一外键关联双向,需要在另一端(idcard),添加<one-to-one>标签,指示hibernate如何加载 其关联对象,默认根据主键加载person,外键关联映射中,因为两个实体采用的是person的外键维护的关系, 所以不能指定主键加载person,而要根据person的外键加载,所以采用如下映射方式: <one-to-one name="person"

系统学习hibernate之五:一对一外键关联

在hibernate中一对一外键关联跟多对一外键关联有很相似的地方, hibernate多对一外键关联先参考这个,然后只要是在*.hbm.xml里面加入以下代码: <many-to-one name="group" column="groupid" unique="true"/>, 就是加入unique="true"属性. 多对一外键关联中的*.hbm.xml里面加入以下代码:<many-to-one nam

Hibernate基于外键的查询方法

我在解决这个问题的时候搜到了百度上的同样问题:hibernate中表怎么根据外键查询 ?? 它的设计为:我有两张表:Teacher id(主键) name Student id(主键) name tid(外键对应Teacher的id) public List findStudentByTeacher(Teacher teacher) { try { session = this.openSession(); String HQL = "select s.name from Student as

在Hibernate框架中编写持久对象类实现外键关联的几点注意事项

关系数据库系统本身就比较复杂,加上Hibernate的O/R映射层,复杂度加重了,很容易出现问题,本人将最近遇到的问题和解决方法做一个总结,整理在下面的一系列文章中 正确理解Hibernate的聚合类型(collection)的使用 在Hibernate中正确实现关联关系中的级联操作(cascading) 在Hibernate框架中编写持久对象类实现外键关联的几点注意事项 本文是第三篇,讲解在one-to-many(一对多)关联关系中的对象类的几个关键方法的实现.主要是equals(),hash

Hibernate一对一单向外键关联(简单总结了5种方法)

比如一对夫妻,丈夫有id,name:妻子有id,name.增加一对一单向外键关联一般有以下几种方法: 1.在husband中增加一个外键,foreign key 2.在husband中增加字段wife_id,wife_id参照wife的id.以wife为主导,必须wife里有id才能参照 3.在wife中增加一个外键,foreign key 4.在wife中增加字段husband_id,husband_id参照husband的id.以hunsband为主导,必须hunsband里有id才能参照

hibernate 一对一注解-hibernate 一对一 唯一外键方式 注解,求大神帮忙?

问题描述 hibernate 一对一 唯一外键方式 注解,求大神帮忙? 例如: 有两张表: Husband(老公表):有字段:hid,hname Wife(老婆表):有字段:wid,wname,husbandid 老公和老婆是一对一,怎么配置一对一 唯一外键 注解,求助?