-->
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.  [ 12 posts ] 
Author Message
 Post subject: String concatenation in select clause
PostPosted: Wed Nov 19, 2003 12:01 pm 
Newbie

Joined: Tue Aug 26, 2003 9:29 am
Posts: 12
Currently it is not possible to use something like

Code:
select object.str1 || object.str2 || object.str3 from MyClass as object


Is there a easy workaround (yes, I know there are, but I want to have just ONE result back instead of 3 because of a design issue) or are there any plans to implement this functionality???

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 19, 2003 12:12 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
There are plans already in action to reimplement HQL using a new ANTLR parser. Perhaps in that timeline (Hibernate 2.2)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 19, 2003 12:13 pm 
Newbie

Joined: Wed Nov 12, 2003 11:16 am
Posts: 9
This is an interesting question as I've come across something similar. Instead of concatenation, I was trying to perform some mathematical functions on the properties from a queried Object:

Code:
select obj.a, obj.b, (obj.a + obj.b) from Object obj


This fails from Hibernate, so what I've done is include the calculation in my DAO layer. So my Business Layer will see a List that has a, b, and (a+b).

a little code snippet from my DAO

Code:
Iterator it = queryresultlist.iterator();
while(it.hasNext()) {
  List newvallist = new ArrayList();
  a = 0;
  b = 0;
         
  Object[] vals = (Object[])it.next();
  for( int i=0; i< vals.length; i++) {
           newvallist.add(vals[i]);
           if( i == 0 ){
                a = new Integer( vals[i].toString() ).intValue();
           }
           if( i == 1 ){
                b = new Integer( vals[i].toString()).intValue();
           }
  }
         
  Integer tot = new Integer( (a+b) );
  newvallist.add(tot);

  newqueryresultlist.add(newvallist.toArray());
}

return newqueryresultlist;



IMHO, if you haven't learned about DAO as part of a design pattern, you really should search in this forum for DAO threads... very enlightnening. It will make your code much more portable and flexible.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 19, 2003 12:18 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
It would be cool if we could do something like this:

Code:
select new StringBuffer(object.str1).append(object.str2).append(object.str3).toString() from MyClass as object

Don't panic :)


Top
 Profile  
 
 Post subject: a solution - but another problem
PostPosted: Wed Nov 19, 2003 1:11 pm 
Newbie

Joined: Tue Aug 26, 2003 9:29 am
Posts: 12
Got a solution by defining an object

Code:
class MyClass {
   private String str1;
   private String str2;
   private String str3;

   public MyClass (String str1,String str2, String str3);
      this.str1 = str1;
      this.str2 = str2;
      this.str3 = str3;
   }

   public String toString() {
        return str1+ str2+ str3;
    }
}


and then:

Code:
select new MyClass(str1,str2,str3) from MyClass as object


works, but returns 3 times the Object instead of one (is there a bug in the parser maybe, because of the ',' ) ???

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 19, 2003 1:14 pm 
Newbie

Joined: Tue Aug 26, 2003 9:29 am
Posts: 12
Correction: I mean

Code:
select new MyClass(object.str1,object.str2,object.str3) from MyClass as object


but still the same - a List of Object[3] rows instead of a List of Objects

p.s. Hibernate stable latest 2.0.3
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 20, 2003 4:51 am 
Newbie

Joined: Tue Aug 26, 2003 9:29 am
Posts: 12
Anyone an idea???


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 20, 2003 10:30 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
You have three records in the table. Maybe.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 20, 2003 10:38 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
I don't believe you.

If you can reproduce this in a VERY simple main method (I mean like one persistent class with 3 properties and a main method), submit it to JIRA.

But I think you are mistaken.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 21, 2003 5:06 am 
Newbie

Joined: Tue Aug 26, 2003 9:29 am
Posts: 12
I'll try that later

- for your info - the select has been done using hibern8ide-0.9; maybe it's a bug in that tool


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2003 5:18 am 
Newbie

Joined: Tue Aug 26, 2003 9:29 am
Posts: 12
Just to confirm it works fine using my suggestion - the bug was related to hibern8ide...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2003 2:49 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
razzi wrote:
I'll try that later

- for your info - the select has been done using hibern8ide-0.9; maybe it's a bug in that tool



A bug in hibern8ide - no way! ;)

It should just return a list of MyClass objects - but i doesn't, or ?

_________________
Max
Don't forget to rate


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