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.  [ 4 posts ] 
Author Message
 Post subject: strange problem with max function
PostPosted: Sat Jul 29, 2006 5:48 pm 
Newbie

Joined: Sat Jul 29, 2006 5:24 pm
Posts: 2
HI!
I am using hibernate 3.0 with mysql 5.0.22 .
I have a table called tradingday with two columns id as primary key and date ad Date type.
I want to have the biggest date so I wrote the sql : select max(date) from tradingday which works fine if i write it directly into mysql. When I am trying to run it with hibernate I am having a strange exception "Column id0_ not found". If I change the sql to : select max(id) as id0_, max(date) as date3_0_ from tradingday it works great. Am I missing something? I cant understand the problem ... thanks for any help
Ziv


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 31, 2006 7:49 am 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
Post your java code.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 31, 2006 1:31 pm 
Newbie

Joined: Sat Jul 29, 2006 5:24 pm
Posts: 2
LOGGER.debug(DataFetcherUtil.class, "quering data base.the sql is:"+sql);
Session session = HibernateSessionFactory.currentSession();
SQLQuery query = session.createSQLQuery("select max(id) as id0_, max(date) as date3_0_ from tradingday");//.addEntity("securityId", Security.class);
Set <String> keys = entities.keySet();
Class clazz = null;
for(String key:keys){
clazz = entities.get(key);
query.addEntity(key, clazz);
}

return query.list();




You can see the sql now that works. If i ommit the id0_ and date3_0_ it doesn't work

thanks,
Ziv


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 01, 2006 4:22 am 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
I think that the problem is that your are using two functions with the same name (max) and no alias. That problem also arises when you join tables with the same column names. The solution is simply to name the columns with an alias to get them distinguished. That's what you get with max(id) as id0_, max(date) as date3_0_ but I think you could get the same with max(id) as max_id, max(date) as max_date.

What I don't understand is how you use Query.addEntity(String, Class).
Does it works? Could you explain it?


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