Searching DB registers by other field instead PK using JPA

I have a project using JPA without any framework. I want to search registers by others fields like 'username', 'password' instead the primary key. I need to use SQL with JPA to it?

    public User findUser(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(User.class, id);
        } finally {
            em.close();
        }
    }


I tried to create the same method in my JPA controller class using a string instead to the id for the search, obviously this give me a error where I see that the method search a numeric PK. But I can't to do something similar?
Was this page helpful?