1

CREATE TABLE ratecodes ( roomId int(11) NOT NULL DEFAULT '0', date date NOT NULL DEFAULT '0000-00-00', ..., PRIMARY KEY (roomId,date)) hbm.xml

<class catalog="hermes" name="com.hermes.data.RateCode" table="ratecodes">
<composite-id class="com.hermes.data.RateCodeId" name="id">
      <key-property name="roomId" type="int">
        <column name="roomId"/>
      </key-property>
      <key-property name="date" type="date">
        <column length="10" name="date"/>
      </key-property>
    </composite-id>
    <version name="version" type="java.lang.Long">
      <column name="version" precision="10" scale="0"/>
    </version>

i want to query HibernateUtil.getCurrentSession().createQuery("from RateCode where (RatecodeId (between|<) RateCodeId)"

SQL: "from RateCode where roomId = 3000 and date > :fromDate and date < :toDate"

RatecodeId.java (composite)

public class RateCodeId implements java.io.Serializable {

    private int roomId;
    private Date date;

    public RateCodeId() {
    }

    public RateCodeId(int roomId, Date date) {
        this.roomId = roomId;
        this.date = date;
    }

    public int getRoomId() {
        return this.roomId;
    }

    public void setRoomId(int roomId) {
        this.roomId = roomId;
    }

    public Date getDate() {
        return this.date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public boolean equals(Object other) {
        if ((this == other)) {
            return true;
        }
        if ((other == null)) {
            return false;
        }
        if (!(other instanceof RateCodeId)) {
            return false;
        }
        RateCodeId castOther = (RateCodeId) other;

        return (this.getRoomId() == castOther.getRoomId())
                && ((this.getDate() == castOther.getDate()) || (this.getDate() != null && castOther.getDate() != null && this.getDate().equals(castOther.getDate())));
    }

    public int hashCode() {
        int result = 17;

        result = 37 * result + this.getRoomId();
        result = 37 * result + (getDate() == null ? 0 : this.getDate().hashCode());
        return result;
    }
}

how to set FromDate and Todate in Object and do condition of RateCodeID

1 Answer 1

4
String hql = "from RateCode rc where rc.id.roomId = 3000"
             + " and rc.id.date > :fromDate"
             + " and rc.id.date < :toDate";
Query q = session.createQuery(hql);
q.setParameter("fromDate", theFromDate);
q.setParameter("toDate", theToDate);
Sign up to request clarification or add additional context in comments.

Comments

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.