0

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

3
  • 1
    Your hyperlink is broken. What doesn't work? Exception, stack trace? Not the result you're expecting? What's the mapping of insertDate? What's its column type in database? What's its value in database? Commented Aug 24, 2011 at 8:13
  • possible duplicate of How do I use the current date in an HQL query with an Oracle database? (take a look at the accepted answer) Commented Aug 24, 2011 at 8:16
  • I will re-write the question to make it clear. Commented Aug 24, 2011 at 9:01

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Well, of course I could do that, and it is what I am doing until I get a solution to this. But the question is about why the current_date() function doesn't work for me.
@NimChimpsky: current_date() is also a HQL function. See docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/…
@JB Nizet, without the brackets it is, with the brackets it isn't.
Then there's a bug in the Hibernate reference documentation.
@JB Nizet more details now provided in the re-written question. I tried with and without the brackets.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.