Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Problem with named queries

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good find !
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic