- Entity and Value Types
- A Simple Entity with a Component Class
- Running the Code
- An Instance of the User Class and Transaction Handling
- A Quick Look at the Schema
- Running SQL Commands
- Dropping Tables
- Code Installation
- Running the Code
- Conclusion
An Instance of the User Class and Transaction Handling
Listing 2 illustrates a complete example of creating an instance of the User class and assigning it an instance of the Address class.
Listing 2An Example Program Manipulating the User Class
// Start EntityManagerFactory EntityManagerFactory emf = Persistence.createEntityManagerFactory("helloworld"); // First unit of work EntityManager em = emf.createEntityManager(); EntityTransaction tx = em.getTransaction(); tx.begin(); User user = new User("Donald Dinosaur"); user.setAddress(new Address("Wide Street", "Chicago")); em.persist(user); tx.commit(); em.close(); // Second unit of work EntityManager newEm = emf.createEntityManager(); EntityTransaction newTx = newEm.getTransaction(); newTx.begin(); List users = newEm.createQuery("select m from User m order by m.userName asc").getResultList(); System.out.println(users.size() + " user(s) found:" ); for (Object u : users) { User loadedUser = (User) u; System.out.println(loadedUser.getUserName()); System.out.println(loadedUser.getAddress()); } newTx.commit(); newEm.close(); // Shutting down the application emf.close();
Notice in Listing 2, the following lines:
EntityManager em = emf.createEntityManager(); EntityTransaction tx = em.getTransaction(); tx.begin();
These lines form the beginning of a transaction. A transaction is a set of steps or actions that either all succeed or all fail. A classic example of a transaction is a bank account being debited; all the steps involved must either succeed or be rolled back. With JPA, you get this level of transactional sophistication almost for free.
Again, Listing 2 is familiar territory. It’s the code from my earlier article with a few minor additions, the most significant of which is the following lines in Listing 3:
Listing 3Instantiating a User Object
User user = new User("Donald Dinosaur"); user.setAddress(new Address("Wide Street", "Chicago"));
Here I create an instance of the User class. I then add a reference to an instance of the Address class. Figure 1 illustrates the database after running the code in Listing 2.
Figure 1 The database after running the code
As you can see in Figure 1, we’ve got one row in the USERS table. Notice in Figure 1 the blank column NEXT_USER_ID. This column represents a logical link to the next user in a group. Your application might, for example, use this column to group users by category. So, how do we link users together? It’s a straightforward taskjust change the code in Listing 3 to that in Listing 4.
Listing 4Instantiating the Composite Entity
User user = new User("Terry Dactyll"); user.setAddress(new Address("Broad Street", "Boston")); User user1 = new User("Donald Dinosaur"); user1.setAddress(new Address("Wide Street", "Chicago")); user.setNextUser(user1); em.persist(user);
After running the code in Listing 4, the database state is as illustrated in Figure 2.
Figure 2 Linked composite entities
Notice in Figure 2 that the second row in the USERS table now has a reference to the first row. To understand what this means, we need to look at the schema. So, let’s now have a look at the database schema file that gets created when we run the schemaexport target. The file is called helloworld-jpa-ddl.sql and it’s located in the top-level folder of the code directory.