简体   繁体   中英

Mapping error retrieving data from DB using Hibernate

I have the below entity class:

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

I also have the following 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>

In my main I'm doing the following:

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

where the dao class is:

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");
        }
    }       
}

when I'm runnig it I'm getting the error:

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)

if I change the findAll method to do:

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

then I'm just getting an empty list.

can Someone help with this?

Maybe the session is corrupted somehow.

thanks.

Change in your main as:

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

And use either of one:

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

or

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

Also check your mapping information and add the fully qualified name of Customer class in mapping. as :

<mapping class="package_name.Customer"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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