Sorting with lazy data provider doesen't work with @OneToOne relations

I have a code similar to the one below, I have one main class “ExampleEntity” which has several @OneToOne relations. In grid, I use the individual fields from these relationships. I have written lazy data binding using callbacks, which I added in the code below. The sorting works with the “setSortProperty” parameter for the fields that are directly in the ExampleEntity class, and for the @OneToOne relationships that are direct (also shown in the code). However, when I want to sort the fields in which I use relations, I get an error:

org.hibernate.query.qsm.PathElementException: Could not resolve attribute 'forgein.name' of 'packageName.entity.ExampleEntity'

Code example below:

//ExampleEntity.java

@Entity
@Table(name = "example")
@Immutable
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class ExampleEntity{

    @id
    @Column(name = "id", unique = true)
    private long id;

    // More columns

    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "foreign_id")
    private ForeignEntity foreign;

}

//ForeignEntity.java
@Entity
@Table(name = "foreign")
@Immutable
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class ForeignEntity{

    @id
    @Column(name = "id", unique = true)
    private long id;

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

    // More columns

    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "another_foreign_id")
    private AnotherForeignEntity anotherForeign;

}


//ExampleView.java


@Route("")
public class ExampleView extends VerticalLayout{
    private final ExampleRepository exampleRepository;

    public ExampleView(ExampleRepository exampleRepository){
        this.exampleRepository = exampleRepository;
        Div div = new Div();
        div.add(myGrid());
        
        add(div);
    }

    private Component myGrid(){
        var grid = new Grid<>(ExampleDTO.class);
        grid.addColumn(ExampleDTO::getId, "id").setKey("id"); // sorting works
        grid.addColumn("foreignName").setSortProperty("forgein.name").setKey("foreingName"); // sorting doesent work
        grid.addColumn("foreignId").setSortProperty("forgein").setKey("foreingId"); // sorting works
        //more columns 
    

        grid.setItems(query -> {
            final var vaadinSortOrders = query.getSortOrders();
            final var springSortOrders = new ArrayList<Sort.Order>();
            for (final QuerySortOrder so : vaadinSortOrders) {
                final Spring colKey = so.getSorted();
                if (so.getDirection() == SortDirection.ASCENDING) {
                    springSortOrders.add(Sort.Order.asc(colKey));
                }
                else if (so.getDirection() = =SortDirection.DESCENDING) {
                    springSortOrders.add(Sort.Order.desc(colKey));
                }
            }
            return this.exampleRepositoryImpl
                .findAllExamples(this.exampleSpecification.getExampleSpecification(exampleRequest), PageRequest.of(query.getPage(), query.getPageSize(), Sort.by(springSortOrders)))
                .stream()
                .map(this.exampleMapper::map);
        })



        return grid;

    }
}

It looks like you have a typo here: forgein instead of foreign

I was writing this example code in the notepad. in my application, I don’t have any typos :slight_smile:

Shouldn’t it be grid.addColumn("foreign.name")... instead? or is there a property called ‘foreignName’ in your ExampleEntity class?

PS: the amount of different spellings is comical (foreign, forgein, foreing). I have some words too that my fingers will always write wrong. I’d make 200% sure that you don’t have any typos in the actual code :)

That’s correct, but these fields are mapped to my ExampleDTO class, with has those fields.

I noticed the typo in the exception message as well. Is it also a copy/paste error?

Yes, that’s also a typo.

The actual code is in fact correct, i’ve double checked that.

1 Like

Have you tried to test your repository? Your problem is not Vaadin related.

You were right, the problem was actually in my repository.