Meaningless Drivel is fun!*
The moose likes Object Relational Mapping and the fly likes Hibernate getting started Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate getting started" Watch "Hibernate getting started" New topic
Author

Hibernate getting started

Franck McGeough
Greenhorn

Joined: Oct 27, 2005
Posts: 26
So, I'm on my 2nd day of looking at Hibernate. I decided to try a real simple example and code from scratch and I'm totally puzzled at the moment. I have a table in sql server with 3 columns. The primary key is an identity column. the other two columns are simple varchar fields. I'm using Microsoft SQL Server.

I can connect to the database, insert rows, update the rows but when I attempt to do a query an exception is thrown in getImportedClassName(). I have a hibernate.cfg.xml that defines that I have one mapping stored in the TestTable.hbm.xml file. My query looks like this :

List items = session.createQuery("from testtable").list();

is there some other configuration thing that I need to twiddle to make this work? Also, are there particular things to read beside the "Hibernate Reference Doc" and "Hibernate In Action" book for people getting started? I'm using Hibernate 3.1. Thanks.
Franck McGeough
Greenhorn

Joined: Oct 27, 2005
Posts: 26
The stack trace on the exception is :

at org.hibernate.hql.ast.ASTQueryTranslatorFactory.createQueryTranslator(ASTQueryTranslatorFactory.java:31)
at org.hibernate.impl.SessionFactoryImpl.createQueryTranslators(SessionFactoryImpl.java:410)
at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:469)
at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:1060)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1010)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)

I've got the following hibernate related jar's in the classpath :

ant-antlr-1.6.5.jar
asm-attrs.jar
asm.jar
cglib-2.1.2.jar
commons-collections-2.1.1.jar
commons-logging-1.0.4.jar
dom4j-1.6.1.jar
ehcache-1.1.jar
hibernate3.jar
jaxen-1.1-beta-7.jar
jdbc2_0-stdext.jar
jta.jar
log4j-1.2.11.jar
xml-apis.jar
Stas Sokolov
Ranch Hand

Joined: Apr 13, 2004
Posts: 117

I am not sure that query can be called without select clause


Good luck for yourself.
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
Stas,

In Hibernate the select clause is optional so "from mytable" is a valid query.

Franck,

The class names in Hibernate queries are case sensitive so your query should be "from TestTable" if your class name is TestTable.

What is the actual exception being thrown? The stack trace above is missing the first line that describes the exception.

Finally, Hibernate Quickly is a good book for Hibernate beginners.
Vinnie Jenks
Ranch Hand

Joined: Apr 26, 2004
Posts: 207
In HQL you want to query the domain-object name, not the table name, for example:



If your class is mapped correctly the query will translate this for you.

HTH.

-v
Franck McGeough
Greenhorn

Joined: Oct 27, 2005
Posts: 26
Got it. Thanks, that worked like a charm. Javaranch rocks! I'll get over my kneejerk "lets just code this in OCI/ODBC C/C++" now. h
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate getting started