Greeting,
I am using hibernate search. Can anyone help me sort out following query issue I have?
I have a Student object that has a teacher type property. I mapped them as one-one relationship.
Student.hbm.xml:
<many-to-one unique="true" name="user" class="Student" column="student_fk" lazy="false" cascade="none" not-found="ignore" update="true" insert="true" />
So I could query students based on their teacher properties.
------------
Now, I have a new requirement on teacher query. I need to query teachers based on their student name similar as following:
query.add(new TermQuery(new Term("students.name", "jack"), BooleanClause.Occur.MUST);
As result, I need to build a two-way relatinoship bw Teacher and Student. But I just only need student's name in query. SO I added a mini version of student object that only contains student name, and mapped it in Teacher.hbm.xml
<set name="students" table="student" cascade="none" lazy="false"> <key column="user_name_fk" not-null="true" /> <one-to-many class="StudentMiniProfile" /> </set>
public class Teacher { private Set<StudentMiniProfile> students;
..getters and setters }
After that, I started to index teachers, and did query. Search result is what I expected. However building teacher index became a time-consuming job. Because a teacher could have thousands of students, that means a lots of extra data queries at background.
--- StudentA has TeacherA. TeacherA has thousands Students including StudentA.
The true nightmare is to build student index. Remember that we have one2one relationship for Student and Teacher, so each of student has a new property in Index, student.teacher.students! (any way to avoid this in index?). It is extremely slow to build student index now.
BTW, is it at all possible to initialize a collection of student name using a single custom sql query in Teacher entity mapping? I think, The reason why the query slow is, hibernate have to iterate students by lots of quires to create a Teacher instance. If it is possbile, it might help my situation.
For property students in Teacher, I hope it is a String type instead of Set<StudentMiniProfile>, and the value format is name1,name2,name3... . Is any way to do it by mapping in hibernate?
Thanks, Ian.
|