私は以下のエンティティクラスを持っています:
@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;
その後、空のリストを取得しています。
誰かがこれを手伝ってくれる?
何らかの理由でセッションが破損している可能性があります。
ありがとう。