我有这两个pojos:
@Entity
@Table(name = "ITEM", uniqueConstraints = @UniqueConstraint(
columnNames = {"ITEM_CODE", "ITEM_CODE_VERSION"}
))
@Indexed
@AnalyzerDef(name = "customAnalyzer", tokenizer = @TokenizerDef(factory = LetterTokenizerFactory.class),
filters = {
@TokenFilterDef(factory = LowerCaseFilterFactory.class),
})
public class Item implements Serializable {
@Field
private String brand;
@Id
@GeneratedValue(strategy=GenerationType.AUTO, generator = "item_generator")
@SequenceGenerator(name = "item_generator",sequenceName = "ITEM_SEQ")
@Column(name = "ITEM_ID")
private Integer id;
@Field(index = Index.YES, analyze = Analyze.YES, store = Store.YES)
@FieldBridge(impl = IntegerBridge.class)
@Column(name = "ITEM_CODE")
private Integer itemCode;
@Column(name = "ITEM_CODE_VERSION")
private Integer itemCodeVersion;
@Column(name="TXT_MODULE_NAME")
@Field
private String moduleName;
@OneToMany(mappedBy = "item", fetch= FetchType.EAGER)
@IndexedEmbedded
private Set<CriticalComponent> criticalComponents = new HashSet<>();
//getters and setters
和
@Entity
@Table(name = "CRITICAL_COMPONENT")
@Indexed
public class CriticalComponent implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO, generator = "crit_comp_generator")
@SequenceGenerator(name = "crit_comp_generator",sequenceName = "CRIT_COMP_SEQ")
@Column(name = "CRITICAL_COMPONENT_ID")
private Integer id;
@ContainedIn
@ManyToOne
@JoinColumns(value = {
@JoinColumn(name = "ITEM_CODE", referencedColumnName = "ITEM_CODE"),
@JoinColumn(name = "ITEM_CODE_VERSION", referencedColumnName = "ITEM_CODE_VERSION")
})
private Item item;
@Field(index = Index.YES, analyze = Analyze.YES, store = Store.YES)
@Column(name = "CRITICAL_FILENAME")
private String criticalFilename;
//getters and setters
我有一个从Item OneToMany到CriticalComponent的关系,我想构建一个lucene查询,通过ItemCode搜索一个CriticalComponent(第二个pojo),这是一个来自Item的字段但是当我从视图中点击搜索按钮时它会给我这个错误:
出现意外错误(type = Internal Server Error,status = 500)。 无法在io.package.CriticalComponent中找到字段itemCode。
这是hibernate搜索服务:
@Transactional
public List<CriticalComponent> searchForCriticalComponent(String searchTerm){
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);
QueryBuilder qb = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(CriticalComponent.class).get();
//Query luceneQuery = qb.keyword().fuzzy().withEditDistanceUpTo(1).withPrefixLength(1).onFields("criticalFilename", "itemCode", "serviceName").matching(searchTerm).createQuery();
Query luceneQuery = qb.keyword().wildcard().onFields("criticalFilename","itemCode").matching(searchTerm.toLowerCase()+'*').createQuery();
javax.persistence.Query jpaQuery = fullTextEntityManager.createFullTextQuery(luceneQuery, CriticalComponent.class);
List<CriticalComponent> criticalComponentsModel = null;
try {
criticalComponentsModel = jpaQuery.getResultList();
} catch (NoResultException nre) {
log.debug("Search: No result found");
}
return criticalComponentsModel;
}
如何通过来自criticalcomponent类的itemcode进行搜索?
答案 0 :(得分:0)
我解决了。
这里有变化:
Item.java看起来相同但必须有@ContainedIn注释
@OneToMany(mappedBy = "item", fetch= FetchType.EAGER)
@ContainedIn
private Set<CriticalComponent> criticalComponents = new HashSet<>();
CriticalComponent.java相同但必须有@IndexEmbedded批注
@IndexedEmbedded
@ManyToOne
@JoinColumns(value = {
@JoinColumn(name = "ITEM_CODE", referencedColumnName = "ITEM_CODE"),
@JoinColumn(name = "ITEM_CODE_VERSION", referencedColumnName = "ITEM_CODE_VERSION")
})
private Item item;
Hibernate Search Service查看由点访问的关系字段:
@Transactional
public List<CriticalComponent> searchForCriticalComponent(String searchTerm){
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);
QueryBuilder qb = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(CriticalComponent.class).get();
//Query luceneQuery = qb.keyword().fuzzy().withEditDistanceUpTo(1).withPrefixLength(1).onFields("criticalFilename", "itemCode", "serviceName").matching(searchTerm).createQuery();
Query luceneQuery = qb.keyword().wildcard().onFields("criticalFilename","serviceName","item.itemCode").matching(searchTerm.toLowerCase()+'*').createQuery();
javax.persistence.Query jpaQuery = fullTextEntityManager.createFullTextQuery(luceneQuery, CriticalComponent.class);
List<CriticalComponent> criticalComponentsModel = null;
try {
criticalComponentsModel = jpaQuery.getResultList();
} catch (NoResultException nre) {
log.debug("Search: No result found");
}
return criticalComponentsModel;
}