令人惊讶的是,我在两个连续的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 );
然后这两种情况我都得到相同的StockQuotes列表。请注意,我已经用不同的公司名称调用了我的DAO方法。
@Entity
@Table(name =“ stockdata”)
公共类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类。现在一切正常。