HibernateSearch学习3

Embedded and associated objects
Using @IndexedEmbedded to index associations
@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;
@ContainedIn
@OneToMany(mappedBy="address")
private Set<Place> places;
...
}

In this example, the place fields will be indexed in the Place index. The Place index documents
will also contain the fields address.id, address.street, and address.city which you will be
able to query. This is enabled by the @IndexedEmbedded annotation.
Be careful. Because the data is denormalized in the Lucene index when using the
@IndexedEmbedded technique, Hibernate Search needs to be aware of any change in the Place
object and any change in the Address object to keep the index up to date. To make sure the Place
Lucene document is updated when it's Address changes, you need to mark the other side of the
bidirectional relationship with @ContainedIn.
@ContainedIn is only useful on associations pointing to entities as opposed to embedded
(collection of) objects.
Let's make our example a bit more complex:
@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<Place> places;
...
}
@Embeddable
public class Owner {
@Field(index = Index.TOKENIZED)
private String name;
...
}

.。。。。。。。。。。。

你可能感兴趣的:(Hibernate,Lucene,UP)