使用 Hibernate 从 DB 检索数据时出现映射错误

Posted

技术标签:

【中文标题】使用 Hibernate 从 DB 检索数据时出现映射错误【英文标题】:Mapping error retrieving data from DB using Hibernate 【发布时间】:2016-03-07 00:04:20 【问题描述】:

我有以下实体类:

@Entity
@Table(name="TB_CUSTOMER")
public class Customer 

....

我还有以下 hibernate.cfg.xml :

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <!-- Database connection settings -->
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@**:**:**</property>
        <property name="connection.username">***</property>
        <property name="connection.password">***</property>
        <property name="hibernate.connection.pool_size">20</property>
        <property name="hibernate.hbm2ddl.auto">update</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>

        <!-- Echo all executed SQL to log -->
        <property name="show_sql">false</property>

        <mapping class="the.path.of.entity.class.Customer"/>

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

在我的主要工作中,我正在做以下事情:

customerDao.openCurrentSession();
List<Customer> customers = customerDao.findAll();
customerDao.closeCurrentSession();

dao 类在哪里:

public class CustomerDao
   
    Session currentSession;

    public  List<Customer> findAll() 
       
        Query query = this.getCurrentSession().createQuery("from Customer");
        List<Customer> customers = query.list();
        return customers;
    

    protected Session getCurrentSession()
        if(this.currentSession == null)
            this.currentSession = getSessionFactory().getCurrentSession();
        
        return this.currentSession;
    

    public Session openCurrentSession()
    
        currentSession = getSessionFactory().openSession();
        return currentSession;
    

    public void closeCurrentSession()
           
        if(currentSession != null)
        
            currentSession.close();
        
    

    private static SessionFactory getSessionFactory() 
        try 
            Configuration configuration = new Configuration();
            configuration.configure();
            return configuration
                    .buildSessionFactory(new StandardServiceRegistryBuilder()
                            .applySettings(configuration.getProperties())
                            .build());
         catch (Exception e) 
            e.printStackTrace();
            throw new RuntimeException(
                    "There was an error building the factory");
        
           

当我运行它时,我得到了错误:

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Customer is not mapped
    at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:171)
    at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:91)
    at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:76)
    at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:321)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3678)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3567)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:708)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:564)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:249)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:262)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:190)

如果我将 findAll 方法更改为:

List<Customer> customers = (List<Customer>)this.getCurrentSession().createCriteria(Customer.class).list();
return customers;

那么我只是得到一个空列表。

有人可以帮忙吗?

也许会话以某种方式损坏。

谢谢。

【问题讨论】:

我尝试将 @name 注释中的表名替换为 Cusomer,因此名称将相同并且它不起作用。表 DB 看起来不错。 【参考方案1】:

将你的主要更改为:

customerDao.openCurrentSession().beginTransaction();
List<Customer> customers = customerDao.findAll();
customerDao.closeCurrentSession();

并使用其中之一:

List<Customer> customers =this.getCurrentSession().createCriteria(Customer.class).list();

Query query = this.getCurrentSession().createQuery("from Customer C");

还要检查您的映射信息并在映射中添加 Customer 类的完全限定名称。如:

<mapping class="package_name.Customer"/>

【讨论】:

关于:Query query = this.getCurrentSession().createQuery("from Customer C");通过此更改,我仍然收到“未映射客户”异常。

以上是关于使用 Hibernate 从 DB 检索数据时出现映射错误的主要内容,如果未能解决你的问题,请参考以下文章

仅在运行单元测试时出现 OLE DB 异常“类未注册”

使用 Eclipse 从数据库中检索列表时出现问题

“需要领域迁移”,从领域数据库中检索值时出现异常

使用 hadoop 时出现错误“无法从 /webhdfs/v1/?op=LISTSTATUS:服务器错误检索数据”

从firebase检索时出现空指针异常[重复]

尝试使用 MongoDB 从解析服务器中保存或检索数据时出现“服务器在没有 SSL 支持的情况下启动”错误