• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with named queries

 
Howard Ralston
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am trying to use a named query but cannot get it to work.

The mapping element is:


I am calling it like this:



The pertinent section of the log looks like this:



Any ideas?

Thanks,

Howard
 
Scott Johnson
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your log above, the query is referenced as "events.TsnEvent.findEventsByGroup".

Try using the full name:

Query q = session.getNamedQuery("events.TsnEvent.findEventsByGroup").setInteger("id", new Integer(1100));
 
Howard Ralston
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Scott,

I tried that and also changed the variable "id" to match "groupId" and used the alias "r". Now I get a different error. It looks as though Hibernate is trying to run the query but generates an exception. The relevant error seems to be: "ResultSet can not re-read row data for column 3." See below.

Mapping:



Code:



Error:

 
Howard Ralston
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I turned on more debugging and found the following. It looks as though Hibernate is pulling 1 record, closing the statment and resultset and then trying to read it again. Is that the case? If so, why? Shouldn't it bring back the full 27 records and then iterate through them?

Here is the new log:

 
Howard Ralston
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I figured it out.

SQL Server requires that you read the columns sequentially into your resultset. I had changed the mapping around and the get() statements were no longer sequential. If you use a tool such as Hibernate Synchronizer (like I did to create your java files, make sure that you keep the property elements in the same order.

If your columns are retrieved in the order:
1. id
2. myname
3. mydate
4. mycity

Your get statements must retrieve in the same order:
1. getId()
2. getMyname()
3. getMydate()
4. getMycity()

If not you will the the error I had.
 
Sabarish Sasidharan
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good find !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic