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

Paul McKenzie
  • 19,646
  • 25
  • 76
  • 120
  • 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? – JB Nizet Aug 24 '11 at 08:13
  • possible duplicate of [How do I use the current date in an HQL query with an Oracle database?](http://stackoverflow.com/questions/513317/how-do-i-use-the-current-date-in-an-hql-query-with-an-oracle-database) (take a look at [the accepted answer](http://stackoverflow.com/questions/513317/how-do-i-use-the-current-date-in-an-hql-query-with-an-oracle-database/513367#513367)) – Sean Patrick Floyd Aug 24 '11 at 08:16
  • I will re-write the question to make it clear. – Paul McKenzie Aug 24 '11 at 09:01

1 Answers1

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.

NimChimpsky
  • 46,453
  • 60
  • 198
  • 311