I'm writing Hibernate JSF application and I currently have one problem. I try to make in xhtml dropdown to select id of foreign key.
xhtml file:
<div class="form-group">
<h:outputLabel for="student" class="control-label col-sm-2">Student</h:outputLabel>
<div class="col-sm-6">
<h:selectOneMenu id="student" class="form-control" value="#{studentGroupID.studentID}" required="true">
<f:selectItems value="#{data.showStudentsOfFaculty(studentGroupID.groupID)}" var="user" itemValue="#{user.studentID}" itemLabel="#{user.toString()}" />
</h:selectOneMenu>
<h:message for="student" style="color:red" />
</div>
</div>
data.showStudentsOfFaculty():
public ArrayList<Student> showStudentsOfFaculty(int groupId) {
session = helper.getSessionFactory().openSession();
String hql = "select s.studentID, s.name, s.surname, s.address, s.city, s.emailAddress from FacultyStudent ws, Student s, Group g, Subject p "+
"where ws.facultyStudentID.studentID=s.studentID and g.subjectID=p.subjectID and p.facultyID=ws.facultyStudentID.facultyID and g.groupID="+groupId;
Iterator query = session.createQuery(hql).list().iterator();
ArrayList<Student> students = new ArrayList<>();
while ( query.hasNext() ) {
Object[] results = (Object[]) query.next();
Student st = new Student((String)results[1],(String)results[2],(String)results[3],(String)results[4],(String)results[5]);
st.setStudentID((Integer)results[0]);
students.add(st);
}
session.close();
return students;
}
Every time I try to sumbit form I get error:
addStudent:student: Validation Error: Value is not valid
When I use other function data.showStudents() intead of one above form works perfectly but it shows too much data.
Data.showStudents():
public List<Student> showStudents() {
session = helper.getSessionFactory().openSession();
String hql = "FROM Student S";
Query query = session.createQuery(hql);
List<Student> results = (List<Student>) query.list();
session.close();
return results;
}