0

I have the following hibernate query -

    events = (List<Event>)session.createQuery(
            "Event where eventName like " 
            + "\"" + event.getEventName() 
            + "\"" +
            " and startingDate >=   "
            + "\"" + 
            event.getStartingDate() 
            + "\" " +
            " and endingDate <=  " 
            + "\"" + 
            event.getEndingDate()
            + "\" "
            ).list();

But it returns the error -

org.hibernate.QueryException: unexpected char: '"' [Event where eventName like "test" and startingDate >= "2011-01-19" and endingDate <= "2011-01-31" ] at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:227) at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:134) at org.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:101) at org.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:80)

Seems to be saying it does'nt like a " somewhere ?

It worked fine until I added the date fields to the where clause.

Thanks

0

1 Answer 1

2

First: you should use the single quote char to delimit strings. Like this: 'string'. Second, don't build queries by concatenating strings. Use parameter markers, like this:

Event where eventName like ? and startingDate >= ? and endingDate <= ?
Event where eventName like :name and startingDate >= :startDate and endingDate <= :endDate

Then, call the setParameter() method from the Query object to set the parameters. You can use positional parameters (like, "? in position 0 is 'name'") or named parameters (":name is 'name'"). See the section "Bind parameters" from the Hibernate documentation: http://docs.jboss.org/hibernate/core/3.5/reference/en/html_single/#objectstate-querying-executing

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.