0

I have a problem with lucene indexation, I insert one indexed entity in a manyToMany association but lucene doesn't index as I expected.

@Entity  
@Indexed  
@Table(name="level")  
public class Level {  

...  
     @IndexedEmbedded
     private List<Course> courses = new ArrayList<Course>();  

     @ManyToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY)  
     @JoinTable(name = "level_course", joinColumns = { @JoinColumn(name = "level_id", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "course_id", nullable = false, updatable = false) })  
@OrderColumn(name="corder")  
public List<Course> getCourses() {  
    return courses;  
}  
    ...  
}    

@Entity  
@Indexed  
@Table(name="course")  
@FullTextFilterDef(name = "filterLevel", impl = LuceneFilterFactory.class ,cache=FilterCacheModeType.NONE)  
public class Course {  
    ...
    @ContainedIn
private List<Level> levels = new ArrayList<Level>();  

    @ManyToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="courses")  
public List<Level> getLevels() {  
    return levels;  
}  
}  

When I do : level.getCourses().add(myCourse1); entityManager.save(level);

myCourse1 (with ID #10 for example) will be well created and attached to level (level is parent class, Course is the child). Then instances of "Course" are well indexed but if I have a look to the indexes generated for Course I expected to find "levels.id" with value #10. But I don't find it. I need this kind of indexation because I use LuceneFilterFactory.class on Course to filter course by one level id.

Maybe my use of @ContainedIn and @IndexEmbedded annotations is not good ? Or maybe I'm completely in the wrong way to do what I need.

To simplify :
I have 2 classes A and B, with a manyToMany association between A and B.
A is master on the relation. A and B are indexed. I'd like to use hibernate search for getting B objects that contains one A object in their manyToMany association. I don't want to get all B but only B objects that contains this specific A.

How to do this ?

Thanks for your help

Sanne
  • 6,027
  • 19
  • 34
Nico
  • 3,430
  • 4
  • 20
  • 27
  • where do you set the other side of the bidirectional association (meaning updating the levels list in course?). you need to update both sides of the association. It would also help if you include the indexing code as well as the actual search you are trying to execute. – Hardy Apr 27 '11 at 14:29
  • I didn't set the other side, I tried to change the mapping by putting @IndexedEmbedded in the class Course over the property levels. And then I updated both sides of the association, and you're right, now I have the result I expected. But there is one important problem by setting both sides of the association : – Nico Apr 27 '11 at 16:12
  • I didn't set the other side, I tried to change the mapping by putting @IndexedEmbedded in the class Course over the property levels. And then I updated both sides of the association, and you're right, now I have the result I expected. But there is one important problem by setting both sides of the association : level.getCourses().add(myCourse1); myCourse1.getLevels().add(level); entityManager.save(level); ==> JPA will load two collections (level.courses and myCourse1.levels) or am I mistaken about the jpa collection loading ? Thanks for your help – Nico Apr 27 '11 at 16:26
  • Setting both sides of the relation is essential. It has nothing with Search to do, but is just basic Hibernate (JPA) behavior/requirement. – Hardy Apr 29 '11 at 06:38

1 Answers1

0

Have you tried to place the annotations consistently? Either all on the fields or all on the getters?

Hardy
  • 18,659
  • 3
  • 49
  • 65
  • I've added more explanations in my question, I hope this will help, because I recognise my question wasn't very clear. – Nico Apr 27 '11 at 12:37
  • !!! I valid hardy response upper : "Setting both sides of the relation is essential. It has nothing with Search to do, but is just basic Hibernate (JPA) behavior/requirement." !!! – Nico May 18 '11 at 12:52