关于org.hibernate.hql.ast.QuerySyntaxException:X is not mapped [from X]
?首先,我们先不说hibernate与其他的整合,就只说纯Hibernate项目.
?
出现org.hibernate.hql.ast.QuerySyntaxException问题的人很多, 但是
有两种情况
①、Hibernate项目中,实体类的映射用.hbm.xml再加入hibernate.cfg.xml中
?
②、实体类用annotations注解,hibernate.cfg.xml中指定<mapping />
?
这两种情况出现QuerySyntaxException问题大多数简单的原因:
a、HQL语句问题(写成SQL没有以面向对象的形式)
b、.hbm.xml文件没有加入hibernate.cfg.xml中
我暂时从网络搜索这个问题出现最多也就上面说的原因,本人并没有发现其他原因只是个人观点。
?
现在我的问题是:我用的是注解,HQL正确、注解映射为<mapping name="code">-- Create tablecreate table T_OMN_DUTY( ID CHAR(32) not null, CREATE_DATE TIMESTAMP(6) default current_date not null, UPDATE_DATE TIMESTAMP(6) default current_date not null, REMARK VARCHAR2(256), DUTY_CODE VARCHAR2(20), DUTY_NAME VARCHAR2(30), PARENT_ID CHAR(32), DUTY_LEVEL NUMBER, DUTY_SEQ VARCHAR2(256), DUTY_TYPE VARCHAR2(255), IS_LEAF VARCHAR2(10), SUB_COUNT NUMBER(10,2), SORT_NO NUMBER)tablespace USERS pctfree 10 initrans 1 maxtrans 255 storage ( initial 16 minextents 1 maxextents unlimited );-- Add comments to the table comment on table T_OMN_DUTY is '定义职务及上下级关系';-- Add comments to the columns comment on column T_OMN_DUTY.ID is '职务编号';comment on column T_OMN_DUTY.CREATE_DATE is '创建时间';comment on column T_OMN_DUTY.UPDATE_DATE is '更新时间';comment on column T_OMN_DUTY.REMARK is '备注';comment on column T_OMN_DUTY.DUTY_CODE is '职务代码';comment on column T_OMN_DUTY.DUTY_NAME is '职务名称';comment on column T_OMN_DUTY.PARENT_ID is '上级职务编号';comment on column T_OMN_DUTY.DUTY_LEVEL is '职务层次';comment on column T_OMN_DUTY.DUTY_SEQ is '职务序列号';comment on column T_OMN_DUTY.DUTY_TYPE is '例如科技类,审计类等';comment on column T_OMN_DUTY.IS_LEAF is '是否叶子节点';comment on column T_OMN_DUTY.SUB_COUNT is '子节点数';comment on column T_OMN_DUTY.SORT_NO is '排列顺序编号';-- Create/Recreate primary, unique and foreign key constraints alter table T_OMN_DUTY add constraint PK_T_OMN_DUTY primary key (ID) using index tablespace USERS pctfree 10 initrans 2 maxtrans 255 storage ( initial 64K minextents 1 maxextents unlimited );
?
??
package org.ibas.framework.omn.entity;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.Table;import org.hibernate.annotations.GenericGenerator;import org.hibernate.annotations.Proxy;/** * Duty entity. * 定义职务及上下级关系 * */@Entity@Proxy(lazy = false)@Table(name="T_OMN_DUTY")public class Duty {protected String id; @GenericGenerator(name = "generator", strategy = "uuid.hex")@Id@GeneratedValue(generator = "generator")public String getId() {return id;}public void setId(String id) {this.id = id;} /** * 备注 */ protected String remark; /** * 职务代码 */ protected String dutyCode; /** * 职务名称 */ protected String dutyName; /** * 上级职务编号 */ protected String parentId; /** * 职务层次 */ protected Integer dutyLevel; /** * 职务序列号 */ protected String dutySeq; /** * 职位类型 */ protected String dutyType; /** * 是否叶子节点 */ protected String isLeaf; /** * 子节点数 */ protected Double subCount; private Integer sortNo;//排列顺序编号 @Column(name = "SORT_NO")public Integer getSortNo() {return sortNo;}public void setSortNo(Integer sortNo) {this.sortNo = sortNo;}@Column(name="remark", length=256)public String getRemark() { return this.remark;} public void setRemark(String remark) { this.remark = remark;}@Column(name="duty_code", length=20)public String getDutyCode() { return this.dutyCode;} public void setDutyCode(String dutyCode) { this.dutyCode = dutyCode;}@Column(name="duty_name", length=30)public String getDutyName() { return this.dutyName;} public void setDutyName(String dutyName) { this.dutyName = dutyName;}@Column(name="parent_id", length=32)public String getParentId() { return this.parentId;} public void setParentId(String parentId) { this.parentId = parentId;}@Column(name="duty_level", length=22)public Integer getDutyLevel() { return this.dutyLevel;} public void setDutyLevel(Integer dutyLevel) { this.dutyLevel = dutyLevel;}@Column(name="duty_seq", length=256)public String getDutySeq() { return this.dutySeq;} public void setDutySeq(String dutySeq) { this.dutySeq = dutySeq;}@Column(name="duty_type", length=255)public String getDutyType() { return this.dutyType;} public void setDutyType(String dutyType) { this.dutyType = dutyType;}@Column(name="is_leaf", length=10)public String getIsLeaf() { return this.isLeaf;} public void setIsLeaf(String isLeaf) { this.isLeaf = isLeaf;}@Column(name="sub_count", length=10)public Double getSubCount() { return this.subCount;} public void setSubCount(Double subCount) { this.subCount = subCount;}}
测试代码:
package org.ibas.framework.omn.test;import java.util.List;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import org.ibas.framework.omn.entity.Duty;public class TestSession {@SuppressWarnings("unchecked")public static void main(String[] args) {Configuration config = new Configuration();SessionFactory factory = config.configure().buildSessionFactory();Session session = factory.openSession();String hql = "from Duty";Query query = session.createQuery(hql);List<Duty> deptList = query.list();for (Duty duty : deptList) {System.out.println(duty.getDutyName());}}}
?
有人说hibernate的注解jar有问题,但不晓得是不是!
?
Hibernate项目架构图:
?
希望大家帮帮!谢谢啦