Now we can create some users and contacts. For this we change the App.java file we created during setup. First we must import the following classes:
import java.security.PrivilegedAction; import java.util.Arrays; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import net.sf.jpasecurity.contacts.model.Contact; import net.sf.jpasecurity.contacts.model.User; import net.sf.jpasecurity.security.authentication.StaticAuthenticationProvider;
To create the users and contacts we must create an EntityManagerFactory, an EntityManager, start a Transaction and persist the users. As recommended by the JPA Specification, we use one EntityManagerFactory per application. So we create it directly within the main:
public static void main(String[] args) {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("contacts");
}
We use a separate method to create the users:
public static void createUsers(EntityManagerFactory entityManagerFactory) {
EntityManager entityManager;
entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(new User("John"));
entityManager.persist(new User("Mary"));
entityManager.getTransaction().commit();
entityManager.close();
}
Ok, let's retrieve the users from the database to see whether they were really persisted:
public static void displayUserCount(EntityManagerFactory entityManagerFactory) {
EntityManager entityManager;
entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
List<User> users = entityManager.createQuery("SELECT user FROM User user").getResultList();
System.out.println("users.size = " + users.size());
entityManager.getTransaction().commit();
entityManager.close();
}
Let's change the main to call our methods and see the output:
public static void main(String[] args) {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("contacts");
createUsers(entityManagerFactory);
displayUserCount(entityManagerFactory);
}
Again on the command-line issue the following command from within the root-directory of our application:
mvn package
Now again change into the target folder and execute our application to see the output:
java -jar jpasecurity-simple-sample-0.3.0.jar
As we can see from the output, there are really two users in the database. Now we can create contacts for these users:
public static void createContacts(EntityManagerFactory entityManagerFactory) {
EntityManager entityManager;
entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
User john = (User)entityManager.createQuery("SELECT user FROM User user WHERE user.name = 'John'").getSingleResult();
User mary = (User)entityManager.createQuery("SELECT user FROM User user WHERE user.name = 'Mary'").getSingleResult();
entityManager.persist(new Contact(john, "peter@jpasecurity.sf.net"));
entityManager.persist(new Contact(john, "0 12 34 - 56 789"));
entityManager.persist(new Contact(mary, "paul@jpasecurity.sf.net"));
entityManager.persist(new Contact(mary, "12 34 56 78 90"));
entityManager.getTransaction().commit();
entityManager.close();
}
Let's see if all four contacts are there:
public static void displayContactCount(EntityManagerFactory entityManagerFactory) {
EntityManager entityManager;
entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
List<Contact> contacts = entityManager.createQuery("SELECT contact FROM Contact contact").getResultList();
System.out.println("contacts.size = " + contacts.size());
entityManager.getTransaction().commit();
entityManager.close();
}
Again we add the method to our main and see the output:
public static void main(String[] args) {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("contacts");
createUsers(entityManagerFactory);
displayUserCount(entityManagerFactory);
createContacts(entityManagerFactory);
displayContactCount(entityManagerFactory);
}
As we can see from the output: All contacts are persisted. Now it's time for some security.