Reputation: 20096
I have re-written this question to make it clear.
I have an entity like this:
@Entity
@NamedQueries({
@NamedQuery(
name = "myObj.findCurrentUsingFunction",
query = "from MyObj where insertDate = current_date() "),
@NamedQuery(
name = "myObj.findCurrentUsingParameter",
query = "from MyObj where insertDate = :currentDate")
})
public class MyObj {
@Id @GeneratedValue @Column
private Long id;
@Column @Temporal(TemporalType.DATE)
private Date insertDate;
/* getter, setter */
}
I have a failing test:
@Test
public void findCurrentUsingFunction() throws Exception {
final MyObj myObj = new MyObj();
myObj.setInsertDate(new Date());
final Session session = dao.getSessionFactory().getCurrentSession();
session.saveOrUpdate(myObj);
final Query namedQuery =
session.getNamedQuery("myObj.findCurrentUsingFunction");
final List results = namedQuery.list();
Assert.assertEquals("size",1L, (long) results.size());
}
Hibernate: select hibernate_sequence.nextval from dual Hibernate: insert into MyObj (insertDate, id) values (?, ?) Hibernate: select myobj0_.id as id0_, myobj0_.insertDate as insertDate0_ from MyObj myobj0_ where myobj0_.insertDate=current_date
java.lang.AssertionError: size expected:<1> but was:<0>
and a passing test
@Test
public void findCurrentUsingParameter() throws Exception {
final MyObj myObj = new MyObj();
myObj.setInsertDate(new Date());
final Session currentSession = dao.getSessionFactory().getCurrentSession();
currentSession.saveOrUpdate(myObj);
final Query namedQuery =
currentSession.getNamedQuery("myObj.findCurrentUsingParameter");
namedQuery.setDate("currentDate", new Date());
final List results = namedQuery.list();
Assert.assertEquals("size",1L, (long) results.size());
}
the dialect is:
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
Upvotes: 0
Views: 8610
Reputation: 47290
You are using a sql function inside an HSQL statement, so it doesn't work. Either remove the brackets to use the hibernate current_date function, like this:
@NamedQuery(
name = "entity.findCurrent",
query = "from EntityName where insertDate = current_date")
Or, I would do it like this (in fact I would also use joda-time to create my datetime variable currentDate)
@NamedQuery(
name = "entity.findCurrent",
query = "from EntityName where insertDate = :currentDate")
Then pass in date when you call the named query
session.getNamedQuery(findCurrent).setDate("currentDate", currentDate);
or you could use a critiera.
Upvotes: 5