I need to search all the usernames and for the application to print out all the usernames containting the search string. I think I have done this but when I execute searchByString()
, I just get no results.
AbstractFacade
/* trying out a search function */
public List<T> searchByString(String string) {
System.out.println("in SearchByString");
return getEntityManager().createNamedQuery("Userdetails.findByUsername").setParameter("username", "%" + string + "%").getResultList();
}
Userdetails.java this is where the query is done
@Entity
@Table(name = "USERDETAILS")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Userdetails.findAll", query = "SELECT u FROM Userdetails u"),
@NamedQuery(name = "Userdetails.findById", query = "SELECT u FROM Userdetails u WHERE u.id = :id"),
@NamedQuery(name = "Userdetails.findByUsername", query = "SELECT u FROM Userdetails u WHERE u.username = :username")})
I can not get a filled list. I am wondering if the query is running right and retriving anything, so I want to be able to see what it retrieves when it is run, so I can look at why nothing gets displayed to the user.
Your query parameter looks like you want a like clause, but your named query uses =. Change
SELECT u FROM Userdetails u WHERE u.username = :username
to
SELECT u FROM Userdetails u WHERE u.username like :username
and it should work.