首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 网络基础 >

Hibernate search的@ContainedIn跟@IndexedEmbedded

2012-09-21 
Hibernate search的@ContainedIn和@IndexedEmbeddedIndexedEmbedded用此注解标注的对象为被嵌入对象,比如

Hibernate search的@ContainedIn和@IndexedEmbedded
       IndexedEmbedded用此注解标注的对象为被嵌入对象,比如我在Blog类中有一个Auther auther的属性,当用@IndexedEmbedded标注此属性时,表示可以用auther.(Auther的属性)进行搜索。当@IndexedEmbedded标注的为一个对象,则在此对象中进行搜索,最终返回的是一个对象。当其标注的是一个集合(如Set<Auther> authers)时,则在所有是集合中搜索,返回的可能是多个对象。

       @ContainedIn根据官方API的解释是用于维护被嵌入对象的一致性,也就是说,为了维护当上面例子上auther改变时Blog中对应于auther的索引也更新,但在实际运用中,并不会产生这样的效果。

        比如当我添加一篇blog时,指定的auther为admin,此时可以根据auther.name为admin搜索出这篇blog。但如果此时把admin改为admins,理论上应该用admins把这篇blog搜索出来,但实际上还是用admin才能把这篇blog搜索出来。所以把admin改为admins只是在Auther中更新了索引,但在Blog中未更新索引,此时的索引需要我们手动去更新此auther对应的所有的Blog。

        至此,好像添加@ContainedIn没有启动应有的作用,根据去Hibernate Search官方论坛了解到,即使添加了@ContainedIn也需要开发者去维护双方的索引的一致性。

下面是我用到了两个实体类

@Indexed@Entity@Table(name="blog")public class Blog {private String id;private String title;private String content;private Date createDate;private int sort;private Set<BlogType> blogTypes;private Auther auther;@Id@GeneratedValue(generator = "system-uuid")@GenericGenerator(name="system-uuid", strategy="uuid")@DocumentIdpublic String getId() {return id;}public void setId(String id) {this.id = id;}@Column@Field(name="title",index=Index.TOKENIZED,store=Store.YES)public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}@Column@Field(name="content",index=Index.TOKENIZED,store=Store.NO)public String getContent() {return content;}public void setContent(String content) {this.content = content;}@Column@Field(name="date",index = Index.UN_TOKENIZED, store = Store.YES)@DateBridge(resolution = Resolution.DAY)public Date getCreateDate() {return createDate;}public void setCreateDate(Date createDate) {this.createDate = createDate;}@Field(name="sort",index = Index.UN_TOKENIZED, store = Store.YES)@NumericField@Columnpublic int getSort() {return sort;}public void setSort(int sort) {this.sort = sort;}@OneToMany(mappedBy="blog")public Set<BlogType> getBlogTypes() {return blogTypes;}public void setBlogTypes(Set<BlogType> blogTypes) {this.blogTypes = blogTypes;}@ManyToOne@JoinColumn(name="autherId")@IndexedEmbeddedpublic Auther getAuther() {return auther;}public void setAuther(Auther auther) {this.auther = auther;}}


@Indexed@Entity@Table(name="auther")public class Auther {private String id;private String name;private Set<Blog> blogs;@Id@GeneratedValue(generator = "system-uuid")@GenericGenerator(name="system-uuid", strategy="uuid")@DocumentIdpublic String getId() {return id;}public void setId(String id) {this.id = id;}@Column@Field(name="authername",index=Index.TOKENIZED,store=Store.YES)public String getName() {return name;}public void setName(String name) {this.name = name;}@ContainedIn@OneToMany(mappedBy="auther",cascade = CascadeType.ALL)public Set<Blog> getBlogs() {return blogs;}public void setBlogs(Set<Blog> blogs) {this.blogs = blogs;}}

热点排行