Home > Articles > Data > MySQL

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Testing the Bidirectional Department-Person Relationship

Testing the Bidirectional Department-Person Relationship

Listing 4 illustrates the same code you saw in Part 1, which creates three Person instances, creates two Department instances, and finally adds the three Person instances to the Department instances.

Listing 4—Creating Persons and Department Entities

            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");

Where we implement the two-way relationship is in a small modification to the method addPersonToDept(), as illustrated in Listing 5. This is the third and last change required to support the new bidirectional relationship:

Listing 5—Adding a Person Entity to a Department

            // 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();
            person1.setDepartment(department);
            department.getPersons().add(person1);
            entityManager.merge(department);
            entityTransaction.commit();
            entityManager.close();
            emf.close();

Just one line of Java code is required to create the relationship between the Person instance and that of the Department instance:

            person1.setDepartment(department);

In the above line, I set the department in which the person resides. To complete the picture, what does this look like in the database itself? Listing 6 illustrates the database after adding a person into a department.

Listing 6—Bidirectional Relationship

mysql> select * from PERSON;
+-----------+-----------+----------+---------+--------------------+
| PERSON_ID | firstName | lastName | friends | DEPARTMENT_DEPT_ID |
+-----------+-----------+----------+---------+--------------------+
|         1 | Einstein  | Albert   | David   |                  1 |
|         2 | Hilbert   | David    | Albert  |                  1 |
|         3 | Dactyll   | Terry    | Albert  |                  2 |
+-----------+-----------+----------+---------+--------------------+
3 rows in set (0.00 sec)

Notice in Listing 6 the new column called DEPARTMENT_DEPT_ID. This column is a foreign key on the DEPT_ID column from the DEPARTMENT table and serves to connect the associated PERSON_ID to the containing department.

That's it. Our unidirectional relationship is now bidirectional! Just a few minor code changes were required to the legacy code from Part 1, followed by some testing.

  • + Share This
  • 🔖 Save To Your Account