I have an Entity
@Entity
@Table(name = "EVENT")
public class Event {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Long id;
@Column(name = "EVENT", columnDefinition="VARCHAR(100)", nullable = false)
private String event;
@Column(name = "DATE", columnDefinition="DATETIME", nullable = false)
@Convert(converter = InstantConverter.class)
private Instant date;
}
And a SearchParams
class where all of the fields are optional
public class SearchParams {
private Long id;
private Instant startDate;
private Instant endDate;
private String event;
public Optional<Long> getID() {
return Optional.ofNullable(id);
}
// ... Similar for other fields ...
}
I would like to be able to search like:
public List<Event> search(SearchParams searchParams) {
// Do something with Entity Manager/another hibernate class to create a custom query based off the search params.
return events;
}
What's the easiest way to achieve this in JPA? Or is this not the right framework to use?
I could always resort to sql
string building but its not a nice way of doing it.