Spring Data Couchbase tutorial: multiple buckets and spatial queries

This commit is contained in:
Kevin Gilmore
2016-06-03 07:26:42 -05:00
parent 167187f732
commit 2e9efa16bc
28 changed files with 1423 additions and 3 deletions
@@ -0,0 +1,79 @@
package org.baeldung.spring.data.couchbase;
import java.util.Arrays;
import java.util.List;
import org.baeldung.spring.data.couchbase.model.Campus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
import org.springframework.data.couchbase.core.CouchbaseTemplate;
import org.springframework.data.couchbase.core.mapping.event.ValidatingCouchbaseEventListener;
import org.springframework.data.couchbase.core.query.Consistency;
import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories;
import org.springframework.data.couchbase.repository.config.RepositoryOperationsMapping;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import com.couchbase.client.java.Bucket;
@Configuration
@EnableCouchbaseRepositories(basePackages={"org.baeldung.spring.data.couchbase"})
public class MyCouchbaseConfig extends AbstractCouchbaseConfiguration {
public static final List<String> NODE_LIST = Arrays.asList("localhost");
public static final String DEFAULT_BUCKET_NAME = "baeldung";
public static final String DEFAULT_BUCKET_PASSWORD = "";
@Override
protected List<String> getBootstrapHosts() {
return NODE_LIST;
}
@Override
protected String getBucketName() {
return DEFAULT_BUCKET_NAME;
}
@Override
protected String getBucketPassword() {
return DEFAULT_BUCKET_PASSWORD;
}
@Bean
public Bucket campusBucket() throws Exception {
return couchbaseCluster().openBucket("baeldung2", "");
}
@Bean
public CouchbaseTemplate campusTemplate() throws Exception {
CouchbaseTemplate template = new CouchbaseTemplate(
couchbaseClusterInfo(), campusBucket(),
mappingCouchbaseConverter(), translationService());
template.setDefaultConsistency(getDefaultConsistency());
return template;
}
@Override
public void configureRepositoryOperationsMapping(RepositoryOperationsMapping baseMapping) {
try {
baseMapping.mapEntity(Campus.class, campusTemplate());
} catch (Exception e) {
//custom Exception handling
}
}
@Override
protected Consistency getDefaultConsistency() {
return Consistency.READ_YOUR_OWN_WRITES;
}
@Bean
public LocalValidatorFactoryBean localValidatorFactoryBean() {
return new LocalValidatorFactoryBean();
}
@Bean
public ValidatingCouchbaseEventListener validatingCouchbaseEventListener() {
return new ValidatingCouchbaseEventListener(localValidatorFactoryBean());
}
}
@@ -0,0 +1,102 @@
package org.baeldung.spring.data.couchbase.model;
import javax.validation.constraints.NotNull;
import org.springframework.data.annotation.Id;
import org.springframework.data.couchbase.core.mapping.Document;
import org.springframework.data.geo.Point;
import com.couchbase.client.java.repository.annotation.Field;
@Document
public class Campus {
@Id
private String id;
@Field
@NotNull
private String name;
@Field
@NotNull
private Point location;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Point getLocation() {
return location;
}
public void setLocation(Point location) {
this.location = location;
}
@Override
public int hashCode() {
int hash = 1;
if(id != null) {
hash = hash * 31 + id.hashCode();
}
if(name != null) {
hash = hash * 31 + name.hashCode();
}
if(location != null) {
hash = hash * 31 + location.hashCode();
}
return hash;
}
@Override
public boolean equals(Object obj) {
if((obj == null) || (obj.getClass() != this.getClass())) return false;
if(obj == this) return true;
Campus other = (Campus) obj;
return this.hashCode() == other.hashCode();
}
@SuppressWarnings("unused")
private Campus() {}
public Campus(Builder b) {
this.id = b.id;
this.name = b.name;
this.location = b.location;
}
public static class Builder {
private String id;
private String name;
private Point location;
public static Builder newInstance() {
return new Builder();
}
public Campus build() {
return new Campus(this);
}
public Builder id(String id) {
this.id = id;
return this;
}
public Builder name(String name) {
this.name = name;
return this;
}
public Builder location(Point location) {
this.location = location;
return this;
}
}
}
@@ -0,0 +1,87 @@
package org.baeldung.spring.data.couchbase.model;
import javax.validation.constraints.NotNull;
import org.joda.time.DateTime;
import org.springframework.data.annotation.Id;
import org.springframework.data.couchbase.core.mapping.Document;
import com.couchbase.client.java.repository.annotation.Field;
@Document
public class Person {
@Id
private String id;
@Field
@NotNull
private String firstName;
@Field
@NotNull
private String lastName;
@Field
@NotNull
private DateTime created;
@Field
private DateTime updated;
public Person(String id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public DateTime getCreated() {
return created;
}
public void setCreated(DateTime created) {
this.created = created;
}
public DateTime getUpdated() {
return updated;
}
public void setUpdated(DateTime updated) {
this.updated = updated;
}
@Override
public int hashCode() {
int hash = 1;
if(id != null) {
hash = hash * 31 + id.hashCode();
}
if(firstName != null) {
hash = hash * 31 + firstName.hashCode();
}
if(lastName != null) {
hash = hash * 31 + lastName.hashCode();
}
return hash;
}
@Override
public boolean equals(Object obj) {
if((obj == null) || (obj.getClass() != this.getClass())) return false;
if(obj == this) return true;
Person other = (Person) obj;
return this.hashCode() == other.hashCode();
}
}
@@ -0,0 +1,113 @@
package org.baeldung.spring.data.couchbase.model;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import org.joda.time.DateTime;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Version;
import org.springframework.data.couchbase.core.mapping.Document;
import com.couchbase.client.java.repository.annotation.Field;
@Document
public class Student {
private static final String NAME_REGEX = "^[a-zA-Z .'-]+$";
@Id
private String id;
@Field
@NotNull
@Size(min=1, max=20)
@Pattern(regexp=NAME_REGEX)
private String firstName;
@Field
@NotNull
@Size(min=1, max=20)
@Pattern(regexp=NAME_REGEX)
private String lastName;
@Field
@Past
private DateTime dateOfBirth;
@Field
@NotNull
private DateTime created;
@Field
private DateTime updated;
@Version
private long version;
public Student() {}
public Student(String id, String firstName, String lastName, DateTime dateOfBirth) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.dateOfBirth = dateOfBirth;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public DateTime getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(DateTime dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public DateTime getCreated() {
return created;
}
public void setCreated(DateTime created) {
this.created = created;
}
public DateTime getUpdated() {
return updated;
}
public void setUpdated(DateTime updated) {
this.updated = updated;
}
@Override
public int hashCode() {
int hash = 1;
if(id != null) {
hash = hash * 31 + id.hashCode();
}
if(firstName != null) {
hash = hash * 31 + firstName.hashCode();
}
if(lastName != null) {
hash = hash * 31 + lastName.hashCode();
}
if(dateOfBirth != null) {
hash = hash * 31 + dateOfBirth.hashCode();
}
return hash;
}
@Override
public boolean equals(Object obj) {
if((obj == null) || (obj.getClass() != this.getClass())) return false;
if(obj == this) return true;
Student other = (Student) obj;
return this.hashCode() == other.hashCode();
}
}
@@ -0,0 +1,19 @@
package org.baeldung.spring.data.couchbase.repos;
import java.util.Set;
import org.baeldung.spring.data.couchbase.model.Campus;
import org.springframework.data.couchbase.core.query.Dimensional;
import org.springframework.data.couchbase.core.query.View;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Point;
import org.springframework.data.repository.CrudRepository;
public interface CampusRepository extends CrudRepository<Campus, String> {
@View(designDocument="campus", viewName="byName")
Set<Campus> findByName(String name);
@Dimensional(dimensions=2, designDocument="campus_spatial", spatialViewName="byLocation")
Set<Campus> findByLocationNear(Point point, Distance distance);
}
@@ -0,0 +1,11 @@
package org.baeldung.spring.data.couchbase.repos;
import java.util.List;
import org.baeldung.spring.data.couchbase.model.Person;
import org.springframework.data.repository.CrudRepository;
public interface PersonRepository extends CrudRepository<Person, String> {
List<Person> findByFirstName(String firstName);
List<Person> findByLastName(String lastName);
}
@@ -0,0 +1,11 @@
package org.baeldung.spring.data.couchbase.repos;
import java.util.List;
import org.baeldung.spring.data.couchbase.model.Student;
import org.springframework.data.repository.CrudRepository;
public interface StudentRepository extends CrudRepository<Student, String> {
List<Student> findByFirstName(String firstName);
List<Student> findByLastName(String lastName);
}
@@ -0,0 +1,54 @@
package org.baeldung.spring.data.couchbase.service;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.baeldung.spring.data.couchbase.model.Campus;
import org.baeldung.spring.data.couchbase.repos.CampusRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Point;
import org.springframework.stereotype.Service;
@Service
@Qualifier("CampusRepositoryService")
public class CampusRepositoryService implements CampusService {
private CampusRepository repo;
@Autowired
public void setCampusRepository(CampusRepository repo) {
this.repo = repo;
}
@Override
public Campus find(String id) {
return repo.findOne(id);
}
@Override
public Set<Campus> findByName(String name) {
return repo.findByName(name);
}
@Override
public Set<Campus> findByLocationNear(Point point, Distance distance) {
return repo.findByLocationNear(point, distance);
}
@Override
public Set<Campus> findAll() {
Set<Campus> campuses = new HashSet<>();
Iterator<Campus> it = repo.findAll().iterator();
while(it.hasNext()) {
campuses.add(it.next());
}
return campuses;
}
@Override
public void save(Campus campus) {
repo.save(campus);
}
}
@@ -0,0 +1,20 @@
package org.baeldung.spring.data.couchbase.service;
import java.util.Set;
import org.baeldung.spring.data.couchbase.model.Campus;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Point;
public interface CampusService {
Campus find(String id);
Set<Campus> findByName(String name);
Set<Campus> findByLocationNear(Point point, Distance distance);
Set<Campus> findAll();
void save(Campus campus);
}
@@ -0,0 +1,58 @@
package org.baeldung.spring.data.couchbase.service;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.baeldung.spring.data.couchbase.model.Person;
import org.baeldung.spring.data.couchbase.repos.PersonRepository;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service
@Qualifier("PersonRepositoryService")
public class PersonRepositoryService implements PersonService {
private PersonRepository repo;
@Autowired
public void setPersonRepository(PersonRepository repo) {
this.repo = repo;
}
public Person findOne(String id) {
return repo.findOne(id);
}
public List<Person> findAll() {
List<Person> people = new ArrayList<Person>();
Iterator<Person> it = repo.findAll().iterator();
while(it.hasNext()) {
people.add(it.next());
}
return people;
}
public List<Person> findByFirstName(String firstName) {
return repo.findByFirstName(firstName);
}
public List<Person> findByLastName(String lastName) {
return repo.findByLastName(lastName);
}
public void create(Person person) {
person.setCreated(DateTime.now());
repo.save(person);
}
public void update(Person person) {
person.setUpdated(DateTime.now());
repo.save(person);
}
public void delete(Person person) {
repo.delete(person);
}
}
@@ -0,0 +1,22 @@
package org.baeldung.spring.data.couchbase.service;
import java.util.List;
import org.baeldung.spring.data.couchbase.model.Person;
public interface PersonService {
Person findOne(String id);
List<Person> findAll();
List<Person> findByFirstName(String firstName);
List<Person> findByLastName(String lastName);
void create(Person person);
void update(Person person);
void delete(Person person);
}
@@ -0,0 +1,58 @@
package org.baeldung.spring.data.couchbase.service;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.baeldung.spring.data.couchbase.model.Student;
import org.baeldung.spring.data.couchbase.repos.StudentRepository;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service
@Qualifier("StudentRepositoryService")
public class StudentRepositoryService implements StudentService {
private StudentRepository repo;
@Autowired
public void setStudentRepository(StudentRepository repo) {
this.repo = repo;
}
public Student findOne(String id) {
return repo.findOne(id);
}
public List<Student> findAll() {
List<Student> people = new ArrayList<Student>();
Iterator<Student> it = repo.findAll().iterator();
while(it.hasNext()) {
people.add(it.next());
}
return people;
}
public List<Student> findByFirstName(String firstName) {
return repo.findByFirstName(firstName);
}
public List<Student> findByLastName(String lastName) {
return repo.findByLastName(lastName);
}
public void create(Student student) {
student.setCreated(DateTime.now());
repo.save(student);
}
public void update(Student student) {
student.setUpdated(DateTime.now());
repo.save(student);
}
public void delete(Student student) {
repo.delete(student);
}
}
@@ -0,0 +1,22 @@
package org.baeldung.spring.data.couchbase.service;
import java.util.List;
import org.baeldung.spring.data.couchbase.model.Student;
public interface StudentService {
Student findOne(String id);
List<Student> findAll();
List<Student> findByFirstName(String firstName);
List<Student> findByLastName(String lastName);
void create(Student student);
void update(Student student);
void delete(Student student);
}
@@ -0,0 +1,17 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>web - %date [%thread] %-5level %logger{36} - %message%n
</pattern>
</encoder>
</appender>
<logger name="org.springframework" level="WARN" />
<logger name="org.baeldung" level="DEBUG" />
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>