Home > Blockchain >  Try to sort records after joining table using PageRequest Hibernate JPA
Try to sort records after joining table using PageRequest Hibernate JPA

Time:05-05

need help I have data table that display joining table records then there is option to sort by clicking a column. In this case I want to sort joined column by using PageRequest.

I want to sort / order by roleIdentifier that joined to employee table.

right now the way I call the repository like this this.employeeRepository.findAll(pageRequest);

where the pageRequest parameters for sort I set with "role" just like inside the employee entity. But then sorting doesn't work properly.

Did I miss something ?

I have Employee Entity like this

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

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


  @OneToOne(fetch = FetchType.EAGER)
  @JoinColumn(name = "identifier",  referencedColumnName = "identifier", insertable = false, updatable = false)
  private UserRoleEntity role;

 

and I have user Role Entity like this

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

  @Column(name = "identifier")
  private String identifier;
  @Column(name = "roleIdentifier")
  private String role;

thanks in advace

CodePudding user response:

use primary key for relation, or is idenitifier unique?

@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "roleId", insertable = false, updatable = false)
private UserRoleEntity role;

and then try

employeeRepo.findAll(PageRequest.of(0,10, Sort.by(Sort.Direction.DESC, "role.identifier")));
  • Related