• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Table not mapped.

 
Vamsi Mohan Pemmaraju
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, we will repost your code surrounded by the CODE tags, which get generated when you click the "CODE" button underneath the "Add Reply" button when posting on JavaRanch, so that we have your code indentation there and makes it easier to read.



Mark
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So now that I see your code easier to read, it is simple, your HQL is incorrect.

You have "from EVENT", so Hibernate is looking for a class called, with the same all-upper case, "EVENT", but your mapped class is "Event", so change your HQL from "from EVENT" to "from Event"

And it is preferred to call list() on the Query object to get the results. Also use Query interface instead of (QueryImpl), it is always better to code to an interface than a concrete class.

Mark
 
Vamsi Mohan Pemmaraju
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark,

Your suggestion was very useful. I have solved the my problem.

Regards,
Vamsi Mohan
[ July 16, 2007: Message edited by: Vamsi Mohan Pemmaraju ]
 
Vamsi Mohan Pemmaraju
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem with <composite-id>
---------------------------

I have two table definitions:

COMPONENT : CMPNT_ID NUMBER(3), CMPNT_TECH_ID, CMPNT_NM VARCHAR2(50)
COMPONENTTECH : CMPNT_TECH_ID NUMBER(3), CMPNT_TECH_NM VARCHAR2(60)

THE DEFINITIONS FOR THE *.HBM.XML ARE AS BELOW:
1) COMPONENT.HBM.XML
<hibernate-mapping>
<class name="events.Component" table="COMPONENT">
<composite-id name="cmpnt_id" class="events.Component">
<key-property name="cmpnt_id"/>
<key-property name="cmpnt_tech_id"/>
</composite-id>
<property name="cmpnt_nm"/>


<many-to-one name="cmpnt_tech_id" class="events.ComponentTech" insert="false" update="false">
<column name="cmpnt_tech_id"/>
</many-to-one>
</class>
</hibernate-mapping>

2) COMPONENTTECH.HBM.XML
<hibernate-mapping schema="PRTL_S01">
<class name="events.ComponentTech" table="COMPONENTTECH">
<id name="cmpnt_tech_id" type="long" unsaved-value="null">
<column name="cmpnt_tech_id" sql-type="number" not-null="true"/>
</id>
<property name="cmpnt_tech_nm" type="string" column="CMPNT_TECH_NM" not-null="true"/>
</class>
</hibernate-mapping>

My code was:
public String retrieve(Long cmpnt_id) {
Component e = (Component)session.load(Component.class.getName(), cmpnt_id);
return e.getCmpnt_nm();
}

I was not succesful in executing with the above configurations. the error was:
org.hibernate.TypeMismatchException: Provided id of the wrong type. Expected: class events.Component, got class java.lang.Long

Another thing is, do we need to have individual .hbm.xml for each class or can we club two class definitions in one. If yes, are there any restrictions or conditions.

Regards,
Vamsi Mohan
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vamsi, can you actually post your last question as a new thread. It is a different problem, and therefore you might get better responses as a new thread rather than adding to this one.

Thanks

Mark
 
xuehua yu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark,I'm just encountered this problem,and your suggestion really helps. :lol:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic