kai
kai

Reputation: 6887

variable value in WHERE clause and wildcard

I am searching since two hours, but can't find a solution. I want to make an easy query which should check if the String m_sName contains a String variable. I have tried many things but can't find the right syntax. Using a variable works and using wildcards works too, but both together don't work.

TypedQuery<Kunde> query; 
query = em.createQuery("SELECT p FROM Kunde p" + 
    " WHERE p.m_sName LIKE :name", Kunde.class);
m_lKunde = query.setParameter("name", m_sSearch).getResultList();

This query actually works, but I don't now how to use wildcards now. I tried *,_,% characters but nothing worked.

Upvotes: 2

Views: 554

Answers (2)

Kevin Bowersox
Kevin Bowersox

Reputation: 94429

Append/Prefix the wildcard symbol to the token your passing in:

m_lKunde = query.setParameter("name", "%" + m_sSearch + "%").getResultList();

Upvotes: 3

mucio
mucio

Reputation: 7119

maybe this can help

"SELECT p FROM Kunde p WHERE p.m_sName LIKE '%'|| :name || '%' ", Kunde.class);

Upvotes: 0

Related Questions