我有以下两个pojo:

@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(2nd pojo),它是Item中的一个字段,但是当我从视图中单击搜索按钮时,它将出现此错误:


  发生意外错误(类型=内部服务器错误,状态= 500)。
  在io.package.CriticalComponent中找不到字段itemCode。


这是休眠搜索服务:

@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搜索?

最佳答案

我解决了

这里是变化:

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;
}

10-04 10:21