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

时间:2015-12-02 16:33:01

标签: java database hibernate hibernate-generic-dao

我有以下实体类:

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

然后我只是得到一个空列表。

有人可以帮忙吗?

也许会话以某种方式被破坏了。

感谢。

1 个答案:

答案 0 :(得分:0)

将您的主要内容更改为:

func bar2(foo: Foo) -> Void {
    print("some boring print")
}

someFoos.forEach(bar2)

并使用其中之一:

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

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

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

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