本文介绍了Hibernate搜索需要@DocumentId吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Hibernate Search,文档和书籍说我需要ID字段的@DocumentId,以便Hibernate Search可以知道如何将索引映射到对象。



我的代码似乎在我的代码中任何地方没有@DocumentId的情况下工作正常。 Hibernate Search是否足够聪明地发现@Id字段是一个很好的默认值?



感谢您的时间!

解决方案

@DocumentId 是必需的,如果您使用旧式风格的映射您的实体 .hbm.xml 文件。如果您使用该映射方法而忽略注释文档ID,则在启动时您将看到如下异常:

  org.hibernate.search.SearchException:没有文档id:com.mypackage.MyEntity 

但是,如果您正在使用批注并使用 @Id 注释主键,则不必使用 @DocumentId



更确切地说,Hibernate Search文档指出,当使用 @DocumentId 是可选的> JPA 注释。所以如果你使用Hibernate 3.x风格的注释,也许你仍然需要使用 @DocumentId ...我从来没有测试过这个。无论哪种方式,Hibernate 4.x都弃用自己的映射注释,以支持JPA样式注释,即使 >你正在使用Hibernate的 Session 而不是JPA的 EntityManager 来查询。所以简而言之:如果您使用的是XML样式的映射,则需要使用 @DocumentId ;而如果您使用注释,则是可选的,因为此时您无论如何应该使用JPA风格的注释。


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!

解决方案

@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.

这篇关于Hibernate搜索需要@DocumentId吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 08:17