Reputation: 2198
I've got the object User with the following attributes: Id(long), Name(string), e-mail(string), activated(bool). I added all the correct annotations.
Now is my question, how do I use the EntityManager to fetch a specific object if only the name is known?
I know that I can use the find method to find an object with a specific Id. But how do I use it for fetching an object with the name property set to "Use R Name" for example.
Thank you!
Yvan
Upvotes: 0
Views: 175
Reputation: 691765
As in any JPA application, you use JPA queries. See http://download.oracle.com/javaee/5/tutorial/doc/bnbqw.html#bnbrg for an introduction on queries and JPQL.
In your case, the code would look like this :
public List<User> findUsersByName(String name) {
List<User> users = entityManager.createQuery(
"SELECT u FROM User u WHERE u.name = :userName")
.setParameter("userName", name)
.getResultList();
return users;
}
Note that everything supported by JPA is not supported in Google App Engine : see http://code.google.com/intl/fr/appengine/docs/java/datastore/jpa/overview.html#Unsupported_Features_of_JPA for details.
Upvotes: 1