简体   繁体   English

线程“主”中的异常org.hibernate.hql.ast.QuerySyntaxException:未映射用户[来自Users]

[英]Exception in thread “main” org.hibernate.hql.ast.QuerySyntaxException: Users is not mapped [from Users]

I am new to hibernate. 我是新来的冬眠。 I am trying to fetch data using the query class but facing the subjected issue. 我正在尝试使用查询类获取数据,但是面临问题。 I have checked in hibernate forum but didn't find an answer. 我已经在休眠论坛中签到,但没有找到答案。

Entity class: 实体类:

package com.base.test;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="DBUSER")
public class Users implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="USERID")
    private String userId;

    @Column(name="USERNAME")
    private String userName;

    @Column(name="CREATEDBY")
    private String createdBy;

    @Column(name="CREATEDDATE")
    private Date createdDate;

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public Date getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }


}

Test Class: 测试类别:

package com.base.test;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import com.base.util.HibernateUtil;


public class Test {

    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        Session session=HibernateUtil.getSessionFactory().openSession();    
        session.beginTransaction();
        Query query = session.createQuery("from Users");
        List<Users> usersList = query.list();
        for(Users user:usersList){
            System.out.println("Id: " + user.getUserId());
            System.out.println("Name: " + user.getUserName());
            System.out.println("Created By: " + user.getCreatedBy());
            System.out.println("Created Date: " + user.getCreatedDate());
        }

    }

}

HibernateUtil class: HibernateUtil类:

package com.base.util;


import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();


    private static SessionFactory buildSessionFactory() {
        try {
            return new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
        } catch (HibernateException ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void shutdown() {
        // Close caches and connection pools
        getSessionFactory().close();
    }

}

Error Log: 错误日志:

3157 [main] INFO org.hibernate.impl.SessionFactoryImpl - building session factory
3160 [main] INFO org.hibernate.type.BasicTypeRegistry - Type registration [materialized_blob] overrides previous : org.hibernate.type.MaterializedBlobType@983d95
3160 [main] INFO org.hibernate.type.BasicTypeRegistry - Type registration [clob] overrides previous : org.hibernate.type.ClobType@f30494
3160 [main] INFO org.hibernate.type.BasicTypeRegistry - Type registration [java.sql.Clob] overrides previous : org.hibernate.type.ClobType@f30494
3160 [main] INFO org.hibernate.type.BasicTypeRegistry - Type registration [materialized_clob] overrides previous : org.hibernate.type.MaterializedClobType@b1cc87
3160 [main] INFO org.hibernate.type.BasicTypeRegistry - Type registration [wrapper_materialized_blob] overrides previous : org.hibernate.type.WrappedMaterializedBlobType@eaf40c
3160 [main] INFO org.hibernate.type.BasicTypeRegistry - Type registration [blob] overrides previous : org.hibernate.type.BlobType@13c6641
3160 [main] INFO org.hibernate.type.BasicTypeRegistry - Type registration [java.sql.Blob] overrides previous : org.hibernate.type.BlobType@13c6641
3160 [main] INFO org.hibernate.type.BasicTypeRegistry - Type registration [characters_clob] overrides previous : org.hibernate.type.PrimitiveCharacterArrayClobType@5d391d
3160 [main] INFO org.hibernate.type.BasicTypeRegistry - Type registration [wrapper_characters_clob] overrides previous : org.hibernate.type.CharacterArrayClobType@50a649
Dec 14, 2017 3:08:29 PM net.sf.ehcache.config.ConfigurationFactory parseConfiguration
WARNING: No configuration found. Configuring ehcache from ehcache-failsafe.xml  found in the classpath: jar:file:/C:/Suhel's%20Folder/Work/POC/jars/ehcache.jar!/ehcache-failsafe.xml
3288 [main] INFO org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: Users is not mapped [from Users]
    at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180)
    at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:111)
    at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:93)
    at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:327)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3441)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3325)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:733)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:584)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:244)
    at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:254)
    at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:185)
    at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
    at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:124)
    at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
    at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
    at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1770)
    at com.base.test.Test.main(Test.java:16)

Table Structure: 表结构:

desc DBUSER
Name        Null Type         
----------- ---- ------------ 
USERID           VARCHAR2(15) 
USERNAME         VARCHAR2(15) 
CREATEDBY        VARCHAR2(15) 
CREATEDDATE      DATE     

Here is hibernate.cfg.xml: 这是hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1522/DevDB</property>
        <property name="hibernate.connection.username">devWork</property>
        <property name="hibernate.connection.password">dev$123</property>
        <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>

        <property name="show_sql">true</property>
         <property name="format_sql">true</property>

        <property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>  
         <!-- <property name="cache.provider_class">org.hibernate.cache.EhCache</property> -->
        <property name="hibernate.cache.use_second_level_cache">true</property>  

         <mapping class="com.base.test.Users"></mapping>


    </session-factory>
</hibernate-configuration>

Can someone please help me see where I am going wrong. 有人可以帮我看看我要去哪里了。

I found the solution. 我找到了解决方案。

HQL I used is: 我使用的HQL是:

Query query = session.createQuery("from Users");

Instead It will be 相反,它将是

Query query = session.createQuery("from com.base.test.Users");

That is we have to give to give fully qualified path of the Entity, along with it. 那就是我们必须给出与实体一起的完全合格的路径。 This is irrespective of the location of the file where it is used. 这与使用文件的位置无关。 That is the file inwhere it is used can be in a different/same package as the Entity object. 那就是使用该文件的文件可以与Entity对象位于不同/相同的包中。

Same Problem is there when we use @NamedQueries. 当我们使用@NamedQueries时,同样存在问题。 So below is wrong: 所以下面是错误的:

@NamedQueries({
    @NamedQuery(
    name = "findUsers",
    query = "from Users "
    )
})

Below is correct: 下面是正确的:

@NamedQueries({
    @NamedQuery(
    name = "findUsers",
    query = "from com.base.test.Users "
    )
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM