Search code examples
javaspringhibernate

An error is thrown because an entity field marked with the @Formula annotation is not contained in the resulting ResultSet


There is an entity that contains both regular and calculated fields via the @Formula annotation.

@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "persons")
@AuditTable(value = "persons_AUD", schema = "history")
@Audited
public class Person implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "name", nullable = false)
    private String name;

    @Column(name = "surname", nullable = false)
    private String surname;

    @Column(name = "middle_name")
    private String middleName;

    @Formula("CONCAT_WS( ' ', surname, name, middle_name ) ")
    @NotAudited
    private String fullName;
}

After migrating from Spring Boot 2 to Spring Boot 3, when executing a custom Spring Data request, the following error began to be thrown:

jakarta.servlet.ServletException: Request processing failed: org.springframework.dao.InvalidDataAccessResourceUsageException: Unable to find column position by name: fullName [Колонки fullName не найдено в этом ResultSet’’е.] [n/a]; SQL [n/a]

However, methods like findAll() do not cause such an error. Before migrating to Spring Boot 3, this error did not occur. The request method itself looks like this:

@Query(value = " SELECT distinct p.* FROM persons p", nativeQuery = true)
List<Person> findBySomeCustomRequest();

Solution

  • The error results from not using the field fullName in your native query. It's the same situation as in Unable to find column position by name: column1 [The column name column1 was not found in this ResultSet. Remove the column from the class or add it to the native query.