I am currently experimenting with JPA in a desktop application using BeansBinding to simplify GUI development. So far, the results are not bad.
As an example of an application, I have a small database with only one table. I have successfully created an object, PU. Then I removed the JTable in the main JFrame and bound its columns to the JPA request. It works like a charm. Thus, changes made to objects are reflected in the table, and vice versa.
Next, I wanted to make the table editable so that the changes were saved in the database. The easiest way I came across was to start a request and make it immediately. So, assuming I have JButton, do the following on actionPerformed :
private void saveClicked(java.awt.event.ActionEvent evt) { this.myEntityManager.getTransaction().begin(); this.myEntityManager.getTransaction().commit(); }
This works fine, but to me it looks weirdly wrong. I also tried to do this on windowClosing . Successfully.
But why did it work? I mean, there is no code between the begin and commit transaction. And more importantly, is it OK to do this?
source share