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 8027867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:56:44+00:00 2026-06-04T23:56:44+00:00

I am using hibernate. I need to create query below. It is possible the

  • 0

I am using hibernate. I need to create query below. It is possible the subContest is null in which case subContest_id=:cont should NOT be part of where clause. In conventional SQL when creating dynamic sql ; for such cases I used to do subContest_id = subContest_id ; BUT how do I achieve it in case below. Here If I try to query.setParameter( "cont", "subContest_id " ); it will give exception as cont is supposed to be an id and not a string.

    Query query = getEntityManager().createQuery( "select * from Upload where moderated=:mod, subContest_id=:cont order by lastModifiedTime desc " );
    query.setParameter( "cont", subContest.getId() );
  • 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-06-04T23:56:46+00:00Added an answer on June 4, 2026 at 11:56 pm

    you can try using criteria query in hibernate

    Criteria criteria = session.createCriteria(Upload.class);
    

    you can add your where clause as restrictions

    Criteria criteria = session.createCriteria(Upload.class)
        .add(Restrictions.eq("mode", value));
    

    in your case you want to check null before adding restriction

    if(subContest_id!=null){
            criteria.add(Expression.le("subContest_id",subContest_id));
        }
    

    and finally order by can be done using this

    Criteria criteria = session.createCriteria(Upload.class)
        .addOrder( Order.desc("lastModifiedTime") );
    

    please refer here for details on how to use criteria queries

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

Sidebar

Related Questions

When using Hibernate 2nd level cache and query cache and not specifying anything inside
I've got the following named query in Hibernate: <sql-query name=CreateLogTable> CREATE TABLE IF NOT
I am storing and retrieving Java Entities in a DB using Hibernate. I need
Hi i am using struts2 and hibernate in my project. I need to use
I am using ClassMetadata to determine the structure of a hibernate POJO. I need
I am developing a Java Web Application using JSF, Spring and Hibernate. I need
I query my Database using NHibernate. Now I need to restrict the data being
I'm working on creating a Hibernate query engine that can create dynamic queries. Thus
I need to replicate the following working HQL query using criteria API. session.CreateQuery( select
We are using grails-groovy with Hibernate, I have a SQL query like this in

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.