Hibernate seems to incorrectly handle date ranges using the Criterion API in Oracle. The SQL Query itself seems correct (copied it from Hibernate and executed it manually). So,
given
Clazz<Bar> clazz;
Date start, end;
this fails
List<Bar> bars = sessionFactory.getCurrentSession()
.createCriteria(clazz)
.add(Restrictions.between("timestamp", start, end))
.list();
and this
List<Bar> bars = sessionFactory.getCurrentSession()
.createCriteria(clazz)
.add(Restrictions.ge("timestamp", start))
.add(Restrictions.le("timestamp", end))
.list();
but this works
List<Bar> bars = sessionFactory.getCurrentSession()
.createQuery("from Bar b where b.timestamp > ? and b.timestamp < ?")
.setDate(0, start)
.setDate(1, end)
.list();
The fail observation is:
The number of
Bar
results returned are the same (and correct)but in the criterion cases a
Bar
with aList<Foo>
returns roughly 10x moreFoo
objects than the corresponding SQL query does. All the extraFoo
objects are identical copies.
EDIT
@Entity
public class Bar {
@Id
private String id;
@Temporal(TemporalType.TIMESTAMP)
private Date timestamp;
@ManyToMany(cascade = CascadeType.ALL, fetch=FetchType.EAGER)
@JoinTable(
name = "bar_foo",
joinColumns = { @JoinColumn(name = "barid") },
inverseJoinColumns = { @JoinColumn(name = "fooid") }
)
private List<Foo> params;
}
@Entity
public class Foo {
@Id private String id;
}