Nested Usage of Indexing Associations
Let's make Example 4.4, “Indexing associations” a bit more complex by nesting @IndexEmbedded as seen in Example 4.5, “Nested usage of @IndexedEmbedded and @ContainedIn”. Example 4.5. Nested usage of @IndexedEmbedded and @ContainedIn @Entity @Indexed public class Place { @Id @GeneratedValue @DocumentId private Long id; @Field( index = Index.TOKENIZED ) private String name; @OneToOne( cascade = { CascadeType.PERSIST, CascadeType.REMOVE } ) @IndexedEmbedded private Address address; .... } @Entity public class Address { @Id @GeneratedValue private Long id; @Field(index=Index.TOKENIZED) private String street; @Field(index=Index.TOKENIZED) private String city; @IndexedEmbedded(depth = 1, prefix = "ownedBy_") private Owner ownedBy; @ContainedIn @OneToMany(mappedBy="address") private Set places; ... } @Embeddable public class Owner { @Field(index = Index.TOKENIZED) private String name; ... } As you can see, any @*ToMany, @*ToOne and @Embedded attribute can be annotated with @IndexedEmbedded. The attributes of the associated class will then be added to the main entity index. In Example 4.5, “Nested usage of @IndexedEmbedded and @ContainedIn” the index will contain the following fields id name address.street address.city address.ownedBy_name The default prefix is propertyName., following the traditional object navigation convention. You can override it using the prefix attribute as it is shown on the ownedBy property. Note The prefix cannot be set to the empty string. The depth property is necessary when the object graph contains a cyclic dependency of classes (not instances). For example, if Owner points to Place. Hibernate Search will stop including Indexed embedded attributes after reaching the expected depth (or the object graph boundaries are reached). A class having a self reference is an example of cyclic dependency. In our example, because depth is set to 1, any @IndexedEmbedded attribute in Owner (if any) will be ignored.