Top
Interstage Studio User's Guide
FUJITSU Software

4.3.4 Operating Database with JPA

With JPA, an Entity is operated by the Entity manager, and this performs database operations. Thus, an Entity manager is required. Operations equivalent to database searches, insertions, updates, and deletions can be performed by using the fetched Entity manager to operate the Entity.

Point

Strictly speaking, the Entity manager performs operations in relation to the persistence context (the accumulation of Entities constructed within memory), and synchronization with the database is performed during breaks in transactions.

Fetching the Entity Manager

Use the following type of @PersistenceContext annotation to fetch the Entity manager.

Example of using the PersistenceContext annotation

@PersistenceContext
private EntityManager em;

Persistence Context Operations Performed by the Entity Manager

An example of persistence context operations performed by the Entity manager is shown below.

Searching using the Entity Primary Key

In this example, the ID (primary key) from the EMPLOYEE table is specified to search for an employee. The Entity class and the primary key value are specified in the Entity manager find method to perform the search.

Example of a Search using the Entity Primary Key

public Employee getEmployeeByPrimaryKey (int id) {
    return em.find(Employee.class, id);
}

Searching using the Entity Java Persistence Query Language

In this example, a search is performed for the employee that matches a name from the EMPLOYEE table. A query is created by the Entity manager createQuery method and the parameters are set, then the query is executed and the search results are fetched.

Example of a Search using the Entity Java Persistence Query Language

public Collection<Employee> getEmployeeByName (String name) {
    Query query = em.createQuery("SELECT employee FROM Employee employee WHERE employee.name = :name");
    query.setParameter("name" ,name);
    return query.getResultList();
}

Inserting an Entity

In this example, a new employee is added to the EMPLOYEE table. An Entity class (Employee) instance is created, and the Entity manager persist method adds it to the persistence context.

Example of inserting an Entity

public void insertEmployee (int id, String name, String address, String tel) {
    Employee newEmployee = new Employee(id, name, address, tel);
    em.persist(newEmployee);
}

Updating an Entity

In this example, the ID (primary key) from the EMPLOYEE table is specified to search for an employee, and the address and phone number are changed. The Entity manager find method performs the search, then the Entity class values are changed.

Example of updating an Entity

public void updateEmployee (int id, String address, String tel) {
    Employee employee = em.find(Employee.class, id);
    employee.setAddress(address);
    employee.setTel(tel);
}

Point

In order to make a change within the persistence context, simply change the Entity instance and the change will be detected automatically.
If an instance is outside the persistence context (for example, if the return value instance returned by the above getEmployeeByPrimaryKey method is to be updated), the Entity manager merge method must be invoked to post the change to the persistence context.

Deleting an Entity

In this example, the ID (primary key) from the EMPLOYEE table is specified to search for an employee, and the information of that employee is deleted. The Entity class instance searched for using the Entity manager find method is specified in an argument and the remove method is invoked to delete it from the persistence context.

Example of Deletion using the Entity Primary Key

public void deleteEmployee (int id) {
    Employee employee = em.find(Employee.class, id);
    em.remove(employee);
}