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!
@DocumentIdis required if you are using the old-school style of mapping your entities with.hbm.xmlfiles. If you are use that mapping approach and neglect to annotate a document id, then at startup you will see an exception like this: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
@DocumentIdis optional when using JPA annotations. So perhaps you would still need to use@DocumentIdif 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
Sessionrather than JPA’sEntityManagerfor your queries. So in a nutshell: you need to use@DocumentIdif 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.