Hi,
I am new to hibernate and have come across some hurdles which I could solve.
I am stuck with one error saying Table is not mapped. I am using Oracle DB.
Event.hbm.xml:
<?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="events.Event" table="Event" schema="test">
<id name="event_id" type="string" unsaved-value="null">
<column name="EVENT_ID" sql-type="varchar2" not-null="true"/>
</id>
<property name="event_nm" type="string" column="EVENT_NM" not-null="true"/>
</class>
</hibernate-mapping>
HIBERTNATE.CFG.XML:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/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
racle:thin:@test:1521:TEST</property>
<property name="connection.username">test</property>
<property name="connection.password">test</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping resource="Event.hbm.xml"/>
</session-factory>
</hibernate-configuration>
EVENTMANAGER.JAVA:
package events;
import org.hibernate.Session;
import org.hibernate.impl.QueryImpl;
import java.util.Date;
import java.util.ArrayList;
import java.util.Iterator;
import java.sql.SQLException;
import util.HibernateUtil;
public class EventManager {
public static void main(String[] args) {
EventManager mgr = new EventManager();
try {
//mgr.store(new Integer(1), new Integer(1));
mgr.store("5","abc");
} catch(Exception sqle) {
sqle.printStackTrace();
}
HibernateUtil.getSessionFactory().close();
}
private void store(String env_id, String title) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
QueryImpl impl = (QueryImpl)session.createQuery("from EVENT");
Iterator it = impl.iterate();
while(it.hasNext()) {
Event event = (Event)it.next();
System.out.println(event.getEvent_id());
System.out.println(event.getEvent_nm());
}
session.getTransaction().commit();
}
}
Aftr executing the above script; I recieve the message:
D:\projects\springHibn>ant run
Buildfile: build.xml
prepare:
[copy] Copying 1 file to D:\projects\springHibn\war\WEB-INF
[copy] Copying 1 file to D:\projects\springHibn\war\properties
compile:
run:
[java] log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
[java] log4j:WARN Please initialize the log4j system properly.
[java] org.hibernate.hql.ast.QuerySyntaxException: TEST.EVENT is not mapped [from EVENT]
[java] at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:158)
[java] at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:87)
[java] at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:70)
[java] at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:255)
[java] at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3056)
[java] at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2945)
[java] at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688)
[java] at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:544)
[java] at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
[java] at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
[java] at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:228)
[java] at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
[java] at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
[java] at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
[java] at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
[java] at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
[java] at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
[java] at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
[java] at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[java] at java.lang.reflect.Method.invoke(Method.java:324)
[java] at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
[java] at $Proxy0.createQuery(Unknown Source)
[java] at events.EventManager.store(EventManager.java:44)
[java] at events.EventManager.main(EventManager.java:17)
It would be nice if anyone provides me the solution.