HQL Join gives me weird data for the joined column
While my HQL query is working fine in the Hibernate Console,
i get weird results.
I tried to change the row below in conjunction with @FXML annotations of the corresponding column, but either i get errors, either i get blank cells.
vstcolName.setCellValueFactory(new PropertyValueFactory("peopleByPeopleId"));
Here are my Entity classes for the 1st table.
package Entities;
import javax.persistence.*;
import java.util.Objects;
@Entity
@Table(name = "people", schema = "learn", catalog = "")
public class PeopleEntity {
private int pplId;
private String pplName;
@Id
@Column(name = "pplID")
public int getPplId() {
return pplId;
}
public void setPplId(int pplId) {
this.pplId = pplId;
}
@Basic
@Column(name = "pplName")
public String getPplName() {
return pplName;
}
public void setPplName(String pplName) {
this.pplName = pplName;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PeopleEntity that = (PeopleEntity) o;
return pplId == that.pplId &&
Objects.equals(pplName, that.pplName);
}
@Override
public int hashCode() {
return Objects.hash(pplId, pplName);
}
}
Here are my Entity classes for the 2nd table.
package Entities;
import javax.persistence.*;
import java.util.Objects;
@Entity
@Table(name = "places", schema = "learn", catalog = "")
public class PlacesEntity {
private int plcId;
private String place;
private PeopleEntity peopleByPeopleId;
@Id
@Column(name = "plcID")
public int getPlcId() {
return plcId;
}
public void setPlcId(int plcId) {
this.plcId = plcId;
}
@Basic
@Column(name = "place")
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PlacesEntity that = (PlacesEntity) o;
return plcId == that.plcId &&
Objects.equals(place, that.place);
}
@Override
public int hashCode() {
return Objects.hash(plcId, place);
}
@ManyToOne
@JoinColumn(name = "people_ID", referencedColumnName = "pplID")
public PeopleEntity getPeopleByPeopleId() {
return peopleByPeopleId;
}
public void setPeopleByPeopleId(PeopleEntity peopleByPeopleId) {
this.peopleByPeopleId = peopleByPeopleId;
}
}
Here is my model that contains the HQL Query.
package Models;
import Entities.PlacesEntity;
import Interfaces.PlacesIF;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import java.util.List;
public class PlacesIM implements PlacesIF {
public List<PlacesEntity> readJoin() {
session = sessionFactory.openSession();
Query query;
query = session.createQuery("SELECT PlcEnt.place as PLACES, pep.pplName as NAMES FROM PlacesEntity as PlcEnt JOIN PlcEnt.peopleByPeopleId as pep");
List<PlacesEntity> tableList;
tableList = query.list();
return tableList;
}
}
Here is the FXML Controller
package FX;
import Entities.PeopleEntity;
import Entities.PlacesEntity;
import Models.PlacesIM;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
public class placesController implements Initializable {
//Visit TableView
@FXML
TableView<PlacesEntity> vstTView;
//I think that this line may need modification
@FXML
private TableColumn<PlacesEntity, PeopleEntity> vstcolName;
@FXML
private TableColumn<PlacesEntity, String> vstcolPlace;
public void readJoin() {
plcTView.getItems().clear();
List<PlacesEntity> tableList = plcIM.read();
for (PlacesEntity pEnt : tableList) {
observableList.add(pEnt);
}
//Here is the other tricky part (that's what i think at least)
vstcolName.setCellValueFactory(new PropertyValueFactory<PlacesEntity, PeopleEntity>("peopleByPeopleId"));
vstcolPlace.setCellValueFactory(new PropertyValueFactory<PlacesEntity, String>("place"));
vstTView.setItems(observableList);
}
}
The data that is retuned in the joined column is something like this Entities.PeopleEntity@864db1b7
...etc... I should get Names - i mean strings.. The other column is displayed normally.
By the way, i have exactly the same result, if i try to display the result of the HQL query in the console, instead of the tableview. Don't mind the change of the weird column. It's because i tried to place numbers (still strind data type) in the place of the names that i had before, to see if anything will change... But the problem still persists.