When I edit a JTable cell in Java and persist it using Hibernate and then click off the cell my new value is saved but cell changes back to original value

When I edit a JTable cell in Java and persist it using Hibernate and then click off the cell my new value is saved but the cell changes back to original value when the cell loses focus.  I am using a TableModel for my JTable that extends AbstractTableModel.  I implement the method
public void setValueAt(Object value, int row, int col)
in my class that extends AbstractTableModel.  In this method I use an update to write the new value to the database through Hibernate.  The new value does get written to the database but when I click off the table cell the cell I changed goes back to the old value.  So the JTable does not refresh itself.  I do call the method fireTableDataChanged(); at the end of the
setValueAt(Object value, int row, int col) method.
gdkinney_2Asked:
Who is Participating?
 
ksivananthConnect With a Mentor Commented:
hoping that rockstars is a list, try this,

public void setValueAt(Object value, int row, int column) {
            Rockstar rs = rockstars.get(row);
            Session session = HibernateUtil.getSessionFactory().openSession();
            String temp = "update Rockstars set " + getColumnNm(column) + " = " + getValue(value, column) + " where ID = " + rs.getId();
            Query q = session.createSQLQuery(temp);
            q.executeUpdate();
            if( column == 0 ) rs.setName( value ) ;
            else if( column == 1 ) rs.setAge( ( Integer )value ) ;
            fireTableDataChanged();
        }
0
 
objectsCommented:
you need to also change the value in your table model. are you sure u are doing that?
whats the getValueAt() method lookk like?

0
 
ksivananthCommented:
>>The new value does get written to the database but when I click off the table cell the cell I changed goes back to the old value.  So the JTable does not refresh itself.  I do call the method fireTableDataChanged(); at the end of the
setValueAt(Object value, int row, int col) method.
>>

seems like the model used is not updated properly! can u post the code?
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
gdkinney_2Author Commented:
    Here is the method in my TableModel class that extends AbstractTableModel:  

     public void setValueAt(Object value, int row, int column) {
            Rockstar rs = rockstars.get(row);
            Session session = HibernateUtil.getSessionFactory().openSession();
            String temp = "update Rockstars set " + getColumnNm(column) + " = " + getValue(value, column) + " where ID = " + rs.getId();
            Query q = session.createSQLQuery(temp);
            q.executeUpdate();
            fireTableDataChanged();
        }
0
 
ksivananthCommented:
try this,

public void setValueAt(Object value, int row, int column) {
            Rockstar rs = rockstars.get(row);
            Session session = HibernateUtil.getSessionFactory().openSession();
            String temp = "update Rockstars set " + getColumnNm(column) + " = " + getValue(value, column) + " where ID = " + rs.getId();
            Query q = session.createSQLQuery(temp);
            q.executeUpdate();
            super.setValueAt( value, row, column ) ;
            fireTableDataChanged();
        }
0
 
objectsCommented:
you don't close the session, and you do not appear to update value in Rockstar.

0
 
ksivananthCommented:
sorry, if it is AbstractTableModel, you can't do that! can you post you getValueAt method as well?
0
 
gdkinney_2Author Commented:
       public Object getValueAt(int row, int column) {
            Rockstar ps = rockstars.get(row);
            switch (column) {

                case 0:
                    return ps.getName();
                case 1:
                    return ps.getAge();
                case 3:
                    return ps.getId();
                default:
                    return null;
            }
        }
0
 
gdkinney_2Author Commented:
Slight typo in previous post

public Object getValueAt(int row, int column) {
            Rockstar rs = rockstars.get(row);
            switch (column) {

                case 0:
                    return rs.getName();
                case 1:
                    return rs.getAge();
                case 3:
                    return rs.getId();
                default:
                    return null;
            }
        }
0
 
objectsCommented:
you need to set the value in the setvalueat method. Something like

Rs.setxyz(...
0
 
gdkinney_2Author Commented:
ksivananth: Your last post worked when I did an rs.set...  I don't fully understand how that worked though could someone explain please?
0
 
gdkinney_2Author Commented:
I do not really understand why it worked but it did.
0
 
ksivananthCommented:
>>I don't fully understand how that worked though could someone explain please?

though you have code for saving the changes to DB, you don't have code for updating the values used for show in table. actually the table uses the getValueAt method to update the view( display ). so when you set the value to Rockstar which is used in getValueAt, the updated value is shown!
0
 
objectsCommented:
>   I don't fully understand how that worked though could someone explain please?

as I mentioned earier you needded to update your value in your list (as well as in your db)


0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.