Pergunta

I have an object called MyItemBean which can have 0 or more associated KeywordBean objects. The resulting classes look like this:

@Entity
public class MyItemBean {

   ...stuff...

   @ManyToMany(targetEntity = KeywordBean.class, cascade = CascadeType.PERSIST)
   @JoinTable(name = "tbl_item_keyword", joinColumns = @JoinColumn(name = "item_id"), inverseJoinColumns = @JoinColumn(name = "keyword_id"))
   private List<KeywordBean> keywords = null;

   ...more stuff...
}

@Entity
public class KeywordBean {

   ...stuff...

   private String value=null;

   ...more stuff...
}

I'm using JBoss Seam/Hibernate Search to index these objects so I can perform search queries against them. I'd like to be able to search for MyItemBean instances with a given keyword value. This relation, however, is unidirectional because I apply KeywordBean objects to more than just MyItemBean. I've looked in the Hibernate Search documentation for examples on how to index relations, but all of the examples they provide are bi-directional. Can anyone tell me what annotations I need to apply on MyItemBean.keywords to index the keyword values properly?

Foi útil?

Solução

The annotation to use is IndexedEmbedded. It works fine with unidirectional associations as well. A problem can occur if you are changing the value of KeywordBean Hibernate Search does not have a way to update the index for the MyItemBean instances which reference the changed KeywordBean. In bidirectional relations you can use @ContainedIn to fix this problem, but you don't really need this. Provided on your usecase this index updating constraint might not be an issue. Maybe your KeywordBean is not changing. Or if it changes you can re-index all *KeywordBean*s which are affected manually.

--Hardy

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top