-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: could not load an entity: [model.Event#1]
PostPosted: Thu Aug 28, 2008 11:20 am 
Newbie

Joined: Thu Aug 28, 2008 11:06 am
Posts: 3
I'm just getting started with Hibernate. I'm loosely following the Hibernate chapter of _Agile Java Development_ and I've created the following:

A Oracle10g DB table called events
id numeric
event_type_id numeric
resource_id numeric
event_date date
comments varchar2(200)

A model called Event:
public class Event {
private int id ;
private int eventTypeId ;
private int resourceId ;
private Date eventDate ; // note this is a java.sql.Date
private String comments ;

The Hibernate mapping is:
<hibernate-mapping>
<class name="model.Event" table="Events">
<id name="id" column="id">
<generator class="assigned"/>
</id>
<property name="eventTypeId" column="event_type_id"/>
<property name="resourceId" column="resouce_id"/>
<property name="eventDate" column="event_date"/>
<property name="comments" column="comments"/>
</class>
</hibernate-mapping>

Using a simple test program to verify that I can access Hibernate:
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Event event;

event = (Event) session.get ( Event.class, 1);
... etc

I get the error:

[java] Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not load an entity: [model.Event#1]

I'm guessing that I have not correctly configured so that Hibernate knows that id is a primary key. But I'm stuck as to what to try next.

Any help or guidance would be appreciated,
-=beeky


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 28, 2008 1:41 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The error message says that there is some problem with the SQL that Hibernate generated. Turn on logging of SQL statments to see what Hibernate sent to the database. Try running the same SQL manually on the database. How to turn on logging is described here http://www.hibernate.org/hib_docs/v3/re ... on-logging


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 28, 2008 1:49 pm 
Newbie

Joined: Thu Aug 28, 2008 11:06 am
Posts: 3
It seems like lately I'm always answering my own questions. Either I'm getting better at this or the world is getting worse.

My problem was twofold:

1. I had a typo in my cfg file:
<property name="resourceId" column="resouce_id"/>
should have been:
<property name="resourceId" column="resource_id"/>

2. I misunderstood the syntax of the createQuery param list, I used the TABLE name when I should have used the OBJECT name so:
correct, Event is the object that a table row maps to:
List eventsList = session.createQuery("from Event").list();
incorrect, events is the table name
List eventsList = session.createQuery("from events").list();

I did not discover the problem with 2. I was searching for information about the specific exception that Hibernate was throwing, org.hibernate.hql.ast.QuerySyntaxException, and found the following:
http://forums.devshed.com/java-help-9/q ... 18157.html
see answer #4 on this page.

-=beeky


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.