Hi,
I am learning Hibernate and have stored a User object to a users table, but when I query for the object I get the following error:
ERROR http-80-Processor25 org.hibernate.hql.PARSER - *** ERROR: users is not mapped.
WARN http-80-Processor25 org.apache.struts.action.RequestProcessor - Unhandled Exception thrown: class org.hibernate.hql.ast.QuerySyntaxError
ERROR http-80-Processor25 StandardWrapper[/ERSS:action] - Servlet.service() for servlet action threw exception
org.hibernate.hql.ast.QuerySyntaxError: users is not mapped. [from users]
at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:63)
Here is the query:
Code:
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
List users = session.createQuery("from users").list();
System.out.println(users.size() + " users found...");
for(Iterator iter = users.iterator(); iter.hasNext();) {
User user = (User)iter.next();
System.out.println(user.getUsername());
}
tx.commit();
HibernateUtil.closeSession();
User.hbm.xml:Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="persistence.data.User" table="users">
<id name="userid" column="userid" type="long">
<generator class="increment"/>
</id>
<property name="username" column="username" type="string" />
<property name="password" column="password" type="string" />
</class>
</hibernate-mapping>
User class:Code:
package persistence.data;
public class User {
private Long userid;
private String username;
private String password;
public User() {
}
public Long getUserid() {
return userid;
}
public void setUserid(Long id) {
this.userid = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
hibernate.cfg.xml:Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.datasource">java:comp/env/jdbc/erss</property>
<property name="show_sql">false</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="c3p0.min_size">10</property>
<property name="c3p0.max_size">100</property>
<property name="c3p0.timeout">18000</property>
<property name="c3p0.acquireRetryAttempts">30</property>
<property name="c3p0.acquireIncrement">5</property>
<property name="c3p0.automaticTestTable">C3P0TestTable</property>
<property name="c3p0.idleConnectionTestPeriod">36000</property>
<property name="c3p0.initialPoolSize">20</property>
<property name="c3p0.maxPoolSize">100</property>
<property name="c3p0.maxIdleTime">1200</property>
<property name="c3p0.maxStatements">50</property>
<property name="c3p0.minPoolSize">10</property>
<!-- Mapping files -->
<mapping resource="persistence/data/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
MySQL users table:
CREATE TABLE users
(
userid BIGINT NOT NULL AUTO_INCREMENT,
primary key (userid),
username VARCHAR(25) NOT NULL,
password VARCHAR(25) NOT NULL,
);
Hibernate log:
INFO main org.hibernate.cfg.Environment - Hibernate 3.0.5
INFO main org.hibernate.cfg.Environment - hibernate.properties not found
INFO main org.hibernate.cfg.Environment - using CGLIB reflection optimizer
INFO main org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
INFO main org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml
INFO main org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml
INFO main org.hibernate.cfg.Configuration - Mapping resource: persistence/data/User.hbm.xml
INFO main org.hibernate.cfg.HbmBinder - Mapping class: persistence.data.User -> users
INFO main org.hibernate.cfg.Configuration - Configured SessionFactory: null
INFO main org.hibernate.cfg.Configuration - processing extends queue
INFO main org.hibernate.cfg.Configuration - processing collection mappings
INFO main org.hibernate.cfg.Configuration - processing association property references
INFO main org.hibernate.cfg.Configuration - processing foreign key constraints
INFO main org.hibernate.util.NamingHelper - JNDI InitialContext properties:{}
INFO main org.hibernate.connection.DatasourceConnectionProvider - Using datasource: java:comp/env/jdbc/erss
INFO main org.hibernate.cfg.SettingsFactory - RDBMS: MySQL, version: 4.1.11-nt
INFO main org.hibernate.cfg.SettingsFactory - JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-3.1.10 ( $Date: 2005/05/19 15:52:23 $, $Revision: 1.1.2.2 $ )
INFO main org.hibernate.dialect.Dialect - Using dialect: org.hibernate.dialect.MySQLDialect
INFO main org.hibernate.transaction.TransactionFactoryFactory - Using default transaction strategy (direct JDBC transactions)
INFO main org.hibernate.transaction.TransactionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
INFO main org.hibernate.cfg.SettingsFactory - Automatic flush during beforeCompletion(): disabled
INFO main org.hibernate.cfg.SettingsFactory - Automatic session close at end of transaction: disabled
INFO main org.hibernate.cfg.SettingsFactory - JDBC batch size: 15
INFO main org.hibernate.cfg.SettingsFactory - JDBC batch updates for versioned data: disabled
INFO main org.hibernate.cfg.SettingsFactory - Scrollable result sets: enabled
INFO main org.hibernate.cfg.SettingsFactory - JDBC3 getGeneratedKeys(): enabled
INFO main org.hibernate.cfg.SettingsFactory - Connection release mode: null
INFO main org.hibernate.cfg.SettingsFactory - Maximum outer join fetch depth: 2
INFO main org.hibernate.cfg.SettingsFactory - Default batch fetch size: 1
INFO main org.hibernate.cfg.SettingsFactory - Generate SQL with comments: disabled
INFO main org.hibernate.cfg.SettingsFactory - Order SQL updates by primary key: disabled
INFO main org.hibernate.cfg.SettingsFactory - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
INFO main org.hibernate.hql.ast.ASTQueryTranslatorFactory - Using ASTQueryTranslatorFactory
INFO main org.hibernate.cfg.SettingsFactory - Query language substitutions: {}
INFO main org.hibernate.cfg.SettingsFactory - Second-level cache: enabled
INFO main org.hibernate.cfg.SettingsFactory - Query cache: disabled
INFO main org.hibernate.cfg.SettingsFactory - Cache provider: org.hibernate.cache.EhCacheProvider
INFO main org.hibernate.cfg.SettingsFactory - Optimize cache for minimal puts: disabled
INFO main org.hibernate.cfg.SettingsFactory - Structured second-level cache entries: disabled
INFO main org.hibernate.cfg.SettingsFactory - Statistics: disabled
INFO main org.hibernate.cfg.SettingsFactory - Deleted entity synthetic identifier rollback: disabled
INFO main org.hibernate.cfg.SettingsFactory - Default entity-mode: pojo
INFO main org.hibernate.impl.SessionFactoryImpl - building session factory
WARN main net.sf.ehcache.config.Configurator - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/C:/jakarta-tomcat-5.5.4/webapps/ERSS/WEB-INF/lib/ehcache-1.1.jar!/ehcache-failsafe.xml
INFO main org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
INFO main org.hibernate.impl.SessionFactoryImpl - Checking 0 named queries
Any ideas are appreciated. Thanks.