gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:09:52 25 4
gpt4 key购买 nike

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

我正在使用 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 );

然后,在这两种情况下,我都会得到相同的股票报价列表。请注意,我已通过不同的公司名称调用我的 DAO 方法检索。

@Entity

@Table(名称 = "stockdata")公共(public)类 StockQuote {

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表中添加了一个id列,并将其作为主键还更新了StockQuote类。现在一切正常。

关于java - Hibernate Criteria 在两次连续调用中为不同的限制返回相同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32497524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com