Hibernate Criteria 在两次连续调用中为不同的限制返回相同的值

Hibernate Criteria is returning same value for different Restrictions in two consecutive calls

令人惊讶的是,我在两次连续的标准调用中获得了相同的不同限制值。

我正在使用 Hibernate 4.2.20 final。请参考我的maven依赖。

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.2.20.Final</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.2.20.Final</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.1.3.Final</version>
    </dependency>

    <dependency>
       <groupId>org.hibernate.javax.persistence</groupId>
       <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.0.Final</version>
     </dependency>

我的 DAO 代码:

  public List<StockQuote> retrieve(final Date startDate, final Date endDate, final String companyName)
        throws StartDateAfterEndDateException
{

    Criteria criteria = getSessionFactory().getCurrentSession().createCriteria(StockQuote.class).createAlias("company", "c")
            .add(Restrictions.eq("c.name", companyName)).add(Restrictions.between("date", startDate, endDate))
            .addOrder(Order.asc("date"));

    return criteria.list();

}

如果上面的代码执行如下:

  DateTime endDate = new DateTime(2016, 4, 24, 0, 0, 0);
    List<StockQuote> quotes;
    quotes = stockService.retrieve(endDate.toDate(), endDate.toDate(), "S&P CNX Nifty", org.technical.analysis.common.Period.DAILY );
    quotes = stockService.retrieve(endDate.toDate(), endDate.toDate(), "Associated Cements", org.technical.analysis.common.Period.DAILY );

然后这两种情况我都得到相同的 StockQuotes 列表。请注意,我已经通过 不同的公司名称 .

调用了我的 DAO 方法检索
@Entity

@Table(姓名="stockdata") public class 股票报价 {

private Company company;
private Date date;
private double open;
private double high;
private double low;
private double close;
private long volume;

/**
 * Empty constructor
 */
public StockQuote() {
}

/**
 * Constructor with arguments
 * 
 * @param company The company
 * @param date Date of the quotation
 * @param open Open quote
 * @param high High quote
 * @param low Low quote
 * @param close Close quote
 * @param volume Volume of data
 */
public StockQuote(Company company, Date date, double open, double high, double low, double close, long volume) {
    this.company = company;
    this.date = date;
    this.open = open;
    this.high = high;
    this.low = low;
    this.close = close;
    this.volume = volume;
}


@ManyToOne
@JoinColumn(name = "companyId")
public Company getCompany() {
    return company;
}

public void setCompany(Company company) {
    this.company = company;
}

@Id
@Column(name = "date", nullable = false)
public Date getDate() {
    return this.date;
}

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

@Column(name = "open", nullable = false)
public double getOpen() {
    return this.open;
}

public void setOpen(double open) {
    this.open = open;
}

@Column(name = "high", nullable = false)
public double getHigh() {
    return this.high;
}

public void setHigh(double high) {
    this.high = high;
}


@Column(name = "low", nullable = false)
public double getLow() {
    return this.low;
}

public void setLow(double low) {
    this.low = low;
}   

@Column(name = "close", nullable = false)
public double getClose() {
    return this.close;
}

public void setClose(double close) {
    this.close = close;
}   

@Column(name = "volume", nullable = false)
public long getVolume() {
    return this.volume;
}

public void setVolume(long volume) {
    this.volume = volume;
}   

@Override
public String toString() {
    return "StockQuote [" + company + ", date=" + date + ", open=" + open
            + ", high=" + high + ", low=" + low + ", close=" + close + ", volume=" + volume + "]";
}

@Override
public int hashCode()
{
    final int prime = 31;
    int result = 1;
    long temp;
    temp = Double.doubleToLongBits(close);
    result = prime * result + (int) (temp ^ (temp >>> 32));
    result = prime * result + ((company == null) ? 0 : company.hashCode());
    result = prime * result + ((date == null) ? 0 : date.hashCode());
    temp = Double.doubleToLongBits(high);
    result = prime * result + (int) (temp ^ (temp >>> 32));
    temp = Double.doubleToLongBits(low);
    result = prime * result + (int) (temp ^ (temp >>> 32));
    temp = Double.doubleToLongBits(open);
    result = prime * result + (int) (temp ^ (temp >>> 32));
    result = prime * result + (int) (volume ^ (volume >>> 32));
    return result;
}

@Override
public boolean equals(Object obj)
{
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    StockQuote other = (StockQuote) obj;
    DateTime otherDateTime = new DateTime(other.date);
    DateTime dateTime = new DateTime(date);
    if (Double.doubleToLongBits(close) != Double.doubleToLongBits(other.close))
        return false;
    if (company == null)
    {
        if (other.company != null)
            return false;
    }
    else if (!company.equals(other.company))
        return false;
    if (date == null)
    {
        if (other.date != null)
            return false;
    }
    else if (!(dateTime.getDayOfMonth() == otherDateTime.getDayOfMonth() &&
               dateTime.getMonthOfYear() == otherDateTime.getMonthOfYear() &&
               dateTime.getYear() == otherDateTime.getYear()))
        return false;
    if (Double.doubleToLongBits(high) != Double.doubleToLongBits(other.high))
        return false;
    if (Double.doubleToLongBits(low) != Double.doubleToLongBits(other.low))
        return false;
    if (Double.doubleToLongBits(open) != Double.doubleToLongBits(other.open))
        return false;
    if (volume != other.volume)
        return false;
    return true;
}

}

请帮我解决这个问题。

经过一些研究,我发现根据第一条评论,StockQuote 中的 @Id 存在 问题。所以我 在 stockdata table 中添加了一个 id 列 并且 将其作为主键 还更新了 StockQuote class.现在可以正常使用了。