0

我有以下实体类:

@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;

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

有人可以帮忙吗?

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

谢谢。

4

1 回答 1

0

将您的主要更改为:

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"/>
于 2015-12-02T17:25:56.630 回答