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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:56:46+00:00 2026-06-03T11:56:46+00:00

Im creating a hibernate criteria that will check if a contract is/has been valid

  • 0

Im creating a hibernate criteria that will check if a contract is/has been valid during a year. The contract has a start and end-date as a standard date-field.

So being used to SQL-SERVER, i would think something in the lines of this SQL:

SELECT * FROM CONTRACT WHERE YEAR(startDate) <= ? AND YEAR(endDate) >= ?

The questionmark is the given year as an integer.

So how to transform this to a hibernate criteria?

int year = 2011; //Just a testyear for example purposes.
DetachedCriteria criteria = DetachedCriteria.forClass(Contract.class)
    .add(Restrictions.le("startDate", year))
    .add(Restrictions.ge("endDate", year));

As you can see, i am missing the bit where i convert the date’s to a year so it can be compared. Naturally my code does not run as it is.

Any suggestion on how to get the desired results?

  • 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-03T11:56:48+00:00Added an answer on June 3, 2026 at 11:56 am

    It seem like the API does not directly support what you wanted, but there is workaround that you might consider.

    Solution 1:

    DetachedCriteria criteria = DetachedCriteria.forClass(Contract.class)
        .add(Restrictions.le("startDate", toEndOfYear(year))
        .add(Restrictions.ge("endDate", toStartOfYear(year)));
    
    public Date toStartOfYear(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.DAY_OF_YEAR, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }
    
    public Date toEndOfYear(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.DAY_OF_YEAR, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND,-1);
        return calendar.getTime();
    }
    

    Example output of the two methods:

    System.out.println(toStartOfYear(2013));
    System.out.println(toEndOfYear(2013));
    
    Mon Dec 31 00:00:00 MYT 2012
    Sun Dec 30 23:59:59 MYT 2012
    

    Solution 2:

    DetachedCriteria criteria = DetachedCriteria.forClass(Contract.class)
        .add(Restrictions.sqlRestriction("YEAR(startDate) >= ? ", year,Hibernate.INTEGER))
        .add(Restrictions.sqlRestriction("YEAR(endDate) <= ? ", year,Hibernate.INTEGER));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on creating a Hibernate query engine that can create dynamic queries. Thus
Hibernate 3.3 If I've got code that is dynamically creating hibernate hql queries like
I have a class that I am creating a Hibernate mapping which contains a
Last night I started working on an NHibernate provider. I'm creating a criteria that
I'm using Hibernate Query by Example (QbE) with a pojo that has a lot
I am creating a Fluent N hibernate subclass mapping that currently looks something like
I was trying to create Hibernate Validator bean, and run into this problem creating
I'm having the following issue with my hibernate 3.6.10 project: org.springframework.beans.factory.BeanCreationException: Error creating bean
Creating liquid layouts is an immense pain. Now, I totally understand that tables should
Creating a server-side socket will fail if I'm trying to use the same port

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.