Skip to content

Instantly share code, notes, and snippets.

@lankydan
Created April 15, 2018 19:24
Show Gist options
  • Save lankydan/e24225d3ca936ce8fb5848eddf9efb1c to your computer and use it in GitHub Desktop.
Save lankydan/e24225d3ca936ce8fb5848eddf9efb1c to your computer and use it in GitHub Desktop.
Datastax driver - person repository
@Repository
public class PersonRepository {
private Mapper<Person> mapper;
private Session session;
private static final String TABLE = "people_by_country";
public PersonRepository(MappingManager mappingManager) {
createTable(mappingManager.getSession());
this.mapper = mappingManager.mapper(Person.class);
this.session = mappingManager.getSession();
}
private void createTable(Session session) {
// use SchemaBuilder to create table
}
public Person find(String country, String firstName, String secondName, UUID id) {
return mapper.get(country, firstName, secondName, id);
}
public List<Person> findAll() {
final ResultSet result = session.execute(select().all().from(TABLE));
return mapper.map(result).all();
}
public List<Person> findAllByCountry(String country) {
final ResultSet result = session.execute(select().all().from(TABLE).where(eq("country", country)));
return mapper.map(result).all();
}
public void delete(String country, String firstName, String secondName, UUID id) {
mapper.delete(country, firstName, secondName, id);
}
public Person save(Person person) {
mapper.save(person);
return person;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment