Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate getting started

 
Franck McGeough
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stan Sokolov
Ranch Hand
Posts: 120
Hibernate IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure that query can be called without select clause
 
Scott Johnson
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic