Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6839357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:42:33+00:00 2026-05-26T23:42:33+00:00

I am using hibernate detached criteria to get the results from a table (Oracle

  • 0

I am using hibernate detached criteria to get the results from a table (Oracle database). I need to fetch the results order by ascending some column. The column contains numbers but data type is varchar. If we don’t apply to_number() for a column then Oracle does ASCII comparison to order by. So, I need to apply to_number() for ordering the results as column contains all numbers but data type is varchar. Please find below query that I am using

public List<EntityClass> getResults(final String someinput) throws Exception {
    DetachedCriteria criteria = DetachedCriteria.forClass(EntityClass.class);
    criteria.add(Restrictions.eq("input", someinput));
    criteria.addOrder(Order.asc("someId")); // **here id column is of varchar type**. 

    List<EntityClass> results= this.getHibernateTemplate().findByCriteria(criteria);
    return results;
} 

criteria.addOrder(Order.asc("someId")); – here I need to apply to_number. We can achieve easily using SQL queries, but I am supposed to use only criteria queries.
Please help me how to achieve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:42:34+00:00Added an answer on May 26, 2026 at 11:42 pm

    I know this is possible with NHibernate using Projections.cast but AFAIK what you want is not possible this way with Hibernate.
    The only solution I see using only Criteria is by using a @Formula (although I have never done this myself):

    @Column
    private String someId;
    
    @Formula(value="to_number(someId)")
    private Long someIdNumber;
    
    ...
    
    criteria.addOrder(Order.asc("someIdNumber"));
    

    Personally I would consider ordering your results in Java (with a comparator) because whatever solution you choose your queries will depend on a specific DBMS.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When using hibernate to retrieve data from Oracle 11g DB using either org.hibernate.dialect.Oracle10gDialect or
When using hibernate criteria just altering the join type affects the results of the
I need to detach some entity objects from the database to make them unmanaged.
I am using hibernate and i want to display only those records of table
i am experimenting using Hibernate. I am trying to get a User by id
I'm using JPA 1, Hibernate and Oracle 10.2.0 and my entities are defined like
Using hibernate for data access. I have a column in database: varchar(40), default value
I am using Hibernate on a JBoss server. I get the error below. The
I'm using Hibernate with Spring in my application. I have been consistently using detached
Using Hibernate 3.6.8.Final and Spring 3.0.5.RELEASE , I'm trying to add some Common DAO

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.