Skip to content

Commit 27c1ed3

Browse files
Merge remote-tracking branch 'origin/master'
2 parents 193adf8 + abb5490 commit 27c1ed3

23 files changed

Lines changed: 696 additions & 161 deletions

File tree

‎spring-data-couchbase-2/.classpath‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<classpathentry kind="src" path="src/main/java"/>
55
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
66
<classpathentry kind="src" path="src/test/resources"/>
7-
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
7+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
88
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/>
99
<classpathentry kind="output" path="target/classes"/>
1010
</classpath>

‎spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/MyCouchbaseConfig.java‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
import java.util.Arrays;
44
import java.util.List;
55

6+
import org.springframework.context.annotation.Bean;
67
import org.springframework.context.annotation.Configuration;
78
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
9+
import org.springframework.data.couchbase.core.mapping.event.ValidatingCouchbaseEventListener;
10+
import org.springframework.data.couchbase.core.query.Consistency;
811
import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories;
12+
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
913

1014
@Configuration
1115
@EnableCouchbaseRepositories(basePackages={"org.baeldung.spring.data.couchbase"})
@@ -29,4 +33,19 @@ protected String getBucketName() {
2933
protected String getBucketPassword() {
3034
return BUCKET_PASSWORD;
3135
}
36+
37+
@Override
38+
protected Consistency getDefaultConsistency() {
39+
return Consistency.READ_YOUR_OWN_WRITES;
40+
}
41+
42+
@Bean
43+
public LocalValidatorFactoryBean localValidatorFactoryBean() {
44+
return new LocalValidatorFactoryBean();
45+
}
46+
47+
@Bean
48+
public ValidatingCouchbaseEventListener validatingCouchbaseEventListener() {
49+
return new ValidatingCouchbaseEventListener(localValidatorFactoryBean());
50+
}
3251
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package org.baeldung.spring.data.couchbase.model;
2+
3+
import javax.validation.constraints.NotNull;
4+
import javax.validation.constraints.Past;
5+
import javax.validation.constraints.Pattern;
6+
import javax.validation.constraints.Size;
7+
8+
import org.joda.time.DateTime;
9+
import org.springframework.data.annotation.Id;
10+
import org.springframework.data.annotation.Version;
11+
import org.springframework.data.couchbase.core.mapping.Document;
12+
13+
import com.couchbase.client.java.repository.annotation.Field;
14+
15+
@Document
16+
public class Student {
17+
private static final String NAME_REGEX = "^[a-zA-Z .'-]+$";
18+
19+
@Id
20+
private String id;
21+
@Field
22+
@NotNull
23+
@Size(min=1, max=20)
24+
@Pattern(regexp=NAME_REGEX)
25+
private String firstName;
26+
@Field
27+
@NotNull
28+
@Size(min=1, max=20)
29+
@Pattern(regexp=NAME_REGEX)
30+
private String lastName;
31+
@Field
32+
@Past
33+
private DateTime dateOfBirth;
34+
@Field
35+
@NotNull
36+
private DateTime created;
37+
@Field
38+
private DateTime updated;
39+
@Version
40+
private long version;
41+
42+
public Student() {}
43+
44+
public Student(String id, String firstName, String lastName, DateTime dateOfBirth) {
45+
this.id = id;
46+
this.firstName = firstName;
47+
this.lastName = lastName;
48+
this.dateOfBirth = dateOfBirth;
49+
}
50+
51+
public String getId() {
52+
return id;
53+
}
54+
public void setId(String id) {
55+
this.id = id;
56+
}
57+
public String getFirstName() {
58+
return firstName;
59+
}
60+
public void setFirstName(String firstName) {
61+
this.firstName = firstName;
62+
}
63+
public String getLastName() {
64+
return lastName;
65+
}
66+
public void setLastName(String lastName) {
67+
this.lastName = lastName;
68+
}
69+
public DateTime getDateOfBirth() {
70+
return dateOfBirth;
71+
}
72+
public void setDateOfBirth(DateTime dateOfBirth) {
73+
this.dateOfBirth = dateOfBirth;
74+
}
75+
public DateTime getCreated() {
76+
return created;
77+
}
78+
public void setCreated(DateTime created) {
79+
this.created = created;
80+
}
81+
public DateTime getUpdated() {
82+
return updated;
83+
}
84+
public void setUpdated(DateTime updated) {
85+
this.updated = updated;
86+
}
87+
88+
@Override
89+
public int hashCode() {
90+
int hash = 1;
91+
if(id != null) {
92+
hash = hash * 31 + id.hashCode();
93+
}
94+
if(firstName != null) {
95+
hash = hash * 31 + firstName.hashCode();
96+
}
97+
if(lastName != null) {
98+
hash = hash * 31 + lastName.hashCode();
99+
}
100+
if(dateOfBirth != null) {
101+
hash = hash * 31 + dateOfBirth.hashCode();
102+
}
103+
return hash;
104+
}
105+
106+
@Override
107+
public boolean equals(Object obj) {
108+
if((obj == null) || (obj.getClass() != this.getClass())) return false;
109+
if(obj == this) return true;
110+
Student other = (Student) obj;
111+
return this.hashCode() == other.hashCode();
112+
}
113+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.baeldung.spring.data.couchbase.repos;
2+
3+
import java.util.List;
4+
5+
import org.baeldung.spring.data.couchbase.model.Student;
6+
7+
public interface CustomStudentRepository {
8+
List<Student> findByFirstNameStartsWith(String s);
9+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.baeldung.spring.data.couchbase.repos;
2+
3+
import java.util.List;
4+
5+
import org.baeldung.spring.data.couchbase.model.Student;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.data.couchbase.core.CouchbaseTemplate;
8+
9+
import com.couchbase.client.java.view.Stale;
10+
import com.couchbase.client.java.view.ViewQuery;
11+
12+
public class CustomStudentRepositoryImpl implements CustomStudentRepository {
13+
14+
private static final String DESIGN_DOC = "student";
15+
16+
@Autowired
17+
private CouchbaseTemplate template;
18+
19+
public List<Student> findByFirstNameStartsWith(String s) {
20+
return template.findByView(ViewQuery.from(DESIGN_DOC, "byFirstName")
21+
.startKey(s)
22+
.stale(Stale.FALSE),
23+
Student.class);
24+
}
25+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.baeldung.spring.data.couchbase.repos;
2+
3+
import java.util.List;
4+
5+
import org.baeldung.spring.data.couchbase.model.Student;
6+
import org.springframework.data.repository.CrudRepository;
7+
8+
public interface StudentRepository extends CrudRepository<Student, String>, CustomStudentRepository {
9+
List<Student> findByFirstName(String firstName);
10+
List<Student> findByLastName(String lastName);
11+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package org.baeldung.spring.data.couchbase.service;
2+
3+
import java.util.ArrayList;
4+
import java.util.Iterator;
5+
import java.util.List;
6+
7+
import org.baeldung.spring.data.couchbase.model.Student;
8+
import org.baeldung.spring.data.couchbase.repos.StudentRepository;
9+
import org.joda.time.DateTime;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.beans.factory.annotation.Qualifier;
12+
import org.springframework.stereotype.Service;
13+
14+
@Service
15+
@Qualifier("StudentRepositoryService")
16+
public class StudentRepositoryService implements StudentService {
17+
18+
private StudentRepository repo;
19+
@Autowired
20+
public void setStudentRepository(StudentRepository repo) {
21+
this.repo = repo;
22+
}
23+
24+
public Student findOne(String id) {
25+
return repo.findOne(id);
26+
}
27+
28+
public List<Student> findAll() {
29+
List<Student> people = new ArrayList<Student>();
30+
Iterator<Student> it = repo.findAll().iterator();
31+
while(it.hasNext()) {
32+
people.add(it.next());
33+
}
34+
return people;
35+
}
36+
37+
public List<Student> findByFirstName(String firstName) {
38+
return repo.findByFirstName(firstName);
39+
}
40+
41+
public List<Student> findByLastName(String lastName) {
42+
return repo.findByLastName(lastName);
43+
}
44+
45+
public void create(Student student) {
46+
student.setCreated(DateTime.now());
47+
repo.save(student);
48+
}
49+
50+
public void update(Student student) {
51+
student.setUpdated(DateTime.now());
52+
repo.save(student);
53+
}
54+
55+
public void delete(Student student) {
56+
repo.delete(student);
57+
}
58+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.baeldung.spring.data.couchbase.service;
2+
3+
import java.util.List;
4+
5+
import org.baeldung.spring.data.couchbase.model.Student;
6+
7+
public interface StudentService {
8+
9+
Student findOne(String id);
10+
11+
List<Student> findAll();
12+
13+
List<Student> findByFirstName(String firstName);
14+
15+
List<Student> findByLastName(String lastName);
16+
17+
void create(Student student);
18+
19+
void update(Student student);
20+
21+
void delete(Student student);
22+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.baeldung.spring.data.couchbase.service;
2+
3+
import java.util.List;
4+
5+
import org.baeldung.spring.data.couchbase.model.Student;
6+
import org.joda.time.DateTime;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.beans.factory.annotation.Qualifier;
9+
import org.springframework.data.couchbase.core.CouchbaseTemplate;
10+
import org.springframework.stereotype.Service;
11+
12+
import com.couchbase.client.java.view.ViewQuery;
13+
14+
@Service
15+
@Qualifier("StudentTemplateService")
16+
public class StudentTemplateService implements StudentService {
17+
18+
private static final String DESIGN_DOC = "student";
19+
20+
private CouchbaseTemplate template;
21+
@Autowired
22+
public void setCouchbaseTemplate(CouchbaseTemplate template) {
23+
this.template = template;
24+
}
25+
26+
public Student findOne(String id) {
27+
return template.findById(id, Student.class);
28+
}
29+
30+
public List<Student> findAll() {
31+
return template.findByView(ViewQuery.from(DESIGN_DOC, "all"), Student.class);
32+
}
33+
34+
public List<Student> findByFirstName(String firstName) {
35+
return template.findByView(ViewQuery.from(DESIGN_DOC, "byFirstName"), Student.class);
36+
}
37+
38+
public List<Student> findByLastName(String lastName) {
39+
return template.findByView(ViewQuery.from(DESIGN_DOC, "byLastName"), Student.class);
40+
}
41+
42+
public void create(Student student) {
43+
student.setCreated(DateTime.now());
44+
template.insert(student);
45+
}
46+
47+
public void update(Student student) {
48+
student.setUpdated(DateTime.now());
49+
template.update(student);
50+
}
51+
52+
public void delete(Student student) {
53+
template.remove(student);
54+
}
55+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
package org.baeldung.spring.data.couchbase;
22

33
import org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter;
4+
import org.springframework.data.couchbase.core.query.Consistency;
45

56
public class TestCouchbaseConfig extends MyCouchbaseConfig {
67

78
@Override
89
public String typeKey() {
910
return MappingCouchbaseConverter.TYPEKEY_SYNCGATEWAY_COMPATIBLE;
1011
}
12+
13+
@Override
14+
public Consistency getDefaultConsistency() {
15+
return Consistency.READ_YOUR_OWN_WRITES;
16+
}
1117
}

0 commit comments

Comments
 (0)