I am asking question about @Transient.
When a getter method is annotated @Transient it means for JPA not to take this returned value as a database value (still prefer the field value for this). But I don’t see what does it means for hibernate search, can you explain ? It is necessary for mass indexer if all AccesType are defined to FIELD (the default). Thanks.
For Hibernate Search, if a property is annotated with @Transient
, it means Hibernate Search won’t receive any information about this property being modified, as it usually does for persisted properties. So it won’t be able to tell when it changed, and thus won’t be able to reindex the entity automatically when the @Transient
property changes.
That’s why Hibernate Search expects more information when a @Transient
property is indexed:
- If the transient property is simply derived from persisted properties, you need to tell Hibernate Search which properties it’s derived from. Then Hibernate Search will know that if those persisted properties change, the
@Transient
property might have changed as well. - If the
@Transient
property never changes, or if you simply don’t care about reindexing the entity when the@Transient
property changes, you need to tell Hibernate Search to not care about changes to that property.
Another consequence of using @Transient
, though it’s more of a side effect, is that Hibernate ORM won’t have any metadata about how to access the property (field or getter). So in that case, the JPA AccessType will not matter, and Hibernate Search will just call the getter if it finds one, or access the field directly otherwise.
@Transient
is only relevant to the mass indexer when it comes to the access type. As explained above, the JPA AccessType does not mean anything for non-persisted properties, so Hibernate Search will just call the getter if it finds one, or access the field directly otherwise.
As to @Transient
being “necessary”… Whether @Transient
is necessary is completely independent from Hibernate Search. You must ask yourself: is this getter/field persisted in one or more database columns. If it is, then don’t use @Transient
. If it is not, then use @Transient
. Then you can deal with the consequences in Hibernate Search.
So, I will do taking into account only JPA rules. I found a nice answer here :