I have created a Java Swing application using MySQL and JPA. I was able to add the data from a
JFrame form.
The following DBM class will use to manage the DB connectivity using JPA.
The entity for the Database Table is as Employee.
public class DBM {
private static EntityManagerFactory emf =
Persistence.createEntityManagerFactory("CompanyWithJPAPU");
private static EmployeeJpaController employeeJpaController = new
EmployeeJpaController(getEmf());
private static EntityManager em = getEmf().createEntityManager();
public static EmployeeJpaController getEmployeeJpaController() {
return employeeJpaController;
}
public static EntityManager getEm() {
return em;
}
public static EntityManagerFactory getEmf() {
return emf;
}
public static void setEmf(EntityManagerFactory aEmf) {
emf = aEmf;
}
}
I have done the data adding to the DB table Employee as the following method.
private void AddData() throws PreexistingEntityException, Exception{
Employee emp = new Employee();
emp.setEmpNo(txtStudNo.getText().trim());
emp.setEmpName(txtStudName.getText().trim());
emp.setEmpAddress(txtAddress.getText().trim());
DBM.getEmployeeJpaController().create(emp);
}
1. My Entity class is the Employee.java and it resides inside in a package called 'Entities'.
2. The Persistence unit is named as 'CompanyWithJPAPU'.
3. My JPA Controller is named as 'EmployeeJpaController'.
4. My DBM class is used to manage the Database connectivity and the code has placed at the
above.
Now I want to find, update and delete records from the database table using JPA. I cannot
figure out how to do that. All I want is to find an specific record according to a unique
value i.e. EmpID. If you someone please tray to help me.
Chiranthaka.