user1091733 user1091733 - 18 days ago 6
Java Question

Hibernate entity containing collection - How to show it in JTable

There is a simple entity that contains collection of another

Document
entity.

class Client{
private int id;
private String name;
private String secondName;
private Set<Document> documents = new HashSet()<Document>;
//getters and setters
}


I want to show all clients in
JTable
, but client must appear each time for each document he has, for example if client has 3 documents in set, then he should appear in 3 rows with different documents. I have written my own
TableModel
. Is there a way to retrieve from hibernate such suitable collection (I think list of clients with only one document in set)?

Answer

For this two-level view, I'd consider two alternatives:

  • org.netbeans.swing.outline.Outline, shown here, would be an appealing choice.

  • Use one JTable to display the master rows, and add a ListSelectionListener that would update a second table's model to display the detail rows for the selected row in the master table. See User Selections for details.

Addendum: In your TableModel, you can query the database in your implementation of getValueAt(), as suggested in this example.