Skip to content

Hibernate Search 6: Methods mapping

In Hibernate Search 5.x I can map entity method as the fulltext field like this:

public class Person {

    private Long id;

    private String name;

    private String surname;

    public String getWholeName() {
        return name + " " + surname;

    // getters, setters

// Mapping configuration, programmatic approach
SearchMapping sm = new SearchMapping();
  .property("wholeName", ElementType.METHOD)

Then I have a field with name “wholeName” in my fulltext index and it contains return value of getWholeName() method.

How to do it in Hibernate Search 6? I found only a way how to map an entity field but not a method. Thank you!


Short answer

If there is no field named wholeName, Hibernate Search 6 will automatically fall back to the getter. The ElementType from Hibernate Search 5 is no longer necessary, and that’s why it was removed.

Note that Hibernate Search is also smarter when it comes to detecting changes in entities. That’s usually great, but the downside is that you’ll need to tell Hibernate Search what other attributes wholeName relies on. See this section of the documentation (you can also find an example using the programmatic mapping API here).

Long answer

When an attribute has a field but no getter, or a getter but no field, there is no ambiguity. Hibernate Search uses the only available access type.

When an attribute has both a field and a getter, there is a choice to be made. Hibernate Search 6 chooses to comply with Hibernate ORM’s access type.

Hibernate ORM accesses attributes either by direct access to the field (“field” access type) or through getters/setters (“property” access type).

By default, the access type in Hibernate ORM is determined by where your @Id annotation is. In this case, the @Id annotation is located on a field, not a method, so Hibernate ORM will use the “field” access type. And so will Hibernate Search.

You can also set the Hibernate ORM access type explicitly using the @Access annotation, either for the whole entity (put the annotation on the class) or for a particular property (put the annotation on the field). Hibernate Search will comply with this too.