Is @DocumentId required for Hibernate Search?
Asked Answered
E

1

6

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!

Elah answered 11/9, 2012 at 20:57 Comment(0)
F
6

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

Foreworn answered 2/10, 2012 at 20:52 Comment(1)
I thought this was the case but I couldn't find it in the docs. Thanks!Elah

© 2022 - 2024 — McMap. All rights reserved.