
Originally Posted by
karldmoore
The reason for the weird looking names is purely because Hibernate has generated them for you.
After some more testing, it has become clear that the query works if every quantity is explicitly queried for:
Code:
List subscriptions = getSession().createSQLQuery("select "
+ " p.ID,"
+ " p.ProductTypeID,"
...
+ " s.PeriodicPrice,"
...
+ " from"
+ " subscription s"
+ " inner join"
+ " product p "
+ " on s.ID = p.ID")
.addEntity("s", Subscription.class)
.list();
Hibernate generates the query:
Code:
select p.ID, p.ProductTypeID, ..., s.PeriodicPrice, ... from subscription s inner join product p on s.ID = p.ID
which works. The query Hibernate executes appears to be unchanged from the one I input in this case.
However, if I use asterisks to 'select all' in a slightly simpler query, like this:
Code:
List subscriptions = getSession().createSQLQuery("select "
+ " {s.*} "
+ " from"
+ " subscription "
+ " inner join"
+ " product p "
+ " on s.ID = p.ID")
.addEntity("s", Subscription.class)
.list();
i get the following errors:
Code:
Hibernate: select s.ID as ID9_0_, s_1_.Description as Descript2_9_0_, s_1_.DoSendAboInfo as DoSendAb3_9_0_, s_1_.IsDeleted as IsDeleted9_0_, s_1_.ItemID as ItemID9_0_, s_1_.OurItemNr as OurItemNr9_0_, s_1_.Price as Price9_0_, s_1_.Title as Title9_0_, s_1_.ProductTypeID as Product10_9_0_, s_1_.VendorID as VendorID9_0_, s.IsDefaultSub as IsDefaul2_67_0_, s.IsVisible as IsVisible67_0_, s.PeriodicPrice as Periodic4_67_0_, s.PortingText as PortingT5_67_0_, s.SortWeight as SortWeight67_0_, s.SubscriptionURL as Subscrip7_67_0_, s.ContractPeriodID as Contract8_67_0_, s.ID as ID67_0_ from subscription s inner join product p on s.ID = p.ID
2007-06-26 11:54:21,891 WARN [org.hibernate.util.JDBCExceptionReporter] - SQL Error: 1054, SQLState: 42S22
2007-06-26 11:54:21,891 ERROR [org.hibernate.util.JDBCExceptionReporter] - Unknown column 's_1_.Description' in 'field list'
Basically, it appears that if Hibernate is doing any mapping whatsoever the SQL gets mangled beyond recognition, with Hibernate creating aliases such as s_1_ out of thin air without even defining them. I've tried a number of different combinations using various combinations of addEntity and addJoin and including {p.*} in the selection, but the query always fails.
Is this an indication that there are annotations missing from my .java class files? Before I make this post even longer by including those files, do I need to specify explicit annotation mapping rules in those files for a native SQL 'select all' to work as expected? Are there any other obvious problems with what I'm doing? I'd like to mention again though, that HQL queries work just fine, which is why I'm surprised to see so many problems with native SQL.