-->
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.  [ 6 posts ] 
Author Message
 Post subject: Sorting fields of nText type
PostPosted: Thu Jan 29, 2009 3:42 am 
Newbie

Joined: Wed Jan 28, 2009 10:04 pm
Posts: 5
Location: Melbourne/Singapore
Hi,
I wish to sort my records retrieved by a column field that is of data type nText.
i have a similar problem as this one "http://forum.hibernate.org/viewtopic.php?p=2386961" from trying to sort column fields of nText data type. But that post has no reply yet.
Checking the forums, i cast the nText to varchar(4000) with "cast(Cat as varchar(4000))". But this returned a syntax error..
Some other forums have suggested this: "cast(description, varchar(4000))"
But this returned a null exception...

This is the line that caused the exception: description is nText
getHibernateTemplate().find("from Cat order by cast(description as varchar(4000) ASC);

Any help would be appreciagted. Thanks.

Hibernate version:
version="1.0" encoding="UTF-8"
Hibernate/Hibernate Mapping DTD 3.0//
Mapping documents:
<class name="Cat" table="tbl_cat" lazy="true">
<id name="id" >
<generator class="native"/>
</id>

<property name="description"/>

<!-- Added a many-to-one for website -->
<many-to-one name="website" class="Website" column="websiteId" not-null="false" not-found="ignore"
unique="true" cascade="none" lazy="false" />
</class>
Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:
nested exception is org.hibernate.hql.ast.QuerySyntaxException: expecting CLOSE, found '(' near line 1, column 81 [from Cat order by cast(description as varchar(4000)) ASC]

Name and version of the database you are using:
MS SQL Server Express 2005

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

Cat.java:
public class Cat extends BaseObject implements Serializable {
. . .
private String description;
...
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 29, 2009 4:52 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
I don't know if its possible at all to use "cast" in the order by, but you could try the following: "from Cat c order by (cast(c.description as varchar(4000))) ASC"

Is cast supported in MSSQL? Isn't it "convert"?

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Sorting fields of nText type
PostPosted: Thu Jan 29, 2009 6:12 am 
Newbie

Joined: Wed Jan 28, 2009 10:04 pm
Posts: 5
Location: Melbourne/Singapore
Thanks for your reply.
I've tried the following from your response.
Hmm, i think i'm getting close.. My basic idea is really to cast any "nText/clob" to something sortable. But none of the syntax from the various forums seem to work... Or is there another approach

1) "from Cat c order by (cast(c.description as varchar(4000))) ASC"
output: returns the same exception as what i had earlier

2) 1) "from Cat c order by (convert(c.description, varchar(4000))) ASC"
output: SQL Error: 195, SQLState: S00010
'varchar' is not a recognized built-in function name.
could not execute query; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query

3) "from Cat c order by (cast(c.description, varchar(4000))) ASC"
4) "from Cat c order by (convert(c.description as varchar(4000))) ASC"

output: both returned a java.util.ArrayList that is null, which it shouldn't be null.


Top
 Profile  
 
 Post subject: Sorting fields of nText type
PostPosted: Thu Jan 29, 2009 6:12 am 
Newbie

Joined: Wed Jan 28, 2009 10:04 pm
Posts: 5
Location: Melbourne/Singapore
Thanks for your reply.
I've tried the following from your response.
Hmm, i think i'm getting close.. My basic idea is really to cast any "nText/clob" to something sortable. But none of the syntax from the various forums seem to work... Or is there another approach

1) "from Cat c order by (cast(c.description as varchar(4000))) ASC"
output: returns the same exception as what i had earlier

2) 1) "from Cat c order by (convert(c.description, varchar(4000))) ASC"
output: SQL Error: 195, SQLState: S00010
'varchar' is not a recognized built-in function name.
could not execute query; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query

3) "from Cat c order by (cast(c.description, varchar(4000))) ASC"
4) "from Cat c order by (convert(c.description as varchar(4000))) ASC"

output: both returned a java.util.ArrayList that is null, which it shouldn't be null.


Top
 Profile  
 
 Post subject: Syntax error
PostPosted: Fri Jan 30, 2009 4:20 am 
Newbie

Joined: Wed Jan 28, 2009 10:04 pm
Posts: 5
Location: Melbourne/Singapore
after fiddling some time, this solved the problem...It was really just finding the right syntax...
convert( nvarchar(4000), description)
It seems hibernate uses convert while sql uses cast...
Thanks all.


Top
 Profile  
 
 Post subject: Re: Sorting fields of nText type
PostPosted: Thu Dec 29, 2011 3:12 pm 
Newbie

Joined: Thu Dec 29, 2011 3:09 pm
Posts: 1
Thank You Newbie.
I learnt two things here .
instead of CAST , need to use CONVERT
and then correct syntax of using this.

Thanks again.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.