我想把分类和 Pageable
关于使用 @Query
来自Spring Data 的注解。
第一个接口的方法(没有 @Query
但是有了 Pageable
)很有魅力。就像我只招一个员工 @Query
但不是 Pageable
我在用 Optional<Employee>
有(第三种方法)。但是当我试着把这两个放在一起的时候,乐趣就开始了——它不再起作用了。
当我试图按 name
它尖叫着说这个错误:
Caused by: org.hibernate.QueryException: could not resolve property: name of: (....).model.employee.Employee
所以问题是:如何告诉spring去寻找 name
在关联字段中?如何处理spring数据?
我已经试过几种方法,但都不起作用,或者我仍然不知道如何正确使用它们:
有人建议添加 countQuery
到 @Query
参数,因此这在某种程度上与分页(spring data jpa@query和pageable)相对应
我遵循了baeldung的教程,但这不包括连接
带分页的spring数据获取连接不起作用也建议使用 countQuery
但我更愿意坚持 Page<Employee>
而不是 List<Employee>
.
我将在下面留下一些代码示例。如果我遗漏了一些重要的事情,请随时要求更新。
// Employee
@Entity
@Table(name = "employee", schema = "emp")
@Data
@NoArgsConstructor
public class Employee {
private static final String SEQUENCE = "EMPLOYEE_SEQUENCE";
@Id
@SequenceGenerator(sequenceName = SEQUENCE, name = SEQUENCE, allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = SEQUENCE)
private Long id;
@Column(name = "employee_number")
private String employeeNumber;
@Column
@Enumerated(EnumType.STRING)
private EmployeeStatus status;
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
@JoinColumn(name = "id_details")
private Details details;
// some other fields ...
}
// Details
@Entity
@Table(name = "details", schema = "emp")
@Data
@NoArgsConstructor
public class Details {
private static final String SEQUENCE = "DETAILS_SEQUENCE";
@Id
@SequenceGenerator(sequenceName = SEQUENCE, name = SEQUENCE, allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = SEQUENCE)
private Long id;
private String name;
private String surname;
// some other fields ...
}
// EmployeeDTO
@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder(toBuilder = true)
public class EmployeeDTO {
private Long id;
private String employeeNumber;
private String status;
private String name;
private String surname;
// some other fields ...
}
// EmployeeRepository
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
// 1st method
Page<Employee> findByStatus(EmployeeStatus status, Pageable pageable);
// 2nd method
@Query(value = "select e from Employee e join e.details where e.status = :status",
countQuery = "select count(*) from Employee e join e.details where e.status = :status")
Page<Employee> getEmployeeDetails(@Param("status") EmployeeStatus status, Pageable pageable);
// 3rd method
@Query("select e from Employee e join fetch e.details where e.id = :id")
Optional<Employee> findByIdWithDetails(Long id);
// ...
}
// EmployeeService
@Service
public class EmployeeService {
private final EmployeeRepository employeeRepository;
private final EntityDtoConverter entityDtoConverter;
@Autowired
public EmployeeService(EmployeeRepository employeeRepository, EntityDtoConverter entityDtoConverter) {
this.employeeRepository = employeeRepository;
this.entityDtoConverter = entityDtoConverter;
}
public EmployeeResponse getEmployeesByStatus(EmployeeStatus status, int pageSize, int pageIndex, Sort.Direction sortDirection, String sortColumn) {
Page<EmployeeDTO> employeePage = employeeRepository.findByStatus(status, PageRequest.of(pageIndex, pageSize, Sort.by(sortDirection, sortColumn)))
.map(entityDtoConverter::convertEmployeeBaseToDto);
return new EmployeeResponse(employeePage);
}
public EmployeeResponse getEmployeeDetails(EmployeeStatus status, int pageSize, int pageIndex, Sort.Direction sortDirection, String sortColumn) {
Page<EmployeeDTO> employeePage = employeeRepository.getEmployeeDetails(status, PageRequest.of(pageIndex, pageSize, Sort.by(sortDirection, sortColumn)))
.map(entityDtoConverter::convertToEmployeeWithDetailsDto);
return new EmployeeResponse(employeePage);
}
// ...
}
// EntityDtoConverter
@Component
public class EntityDtoConverter {
public EmployeeDTO convertEmployeeBaseToDto(Employee entity) {
return EmployeeDTO.builder()
.id(entity.getId())
.employeeNumber(entity.getEmployeeNumber())
.status(entity.getStatus())
.build();
}
public EmployeeDTO convertToEmployeeWithDetailsDto(Employee entity) {
return convertEmployeeBaseToDto(entity).toBuilder()
.name(entity.getDetails().getName())
.surname(entity.getDetails().getSurname())
.build();
}
// ...
}
编辑:
这是我的rest控制器的方法之一:
@GetMapping
public ResponseEntity<EmployeeResponse> getEmployeesByStatus(EmployeeStatus status, int pageSize, int pageIndex, String sortDirection, String sortColumn) {
try {
EmployeeResponse employeeResponse = employeeService.getEmployeesByStatus(status, pageSize, pageIndex, Sort.Direction.fromString(sortDirection), sortColumn);
return employeeResponse.getTotalElements().equals(0L) ? ResponseEntity.noContent().build() : ResponseEntity.ok(employeeResponse);
} catch (Exception e) {
log.error(ERROR_MESSAGE, e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
1条答案
按热度按时间ifmq2ha21#
试试下面的代码。
这里我们试图通知jpa这个名称是employee表的外键。