Luasjian
Luasjian

Reputation: 96

Why parameter substitution in HQL-Query failed but native type succeeds?

did one of you ever had the problem that parameter substitution in a HQL-Query didn't worked?

I'm using hibernate for some, I think, simple queries getting entities from my MySQL Database.

Here are the Queries:

entityManager.
            createQuery("SELECT c FROM PersistentEvent c WHERE c.sessionId LIKE :sessionId").
            setParameter("sessionId", session.getId()).getResultList();

Doesn't get a result! Under debugging I saw session.getId() returns the right value.

entityManager.
                createQuery("SELECT c FROM PersistentEvent c WHERE c.sessionId LIKE :sessionId").
                setParameter("sessionId", "TestSessionId - 1").getResultList();

Works for my testdata where such an entry exists.

Any suggestions? Thanks

Upvotes: 0

Views: 930

Answers (1)

Jeremy
Jeremy

Reputation: 22435

After you posted the query Hibernate was generating, I noticed that session.getId() was inserting TestSession - 1 instead of TestSessionId - 1, which was in your hard coded example.

Upvotes: 3

Related Questions