再谈compass:集成站内搜索2011-01-05 javaeye lqw8668前段时间已经写了一篇关于compass的文章,相信大家对compass也已经有了一定的了解由于最近做的项目中涉及到了站内搜索,而且是基于JPA注解形式的,在网上找了好久,关于JPA集成compass的例子很少,有些也是基于 xml的,基于注解形式的甚是少,没有办法只有去compass的官网下载英文文档自己研究一下,花费了一下午时间调试出来,集成到项目中!在这里给大家分享下,希望大家可以少走些弯路!1.去官方网站下载compass的jar包,我用的的2.1版本http://www.compass-project.org/ProductInfo.javaJava代码@Entity @Searchable public class ProductInfo implements Serializable{ private static final long serialVersionUID = -8860864584425256200L; private Integer id; /** 货号 **/ private String code; /** 产品名称 **/ private String name; /** 产品类型 **/ private ProductType type; /** 产品样式 **/ private Set<ProductStyle> styles = new HashSet<ProductStyle>();
public ProductInfo() {}
@OneToMany(cascade={CascadeType.REMOVE,CascadeType.PERSIST}, mappedBy="product",fetch=FetchType.EAGER) @OrderBy("visible desc, id asc") @SearchableReference public Set<ProductStyle> getStyles() { return styles; } public void setStyles(Set<ProductStyle> styles) { this.styles = styles; }
@Id @GeneratedValue @SearchableId public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Column(length=30) @SearchableProperty(index = Index.TOKENIZED, store = Store.YES) public String getCode() { return code; } public void setCode(String code) { this.code = code; } @Column(length=50,nullable=false) @SearchableProperty(index = Index.TOKENIZED, store = Store.YES) public String getName() { return name; } public void setName(String name) { this.name = name; }
@ManyToOne(cascade=CascadeType.REFRESH,optional=false) @JoinColumn(name="typeid") @SearchableReference public ProductType getType() { return type; } public void setType(ProductType type) { this.type = type; }
@Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((id == null) ? 0 : id.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; final ProductInfo other = (ProductInfo) obj; if (id == null) { if (other.id != null) return false; } else if (!id.equals(other.id)) return false; return true; } }