Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Repository
- public class PersonRepository extends SimpleCassandraRepository<Person, PersonKey> {
- private final Session session;
- private final CassandraOperations cassandraTemplate;
- private final PreparedStatementCache cache = PreparedStatementCache.create();
- public PersonRepository(
- Session session,
- CassandraEntityInformation entityInformation,
- CassandraOperations cassandraTemplate) {
- super(entityInformation, cassandraTemplate);
- this.session = session;
- this.cassandraTemplate = cassandraTemplate;
- }
- // using ORM
- public List<Person> findByFirstNameAndDateOfBirth(String firstName, LocalDate dateOfBirth) {
- return cassandraTemplate
- .getCqlOperations()
- .query(
- findByFirstNameAndDateOfBirthQuery(firstName, dateOfBirth),
- (row, rowNum) -> cassandraTemplate.getConverter().read(Person.class, row));
- }
- private BoundStatement findByFirstNameAndDateOfBirthQuery(
- String firstName, LocalDate dateOfBirth) {
- return CachedPreparedStatementCreator.of(
- cache,
- select()
- .all()
- .from("people_by_first_name")
- .where(eq("first_name", bindMarker("first_name")))
- .and(eq("date_of_birth", bindMarker("date_of_birth"))))
- .createPreparedStatement(session)
- .bind()
- .setString("first_name", firstName)
- .setDate("date_of_birth", toCqlDate(dateOfBirth));
- }
- private com.datastax.driver.core.LocalDate toCqlDate(LocalDate date) {
- return com.datastax.driver.core.LocalDate.fromYearMonthDay(
- date.getYear(), date.getMonth().getValue(), date.getDayOfMonth());
- }
- // without ORM
- public List<Person> findByFirstNameAndDateOfBirthWithoutORM(
- String firstName, LocalDate dateOfBirth) {
- return cassandraTemplate
- .getCqlOperations()
- .query(
- findByFirstNameAndDateOfBirthQuery(firstName, dateOfBirth),
- (row, rowNum) -> convert(row));
- }
- private Person convert(Row row) {
- return new Person(
- new PersonKey(
- row.getString("first_name"),
- toLocalDate(row.getDate("date_of_birth")),
- row.getUUID("person_id")),
- row.getString("last_name"),
- row.getDouble("salary"));
- }
- private LocalDate toLocalDate(com.datastax.driver.core.LocalDate date) {
- return LocalDate.of(date.getYear(), date.getMonth(), date.getDay());
- }
- }
Add Comment
Please, Sign In to add comment