我有一个已注释的类,我正在尝试对来自lucene/hibernate搜索查询的结果进行排序。我终于成功地让查询正常工作,但当我实施必要的注释(在jobStatus上)以对该列进行排序时,就无法搜索该列了。我是基于我在这里找到的指南。我一直在困惑如何处理整个hibernate搜索和排序的问题,现在我终于弄清楚如何进行排序和搜索,现在我需要能够同时进行它们。
@Entity
@Table(name="jobReq")
@Indexed
public class JobReq {
@Id
@DocumentId
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@Field(index = Index.YES)
@Column(name="jobId", nullable=false, unique=true)
private String jobId;
@Field(index = Index.YES)
@Column(name="jobTitle", nullable=false)
private String jobTitle;
@Field(index = Index.YES)
@Column(name="jobContract", nullable=false)
private String contract;
@Field(index = Index.YES)
@Column(name="jobProject", nullable=true)
private String project;
@Field(index = Index.YES)
@Column(name="jobLaborCategory", nullable=false)
private String laborCategory;
@Field(index = Index.YES)
@Column(name="jobSummary", nullable=false)
private String summary;
@Field(index = Index.YES)
@Column(name="jobDescription", nullable=false)
private String jobDescription;
@Fields({@Field, @Field(analyze = Analyze.NO, name = "jobStatus")})
@Column(name="jobStatus", nullable=false)
private String status;
@Field(index = Index.YES)
@Column(name="TTONumber", nullable=false)
private String TTONumber;
@Field(index = Index.YES)
@Column(name="jobPostedDate", nullable=false)
@Type(type="date")
private Date postedDate;
还有来自搜索功能的片段
Field[] allFields = this.type.getDeclaredFields();
SortField field =new SortField(sortColumn, SortField.STRING, reverseSort);
Sort sort = new Sort(field);
hibQuery = fullTextSession.createFullTextQuery(bq, this.type).setSort(sort);
results = hibQuery.list();
@Fields({@Field, @Field(analyze = Analyze.NO, name = "jobStatus")})
并用@Field()
替换它,然后尝试根据列“jobStatus”和“status”对其进行排序。但是它没有排序,我不确定这是因为变量名为“status”,而数据库中的列名为“jobStatus”。 - Adam JamesORDER BY
可以做的内容。Criteria系统基本上为您提供了一种干净的方式来以编程方式构建HQL查询(而不是显式地构建查询字符串)。对于基于业务值的排序,您将不得不在业务逻辑中进行排序,通过使用自定义的“比较器”对对象集合进行排序。或者,如果更适合您,您可以分别选择“打开”和“关闭”的项目,并将两个列表连接起来。 - Jason C