JTable refresh with Hibernate
I've been searching and trying for hours now, still can't find a solution that should be obvious. This is my first post, so please be kind.
Using NetBeans 6.9.1, I've created a JTable and drag&dropped a Hibernate MySQL database table onto it. This works well, and fills the JTable with the column headers and rows. I can successfully add rows to the database, but the table is not updated. Looking at the code created by the IDE for a generic desktop database app, I came up with the following code, which seems like it should work, but does not.
Code :
private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) {
tournamentmarshalPUEntityManager.getTransaction().begin();
java.util.Collection data = tournamentQuery.getResultList();
for (Object entity : data) {
tournamentmarshalPUEntityManager.refresh(entity);
}
tournamentList.clear();
tournamentList.addAll(data);
tournamentmarshalPUEntityManager.getTransaction().commit();
System.out.println("refreshing");
}
Do I need to be thinking about firePropertyChange() to solve this?
I've seen this question asked online many different ways, and nobody had written a clear concise answer as yet. Thanks in advance for your thoughtful, considered answers!
Re: JTable refresh with Hibernate
Is the model of you table based upon the tournamentList variable? Once you add items to the table model, you should fire the model's data changed function to notify all listeners...assuming the model is or extends off AbstractTableModel:
You can also call repaint() on the table, but this won't fire any table model listeners you have registered.
Re: JTable refresh with Hibernate
did u check the added rows in the database? are they really there? hibernate have some caches and the application gets the data there, not directly from the database. it seems your hibernate session is not synchronized with your database. did you try flushing the session after the query? it would help if you can post your hibernate configuration file.