0
當我嘗試在JavaFX TableView中顯示數據庫中的數據時遇到了一個小問題。問題JFX TableView和HQL查詢
我有一個學生表
user_id | name | nick | password | surname | classroom_id
---------|-----------|-----------|-----------|-----------|--------------
2 | AAA | studentA | test123 | AAA | 1
3 | BBB | studentB | test321 | BBB | 1
該表連接到課堂表
classroom_id | classrooName | classroomYear
--------------|--------------|-------------
1 | TestClass | 2016/2017
我使用這個查詢與他們所屬的教室讓所有的學生一起,沒有問題在這裏。當我嘗試打印出值時,一切都是正確的。
Select student from Student student Join Fetch student.classroom
現在,實際的問題是,classroomName和classroomYear不TableView中this顯示,像(Jméno - 姓名,Příjmení - 姓氏,尼克 - ,Třída - 教室名,Rok-課堂年)
單元格值工廠這2列
classroomNameColumn.setCellValueFactory(new PropertyValueFactory<>("classroomName"));
classroomYearColumn.setCellValueFactory(new PropertyValueFactory<>("classroomYear"));
現在,我設法得到顯示所有數據的唯一方法是,以classroomName和classroomYear添加到裏面加他們爲2我的Java類額外的列數據庫。這種方式違背了我認爲的加入ID的目的,是否有更好的方式可以讓我的TableView中顯示「課堂」數據?
編輯:這裏是學生和課堂爲Java類
@Entity
public class Student extends User{
private Classroom classroom;
public Student() {
}
public Student(String name, String surname, String nick, String password, Classroom studentClassroom) {
super(name, surname, nick, password);
this.classroom = studentClassroom;
}
@ManyToOne
@JoinColumn(name = "classroom_id")
public Classroom getClassroom() {
return classroom;
}
public void setClassroom(Classroom classroom) {
this.classroom = classroom;
}
}
@Entity
public class Classroom {
private SimpleStringProperty classroomName = new SimpleStringProperty();
private SimpleStringProperty classroomYear = new SimpleStringProperty();
private int classroom_id;
public Classroom() {
}
public Classroom(String name, String year) {
classroomName.set(name);
classroomYear.set(year);
}
@Column(nullable = false)
public String getClassroomName() {
return classroomName.get();
}
public SimpleStringProperty nameProperty() {
return classroomName;
}
public void setClassroomName(String name) {
this.classroomName.set(name);
}
@Column(nullable = false)
public String getClassroomYear() {
return classroomYear.get();
}
public SimpleStringProperty classroomYearProperty() {
return classroomYear;
}
public void setClassroomYear(String year) {
this.classroomYear.set(year);
}
@Id
@GenericGenerator(name="id" , strategy="increment")
@GeneratedValue(generator="id")
public int getClassroom_id() {
return classroom_id;
}
public void setClassroom_id(int classroom_id) {
this.classroom_id = classroom_id;
}
}
真棒,作品像一個魅力。我認爲這是因爲學生課沒有課堂年份和姓名,但我不知道如何寫。非常感謝你。 –