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;
}
}