2

i have an hibernate entity called user.I want to get the list of users who are between two dates a date interval. Example, I have a date D1 and I want to know the people hired between my hiDate and my depDate. Here is the mapping of the hibernate file of my user entity

<class name="com.bam.model.User" table="USER">
        <composite-id class="com.bam.model.UserId" name="id">
            <key-property name="idUser" type="java.lang.String">
                <column name="ID_USER" not-null="true" precision="0" scale="30" sql-type="varchar"/>
            </key-property>
            <key-property name="idCompany" type="java.lang.String">
                <column name="ID_COMPANY" not-null="true" precision="0" scale="30" sql-type="varchar"/>
            </key-property>
        </composite-id> 
        <property name="gender" type="java.lang.String">
            <column name="GENDER" not-null="false" precision="0" scale="4" sql-type="varchar"/>
        </property>
        <property name="firstname" type="java.lang.String">
            <column name="FIRSTNAME" not-null="false" precision="0" scale="100" sql-type="varchar"/>
        </property>
        <property name="lastname" type="java.lang.String">
            <column name="LASTNAME" not-null="false" precision="0" scale="100" sql-type="varchar"/>
        </property>

        
</class>

Here is also the class of my user entity.

@XmlRootElement(name = "user")
public class User implements Serializable {

    @XmlElement(name = "id")
    private UserId id;

    @XmlElement(name = "gender")
    private String gender;

    @XmlElement(name = "firstName")
    private String firstname;

    @XmlElement(name = "lastName")
    private String lastname;
}

The query that I will write with the hql language is this one :

Query query = session.createQuery("SELECT * FROM USER us oh WHERE   DATE_FORMAT(us.HI_DATE,'%dd-%mm-%YYYY') <= :from_date <= DATE_FORMAT(us.DEP_DATE'%dd-%mm-%YYYY') ");
query.setParameter( "from_date ", from_date );

but it doesn't work, any idea ?

1 Answer 1

2

you can try this.

Do you need to format the dates?

Query query = session.createQuery("FROM USER us WHERE :from_date between us.hiDate and us.depDate");
query.setParameter( "from_date ", from_date );
Sign up to request clarification or add additional context in comments.

2 Comments

i am interested for a way with JpaRepository with method name query. Just for curiosity :).
public interface CurrencyRepository extends JpaRepository<Currency, Long> { Currency findByHireDateBetween(Date start, Date end); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.