I'm using Hibernate with HSQL database in a Swing application.
For representing data JTable is used with custom table model. The main entity in database is a Movie, and for getting a Movie there is a method:
Code:
public static Movie getMovie(Integer movieId) {
EntityManager em = DatabaseManager.getEntityManager();
return em.find(Movie.class, movieId);
}
So, table model also uses this method for filling the table. Here is how that table model looks:
Code:
public class MovieTableModel extends AbstractTableModel {
private List<Integer> idList;
public MovieTableModel() {
EntityManager em = DatabaseManager.getEntityManager();
Query query = em.createQuery("select m.id from Movie m");
idList = query.getResultList();
}
...
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Object result = null;
Movie movie = MovieManager.getMovie(idList.get(rowIndex));
if (movie == null)
return null;
switch (columnIndex) {
case 1:
result = movie.getTitle();
break;
case 2:
...
}
return result;
}
}
The problem is when getMovie method is called from two threads, for example from EDT when repainting table and SwingWorker that is used to export data to e.g. PDF ... and I get ConcurrentModificationException or NullPointerException.
Any suggestion how to solve this ?