Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Java
    public class DBM {
    
    private static EntityManagerFactory emf = 
          Persistence.createEntityManagerFactory("CompanyWithJPAPU");
    private static EmployeeJpaController employeeJpaController = new
          EmployeeJpaController(getEmf());
    private static EntityManager em = getEmf().createEntityManager();

    /**
     * @return the employeeJpaController
     */
    public static EmployeeJpaController getEmployeeJpaController() {
        return employeeJpaController;
    }

    /**
     * @return the em
     */
    public static EntityManager getEm() {
        return em;
    }

    /**
     * @return the emf
     */
    public static EntityManagerFactory getEmf() {
        return emf;
    }

    /**
     * @param aEmf the emf to set
     */
    public static void setEmf(EntityManagerFactory aEmf) {
        emf = aEmf;
    }
    
}


I have done the data adding to the DB table Employee as the following method.

Java
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.
Posted

1 solution

 
Share this answer
 
Comments
Chiranthaka Sampath 8-Jul-13 6:10am    
I had tried this as at the below. But it doesn't work properly.

-----------------------------------------------------------------------------------------
Employee emp = new Employee();

String id = new String();

id = txtStudNo.getText();

DBM.getEmployeeJpaController().findEmployee(id);

txtStudName.setText(emp.getEmpName());
txtAddress.setText(emp.getEmpAddress());

-----------------------------------------------------------

Please could you explain what the problem is?
Shubhashish_Mandal 8-Jul-13 6:19am    
check the third example in the given link
Chiranthaka Sampath 8-Jul-13 7:11am    
I am wondering that I can find there that using Java Beans for the development. I cannot understand that how to amend Java Beans with
JPA.
Shubhashish_Mandal 8-Jul-13 7:18am    
All the steps are mentioned in the tutorial. From creating ntityManagerFactory
to EntityManager

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900