-->
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.  [ 2 posts ] 
Author Message
 Post subject: NamedNativeQuery failing with unknown column
PostPosted: Tue Jul 14, 2009 11:11 am 
Beginner
Beginner

Joined: Fri Sep 12, 2008 10:55 am
Posts: 33
When attempting to use a @NamedNativeQuery, hibernate appears to be attempting to get the results based on the automatic column alias' it typically generates for standard NamedQuery items.

Here is the made up code...

Code:
@NamedNativeQuery( name = "FindFields",
  resultSetMapping = "FindFieldsResultSet",
  query = "SELECT X.field1 FIELD1, Y.field2 FIELD2, Z.field3 FIELD3 from X, Y, Z ..." )


SQL Result set mapping
Code:
@SqlResultSetMapping( name = "FindFieldsResultSet", entities = {
    @EntityResult( entityClass = X.class,
       fields = { @FieldResult( name = "field1", column = "FIELD1" ) } ),
    @EntityResult( entityClass = Y.class,
       fields = { @FieldResult( name = "field2", column = "FIELD2" ) } ),
    @EntityResult( entityClass = Z.class,
       fields = { @FieldResult( name = "field3", column = "FIELD3" ) } ) } )


Code:
Query query = entityManager.createNamedQuery( "FindFields" );
List<Object[]> results = query.getResultList();


What I end up with on my query is:
ERROR JDBCExceptionReporter - Invalid argument: unknown column name X1_71_0_

Why is hibernate attempting to use column alias' that do not exist in the result set? Debugging, I see the query as my query above, however, when hibernate is trying to do a resultSet.getInt(name), name appears to be the X1_71_0_, NOT my defined alias! It fails at this point because those column alias' do not even exist.


Top
 Profile  
 
 Post subject: Re: NamedNativeQuery failing with unknown column
PostPosted: Tue Jul 14, 2009 11:28 am 
Beginner
Beginner

Joined: Fri Sep 12, 2008 10:55 am
Posts: 33
Side note:

if I changed the SQL query to:

X.*, Y.*, Z.*

It works. My guess is that it is attempting to use the pre-defined query for those 3 entity classes, and trying to put mappings together from my query, but it isn't working. I can go with .* for now, but it really doesn't make sense if I'm only after a few minor fields in an entity class.

The reason I'm doing this is there are rare cases that I need to join to other tables that I don't want to create @OneToMany in my entities across these XYZ classes, they aren't exactly related per se, but I need data across them to do a unit of work.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.