Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is @DocumentId required for Hibernate Search?

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!

like image 826
jackcrews Avatar asked Sep 11 '12 20:09

jackcrews


1 Answers

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

like image 70
Steve Perkins Avatar answered Jan 04 '23 00:01

Steve Perkins