Dashboard > Struts University > Home > JPA Training Course > JPA Agenda > Login-JPA-Notes
Added by Ted Husted, last edited by Ted Husted on Dec 07, 2007  (view change)

We persist data so that we can retrieve it again. How can we retrieve the data we need, and only the data that we want?

Presentation - Mapping workflows

Retrieving objects can be the most interesting part of the CRUD quartet. In this session, we put to task the Java Persistence API Query Language (JPL).

  • How do we write simple queries?
  • Can we use functions?
  • Can we use parameters?

Workshop - Login

In the lab, we authenticate a MailReader user by retrieving a User's credentials (username and password).

Story: Subscribers can end an application session, retaining any changes. Subscribers can present their registration credentials to use an account again.

  • Retrieve the credentials for a User entity
  • Compare the credentials with input parameters


  • Fundamentals JPA Query Language
  • EntityService and EnityManagerHelper
  • User entity object


  • Review the Login use cases
  • Create a LoginTest class
    • Put the test in a new package, named action.user
    • Include omnibus pass and fail tests and individual fail testes for username and password.
  • Extend UserService to include a findByName (or Username) method. Use JQL to select the User object using a username.


  • Created a new unit test
  • Coded a JQL statement
  • Retrieved and verified input parameters

Hint Code

To test retrievals, we need a known dataset to be available in the database.

public class LoginTest extends TestCase {
    UserService helper;
    User user;
    public void setUp() throws Exception {
    helper = new UserServiceImpl();
    user = new UserImpl();
    user.setFullName("John Q. User");
    public void tearDown() throws Exception {
    private boolean authenticate(String username, String password) {
    User foundValue = (User) helper.findByName(username);
    if ((foundValue != null) && !foundValue.getPassword().equals(password)) {
    foundValue = null;
    return (foundValue != null);

To make LoginTest.setUp to work, add a flush method to UserService.

public void flush() throws PersistenceException {
    EntityManager manager = EntityManagerHelper.getEntityManager();
private static final String FIND_BY_NAME_QUERY =
    "SELECT u FROM USER u WHERE u.username = :username";

Extra Credit

  • Use DatabaseExplorer to confirm that the "user" entity is not being retained.
  • Add findByName methods to SubscribeService and ProtocolService.
  • Add tests to exercise the new methods. (Put the tests in SubscriptionServiceTest and ProtocolServiceTest.)
  • Repeat for a findAll method.

Site running on a free Atlassian Confluence Open Source Project License granted to OSS. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.5 Build:#811 Jul 25, 2007) - Bug/feature request - Contact Administrators