0

I am new to hibernate. I am developing swing application using hibernate. I have created the method to fill the JTable as follows.

 public static void FillTable(JTable table,String cls){
    SessionFactory sf = ConnectionDao.getSessionFactory();
    Session session = sf.openSession();
    Query query=session.createQuery("from "+cls);//here persistent class name is cls
    ClassMetadata classMetadata = sf.getClassMetadata(cls);
    List l = query.list();
    Object[] columnNames = classMetadata.getPropertyNames();
    DefaultTableModel model = new DefaultTableModel(new Object[0][0],
            columnNames);
    for (int i=0; i<l.size();  i++) {
        CustomerBean cb = (CustomerBean) l.get(i);
        Object[] o = new Object[3];
        o[0] = cb.getCustomerFName();
        o[1] = cb.getCustomerCity();
        o[2] = cb.getCustomerCity();
        model.addRow(o);
    }
    table.setModel(model);
}

Above method takes the JTable and pojo class name as parameter and fill the JTable. I dont know how to make this method work for all pojo classes of any no. of properties. I am not getting any idea what to write in for loop so that it will work for all class. Because each class will have different no. of properties.

Any help will be appreciated. Thanks

1
  • 1
    What happens qhen you run this code? Are you sure it's a good idea to pass new Object[0][0] as a parameter to your DefaultTableModel? Commented Feb 9, 2015 at 8:32

1 Answer 1

2

I have never used Hibernate. From your code it looks like Hibernate is returning the "CustomerBean" object as a result of a query. If so then just store the CustomerBean object in the TableModel. Then you can use a custom TableModel that uses reflection to access the properties of the CustomBean class.

Check out Bean Table Model for a TableModel that supports this type of functionality.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.