Created
April 15, 2018 19:24
-
-
Save lankydan/e24225d3ca936ce8fb5848eddf9efb1c to your computer and use it in GitHub Desktop.
Datastax driver - person repository
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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