permaculture playing cards*
The moose likes Object Relational Mapping and the fly likes EVENTS is not mapped [FROM EVENTS] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "EVENTS is not mapped [FROM EVENTS]" Watch "EVENTS is not mapped [FROM EVENTS]" New topic
Author

EVENTS is not mapped [FROM EVENTS]

amar rajula
Greenhorn

Joined: Sep 01, 2007
Posts: 3
Hi,
i am new to hibernate, i am following with hibernate tutorial.
DB : HSQLBD


storing records succussfully but Could not able to retrive the records.
Facing problem with table mapping.

ERROR :


run:
[java] Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxExceptio
n: EVENTS is not mapped [FROM EVENTS]
[java] at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassP
ersister(SessionFactoryHelper.java:158)
[java] at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(
FromElementFactory.java:87)
[java] at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClau
se.java:70)
[java] at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWa
lker.java:255)
[java] at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBa
seWalker.java:3056)
[java] at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlS
qlBaseWalker.java:2945)
[java] at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBas
eWalker.java:688)
[java] at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalk
er.java:544)
[java] at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlS
qlBaseWalker.java:281)
[java] at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBase
Walker.java:229)
[java] at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTransl
atorImpl.java:228)
[java] at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTran
slatorImpl.java:160)
[java] at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTransl
atorImpl.java:111)
[java] at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.j
ava:77)
[java] at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.j
ava:56)
[java] at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(Que
ryPlanCache.java:72)
[java] at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(Abstra
ctSessionImpl.java:133)
[java] at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSe
ssionImpl.java:112)
[java] at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1
623)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAcces
sorImpl.java:39)
[java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMet
hodAccessorImpl.java:25)
[java] at java.lang.reflect.Method.invoke(Method.java:585)
[java] at org.hibernate.context.ThreadLocalSessionContext$TransactionPr
otectionWrapper.invoke(ThreadLocalSessionContext.java:301)
[java] at $Proxy0.createQuery(Unknown Source)
[java] at events.EventManager.listEvents(Unknown Source)
[java] at events.EventManager.main(Unknown Source)
[java] Java Result: 1

code:

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="EVENTS" >
<id name="id" column="EVENT_ID">
<generator class="increment"/>
</id>
<property name="date" type="timestamp" column="EVENT_DATE"/>
<property name="title"/>
</class>

</hibernate-mapping>


EventManager.java
----------------
package events;
import org.hibernate.Query;
import org.hibernate.Session;


import java.util.Date;
import java.util.List;


import util.HibernateUtil;

public class EventManager {

public static void main(String[] args) {
EventManager mgr = new EventManager();

if (args[0].equals("store")) {
mgr.createAndStoreEvent("My Event", new Date());
}else if (args[0].equals("list")) {
List events = mgr.listEvents();
for (int i = 0; i < events.size(); i++) {
Event theEvent = (Event) events.get(i);
System.out.println("Event: " + theEvent.getTitle() +
" Time: " + theEvent.getDate());
}
}

HibernateUtil.getSessionFactory().close();
}

private void createAndStoreEvent(String title, Date theDate) {

Session session = HibernateUtil.getSessionFactory().getCurrentSession();

session.beginTransaction();

Event theEvent = new Event();

theEvent.setTitle(title);
theEvent.setDate(theDate);

session.save(theEvent);

session.getTransaction().commit();


}
private List listEvents() {

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();

List result = session.createQuery("FROM EVENTS").list();

session.getTransaction().commit();
return result;
}

}

please any body can help me?

Thanks in advance.

regards,
Amar
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Remember in HQL you are querying classes not tables. Your class is called Event, not Events.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Edvins Reisons
Ranch Hand

Joined: Dec 11, 2006
Posts: 364
In a HQL query, it is the name of the class (Event), and not the name of the database table (EVENTS), that you need to specify in the FROM clause.
amar rajula
Greenhorn

Joined: Sep 01, 2007
Posts: 3
It is working now.
Thank you very much for you both.
 
 
subject: EVENTS is not mapped [FROM EVENTS]