Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2011
    Posts
    36
    Rep Power
    0

    Default [SOLVED] JTable Refresh -> Problem with Hibernate Session?

    Hi,

    I know that JTable data refreshing seems to be a very common problem. I have a JTable and I want to refresh it by pressing a button after I insert a new record in MySQL through INSERT INTO `users` (`userid`, `userdescription`, `username`) VALUES (1, 'Description test', 'Name test');

    I use (in theory this is working):

    Java Code:
    	private List<Object> usersList = null;
    	private final String[] columns = new String[] { "User name", "User description" };
    	private final boolean[] editableRows = { false, false };
    
    ...
    
    		tableUsers = new JTable();
    		// Get all Users
    		usersList = new UsersController().getUserList();
    		// Define TableModel with all Users
    		tableModel = new CustomTableModel(usersList, columns, editableRows);
    		// Use TableModel with Table
    		tableUsers.setModel(tableModel);
    		// Define a variable to be able to access Custom Table Model procedures
    		customTableModel = (CustomTableModel) tableUsers.getModel();
    
    ...
    	// When I want to refresh my JTable
    	private void refreshAction() {
    		usersList = new UsersController().getUserList();
    		tableModel = new CustomTableModel(usersList, columns, editableRows);
    		tableUsers.setModel(tableModel);
    	}
    I'm using Hibernate and my User List is populated through createCriteria applied to a Session, like this [From my GenericDAO java class]:

    Java Code:
    	public GenericDAO() {
    		this.session = HibernateUtil.getSession();
    		this.persistentClass = (Class<T>) ((ParameterizedType) getClass()
    				.getGenericSuperclass()).getActualTypeArguments()[0];
    	}
    
    	public Session getSession() {
    		return session;
    	}
    
    ...
    
    	@SuppressWarnings("unchecked")
    	protected List<T> findAll() {
    		return getSession().createCriteria(persistentClass).list();
    	}
    So I'm thinking that my problem is related to the Session, because when I close my App and re-run it I can get all values from DB, but if I insert them while running my App my refresh doesn't pick up the new data because maybe it needs to close the current session and create a new one. Am I right? Please, any suggestions are always welcome.

    Thank you.
    Last edited by buyapentiumjerk; 06-15-2011 at 02:17 AM. Reason: Solved
    There is not knowledge that it is not power!
    buyapentiumjerk.blogspot.com

  2. #2
    Join Date
    Apr 2011
    Posts
    36
    Rep Power
    0

    Default

    Ok, I've solved this issue by putting the Model Creation inside a procedure and whenever I alter mu data structure I just call that procedure and my Jtable gets all the information I need. So there is no problem with sessions and no more removeRow, setValueAt and addRow. One thing I0ve noticed though is that when I don't use Envers if I update my data directly in MySQL if I press Refresh button with this method I get the new data in my JTable but when using Envers this doesn't happen only when I insert a new Entity through the program. Ohh well, for me this just works.

    Thank you for reading.
    There is not knowledge that it is not power!
    buyapentiumjerk.blogspot.com

Similar Threads

  1. Could not able to refresh JTable.
    By pratim in forum AWT / Swing
    Replies: 3
    Last Post: 03-28-2011, 01:52 PM
  2. Hibernate - Session use
    By orion_mcl in forum New To Java
    Replies: 0
    Last Post: 10-31-2010, 11:54 PM
  3. Refresh session
    By cachi in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-07-2007, 07:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •