I'm new to JPA. I have a class like this
@Table(name="student")
@Entity
public class Student{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int studentId;
@Column
@Size(max = 20)
private String name;
@Column
@Min(value = 2014)
@Max(value = 2020)
private int yearOfBirth;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "classroomId")
Classroom classroom;
//Getters and setters
}
My repository:
@Repository
public interface HocSinhRepository extends JpaRepository<Student, Integer> {}
My controller:
public class StudentController {
@Autowired
StudentRepository studentRepository;
@GetMapping(value = "/get")
public Page<Student> get(@RequestParam Optional<Integer> page, @RequestParam Optional<String> sortBy) {
return studentRepository.findAll(
PageRequest.of(page.orElse(0), 3, Sort.Direction.ASC, sortBy.orElse("name"))
);
}
}
By using Optional.orElse, I can assign a default value to the sortBy parameter if it's null. How can I do the same thing if the parameter is not null, but just a non-sensible string (like "asdasd")?