I'm trying to make an application that gets it's data from database and displays it in JavaFX TableView control. In my database I have two tables: members and members_details. Members and members_details have one to one relationship. Table members has 'members_details_id' column which is a foreign key that points to column 'id' in members_details table.
Using the HQL query List<Object[]> members = session.createQuery("from Members m inner join m.membersDetails").getResultList();
I can get the data I want.
Is it possible to populate TableView using the data gathered with the above query? I have no problem populating the table with just members or members_data but I have no clue how to populate it with both types of data.
EDIT
I tried what user @fabian suggested but I still can't get it to work. Here is the code of my class that controls the TableView. The app crashes immediately after starting.
public class MembersScreenController {
private SessionFactory factory;
private Session session;
@FXML
private TableView<Object[]> membersTable;
@FXML
private TableColumn<Members,Integer> idCol;
@FXML
private TableColumn<Members,String> firstNameCol;
@FXML
private TableColumn<Members,String> lastNameCol;
@FXML
private TableColumn<MembersDetails, String> addressCol;
@FXML
private TableColumn<MembersDetails, String> phoneNumberCol;
@FXML
private TableColumn<MembersDetails, String> emailCol;
public void initialize() {
factory = Factory.getSessionFactory();
session = factory.getCurrentSession();
session.beginTransaction();
List<Object[]> list = session.createQuery("from Members m inner join m.membersDetails").getResultList();
session.close();
Factory.closeFactory();
ObservableList<Object[]> listObs = FXCollections.observableArrayList(list);
idCol.setCellValueFactory(cd -> new SimpleIntegerProperty(((Members)cd.getValue()).getId()).asObject());
firstNameCol.setCellValueFactory(cd -> new SimpleStringProperty(((Members)cd.getValue()).getFirstName()));
lastNameCol.setCellValueFactory(cd -> new SimpleStringProperty(((Members)cd.getValue()).getLastName()));
addressCol.setCellValueFactory(cd -> new SimpleStringProperty(((MembersDetails)cd.getValue()).getAddress()));
phoneNumberCol.setCellValueFactory(cd -> new SimpleStringProperty(((MembersDetails)cd.getValue()).getPhoneNum()));
emailCol.setCellValueFactory(cd -> new SimpleStringProperty(((MembersDetails)cd.getValue()).getAddress()));
membersTable.setItems(listObs);
}
And here is the part of the FXML file that defines my TableView
<TableView fx:id="membersTable">
<columns>
<TableColumn fx:id="idCol" text="id"/>
<TableColumn fx:id="firstNameCol" text="First Name"/>
<TableColumn fx:id="lastNameCol" text="Last Name"/>
<TableColumn fx:id="addressCol" text="Address"/>
<TableColumn fx:id="phoneNumberCol" text="Phone Number"/>
<TableColumn fx:id="emailCol" text="Email"/>
</columns>
</TableView>
Another thing that comes to mind is to have two separate querys. With one I could retrieve all from members table and with the other all from members_details table. That would leave me with two lists, one with members objects and the other with membersdetails object. But I still have no idea how to populate TableView.
`](https://openjfx.io/javadoc/12/javafx.controls/javafx/scene/control/TableColumn.CellDataFeatures.html) object where `S` is the `TableView` item type and `T` is the cell value type and needs to return a `ObservableValue`. `Simple...Property` implements this. – fabian Dec 27 '19 at 16:26