Is @DocumentId required for Hibernate Search?

大兔子大兔子 提交于 2019-12-06 02:12:03

问题


I'm using Hibernate Search and the documentation and books say I need @DocumentId on the id field so that Hibernate Search can know how to map the index to the objects.

My code appears to be working fine without the @DocumentId anywhere in my code. Did Hibernate Search become smart enough to figure out that @Id field is a great default? Are there problems this will cause that are not obvious?

Thanks for your time!


回答1:


@DocumentId is required if you are using the old-school style of mapping your entities with .hbm.xml files. If you are use that mapping approach and neglect to annotate a document id, then at startup you will see an exception like this:

org.hibernate.search.SearchException: No document id in: com.mypackage.MyEntity

However, if you are using annotations and have annotated a primary key with @Id, then you do not have to use @DocumentId.

To be more precise, the Hibernate Search documentation says that @DocumentId is optional when using JPA annotations. So perhaps you would still need to use @DocumentId if you are using Hibernate 3.x-style annotations... I've never tested this.

Either way, Hibernate 4.x deprecates its own mapping annotations in favor of JPA-style annotations, even if you are using Hibernate's Session rather than JPA's EntityManager for your queries. So in a nutshell: you need to use @DocumentId if you are using XML-style mappings... whereas it's optional if you're using annotations, because at this point you should be using JPA-style annotations anyway.



来源:https://stackoverflow.com/questions/12377808/is-documentid-required-for-hibernate-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!