- 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
Creating Departments and Adding Person Entities
In Listings 1 through 4, you've seen how to create discrete Department and Person instances. That's a good start, but how do we add Person instances to Departments? Again, it's pretty easy, as illustrated in Listing 5.
Listing 5Adding a Person Entity to a Department
public void addPersonToDept(Person person, String departmentName) { try { // Start EntityManagerFactory EntityManagerFactory emf = Persistence .createEntityManagerFactory("punit"); // First unit of work EntityManager entityManager = emf.createEntityManager(); EntityTransaction entityTransaction = entityManager .getTransaction(); entityTransaction.begin(); Query query = entityManager.createNamedQuery("findDepartmentByName"); query.setParameter("name", departmentName); Department department = (Department)query.getSingleResult(); Query query1 = entityManager.createNamedQuery("findPersonByLastName"); query1.setParameter("lastName", person.getLastName()); Person person1 = (Person)query1.getSingleResult(); department.getPersons().add(person1); entityManager.merge(department); entityTransaction.commit(); entityManager.close(); emf.close(); } catch (Exception e) { e.printStackTrace(); } }
As with the earlier code examples, the operation of adding a Person to a Department is pretty straightforwardjust a few extra items are required. Let's work backwards from where the Person is added to the Department collection:
department.getPersons().add(person1);
In this line, you can see that I access the Collection inside the Department instance and simply add the Person instance. That's it! After this, the required Person instance resides inside the Department instance collection.
However, if you look at Listing 5 just above the call to add(), you'll see the following lines:
Query query = entityManager.createNamedQuery("findDepartmentByName"); query.setParameter("name", departmentName); Department department = (Department)query.getSingleResult(); Query query1 = entityManager.createNamedQuery("findPersonByLastName"); query1.setParameter("lastName", person.getLastName()); Person person1 = (Person)query1.getSingleResult();
You can see in this code that we search for the Department and Person instances. Once these entities are retrieved, we can then add the Person instance to the Department. The modified Department instance is then merged back (i.e., updated) into the database.
What's all this code about? Well, before we can modify a Department instance, we have to retrieve it from the database, and we do this by searching for it with a named query.
Named Queries
A named query is a mechanism for searching for a given item of data. An example named query is illustrated in Listing 6, which is an excerpt from the Department class.
Listing 6Creating the Database
@Entity @Table(name = "DEPARTMENT") @NamedQuery( name="findDepartmentByName", query="select OBJECT(d) from Department d where d.name = :name") public class Department implements Serializable
Notice in Listing 6 the @NamedQuery annotation, which is followed by a name and a query (no pun intended). The name describes the named query that can be called from elsewhere in the persistence code base, as we saw in Listing 5:
Query query = entityManager.createNamedQuery("findDepartmentByName"); query.setParameter("name", departmentName); Department department = (Department)query.getSingleResult();
The attractive thing about named queries is that the query is included as part of the entity class itself. This is a nice example of encapsulation, where the logic to manage the entity remains inside the associated entity class itself. All this sounds like there's an alternative to the use of named queries, doesn't it? Well, there is an alternative: You can use JPQL queries inside the client code. A JPQL query looks similar to a named query, but I think it represents a kind of leakage of concerns. For this reason, I generally prefer to use named queries instead of JPQL.