- Entity and Value Types / A Simple Person Entity
- Creating Departments and Adding Person Entities / Named Queries
- Setting Up Your Own JPA Eclipse Project / Importing the Downloaded JPA Eclipse Project / Setting Up the MySQL Database
- Checking the Contents of the Database / Explaining the Relationship Between the Entities
- Setting Up the Persistence Unit / Running the Code / Conclusion
Checking the Contents of the Database
There are a number of ways to check the database contents after running the Java code. The approach I tend to employ is to use the MySQL command prompt, as illustrated in Listing 8.
Listing 8Displaying the Database Table (Person) Contents
mysql> select * from PERSON; mysql> select * from PERSON; +-----------+-----------+----------+---------+ | PERSON_ID | firstName | lastName | friends | +-----------+-----------+----------+---------+ | 1 | Einstein | Albert | David | | 2 | Hilbert | David | Albert | | 3 | Dactyll | Terry | Albert | +-----------+-----------+----------+---------+ 3 rows in set (0.00 sec)
Notice in Listing 8 that the Person table contains three rowsi.e., three people. What about the Department table? Listing 9 illustrates the Department table.
Listing 9Displaying the Database table (Department) Contents
mysql> select * from DEPARTMENT; +---------+----------+ | DEPT_ID | name | +---------+----------+ | 2 | ACCOUNTS | | 1 | SOFTWARE | +---------+----------+ 2 rows in set (0.00 sec)
In Listing 9, we see two Department rows.
Finally, let's look at the contents of the DEPARTMENT_PERSON table in Listing 10.
Listing 10Displaying the Database Table (Department) Contents
mysql> select * from DEPARTMENT_PERSON; +--------------------+-------------------+ | Department_DEPT_ID | persons_PERSON_ID | +--------------------+-------------------+ | 1 | 1 | | 1 | 2 | | 2 | 3 | +--------------------+-------------------+ 3 rows in set (0.00 sec)
Listing 10 is what's called a join table, which is used to model the relationship between the Person and Department instances. Try comparing the contents of Listing 10 with Listings 8 and 9 to see if you can figure out the relationships.
Explaining the Relationship Between the Entities
The relationship I wanted to model is a unidirectional one-to-many associationi.e., a department contains zero, one, or more persons. To create the association, I call code such as that illustrated in Listing 11.
Listing 11Entity and Collection Creation
Person person1 = createEntity.createPerson("Albert", "Einstein", "David"); Person person2 = createEntity.createPerson("David", "Hilbert", "Albert"); Person person3 = createEntity.createPerson("Terry", "Dactyll", "Albert"); createEntity.createDepartment("SOFTWARE"); createEntity.createDepartment("ACCOUNTS"); createEntity.addPersonToDept(person1, "SOFTWARE"); createEntity.addPersonToDept(person2, "SOFTWARE"); createEntity.addPersonToDept(person3, "ACCOUNTS");
Notice in Listing 11 that I create three instances of Person (person1, person2, and person3, respectively). I then create two instances of Department, and I then add each of the Person instances to one of the Department instances. That's all that's required to build the required relationships you saw in Listing 10.