I am trying to implement within a small JavaFX desktop application a search textfield with auto suggestions like the famous search engines on the web. The auto suggestions are database driven in conjunction with Hibernate. For the textfield I am using ControlsFX library.
I have two issues:
1.) How can I display the correct student's name and first name in the suggestion list? (Presently I see the student object representation e.g. java.lang.object@23c884fb
) I even tried with List<String>
.
2.) My query doesn't return the correct results. E.g. if I type "a" I expect to get all student names which contain "a".
EntityManager em = JpaUtil.getEntityManager();
em.getTransaction().begin();
//System.out.println(searchField.getText());
Query q = em.createQuery("SELECT s.name, s.firstName FROM Student AS s WHERE s.name like :name");
q.setParameter("name", ("%" + searchField.getText().toLowerCase().trim() +"%"));
List<Student> results = (List<Student>) q.getResultList();
TextFields.bindAutoCompletion(searchField, results);
em.getTransaction().commit();
em.close();
Thanks. (I am still learning).