JAVA-20166 Migrating spring-date-eclipselink (#13959)

* JAVA-20166 Migrating spring-date-eclipselink

* JAVA-20166 Fix failed tests address already bind

* JAVA-20166 Migrating spring-date-couchbase-2

* JAVA-20166 Replace with optional instead of returning null

---------

Co-authored-by: timis1 <noreplay@yahoo.com>
This commit is contained in:
timis1
2023-05-19 09:53:28 +03:00
committed by GitHub
parent d9182f160c
commit 09e421c3cd
40 changed files with 266 additions and 220 deletions
@@ -1,13 +1,12 @@
package com.baeldung.spring.data.couchbase.model;
import javax.validation.constraints.NotNull;
import jakarta.validation.constraints.NotNull;
import org.springframework.data.annotation.Id;
import org.springframework.data.couchbase.core.mapping.Document;
import org.springframework.data.couchbase.core.mapping.Field;
import org.springframework.data.geo.Point;
import com.couchbase.client.java.repository.annotation.Field;
@Document
public class Campus {
@@ -1,12 +1,11 @@
package com.baeldung.spring.data.couchbase.model;
import javax.validation.constraints.NotNull;
import jakarta.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;
import org.springframework.data.couchbase.core.mapping.Field;
@Document
public class Person {
@@ -1,16 +1,15 @@
package com.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 jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Past;
import jakarta.validation.constraints.Pattern;
import jakarta.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;
import org.springframework.data.couchbase.core.mapping.Field;
@Document
public class Student {
@@ -5,9 +5,7 @@ import java.util.List;
import com.baeldung.spring.data.couchbase.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.couchbase.core.CouchbaseTemplate;
import com.couchbase.client.java.view.Stale;
import com.couchbase.client.java.view.ViewQuery;
import org.springframework.data.couchbase.core.query.QueryCriteria;
public class CustomStudentRepositoryImpl implements CustomStudentRepository {
@@ -17,6 +15,6 @@ public class CustomStudentRepositoryImpl implements CustomStudentRepository {
private CouchbaseTemplate template;
public List<Student> findByFirstNameStartsWith(String s) {
return template.findByView(ViewQuery.from(DESIGN_DOC, "byFirstName").startKey(s).stale(Stale.FALSE), Student.class);
return template.findByQuery(Student.class).matching(QueryCriteria.where("firstName").startingWith(s)).all();
}
}
@@ -3,6 +3,7 @@ package com.baeldung.spring.data.couchbase.service;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import com.baeldung.spring.data.couchbase.model.Person;
import com.baeldung.spring.data.couchbase.repos.PersonRepository;
@@ -22,12 +23,12 @@ public class PersonRepositoryService implements PersonService {
this.repo = repo;
}
public Person findOne(String id) {
return repo.findOne(id);
public Optional<Person> findOne(String id) {
return repo.findById(id);
}
public List<Person> findAll() {
List<Person> people = new ArrayList<Person>();
List<Person> people = new ArrayList<>();
Iterator<Person> it = repo.findAll().iterator();
while (it.hasNext()) {
people.add(it.next());
@@ -1,12 +1,13 @@
package com.baeldung.spring.data.couchbase.service;
import java.util.List;
import java.util.Optional;
import com.baeldung.spring.data.couchbase.model.Person;
public interface PersonService {
Person findOne(String id);
Optional<Person> findOne(String id);
List<Person> findAll();
@@ -1,6 +1,9 @@
package com.baeldung.spring.data.couchbase.service;
import static org.springframework.data.couchbase.core.query.QueryCriteria.where;
import java.util.List;
import java.util.Optional;
import com.baeldung.spring.data.couchbase.model.Person;
import org.joda.time.DateTime;
@@ -9,8 +12,6 @@ import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.couchbase.core.CouchbaseTemplate;
import org.springframework.stereotype.Service;
import com.couchbase.client.java.view.ViewQuery;
@Service
@Qualifier("PersonTemplateService")
public class PersonTemplateService implements PersonService {
@@ -24,33 +25,33 @@ public class PersonTemplateService implements PersonService {
this.template = template;
}
public Person findOne(String id) {
return template.findById(id, Person.class);
public Optional<Person> findOne(String id) {
return Optional.of(template.findById(Person.class).one(id));
}
public List<Person> findAll() {
return template.findByView(ViewQuery.from(DESIGN_DOC, "all"), Person.class);
return template.findByQuery(Person.class).all();
}
public List<Person> findByFirstName(String firstName) {
return template.findByView(ViewQuery.from(DESIGN_DOC, "byFirstName"), Person.class);
return template.findByQuery(Person.class).matching(where("firstName").is(firstName)).all();
}
public List<Person> findByLastName(String lastName) {
return template.findByView(ViewQuery.from(DESIGN_DOC, "byLastName"), Person.class);
return template.findByQuery(Person.class).matching(where("lastName").is(lastName)).all();
}
public void create(Person person) {
person.setCreated(DateTime.now());
template.insert(person);
template.insertById(Person.class).one(person);
}
public void update(Person person) {
person.setUpdated(DateTime.now());
template.update(person);
template.removeById(Person.class).oneEntity(person);
}
public void delete(Person person) {
template.remove(person);
template.removeById(Person.class).oneEntity(person);
}
}
@@ -3,6 +3,7 @@ package com.baeldung.spring.data.couchbase.service;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import com.baeldung.spring.data.couchbase.model.Student;
import com.baeldung.spring.data.couchbase.repos.StudentRepository;
@@ -22,12 +23,12 @@ public class StudentRepositoryService implements StudentService {
this.repo = repo;
}
public Student findOne(String id) {
return repo.findOne(id);
public Optional<Student> findOne(String id) {
return repo.findById(id);
}
public List<Student> findAll() {
List<Student> people = new ArrayList<Student>();
List<Student> people = new ArrayList<>();
Iterator<Student> it = repo.findAll().iterator();
while (it.hasNext()) {
people.add(it.next());
@@ -1,12 +1,13 @@
package com.baeldung.spring.data.couchbase.service;
import java.util.List;
import java.util.Optional;
import com.baeldung.spring.data.couchbase.model.Student;
public interface StudentService {
Student findOne(String id);
Optional<Student> findOne(String id);
List<Student> findAll();
@@ -1,6 +1,9 @@
package com.baeldung.spring.data.couchbase.service;
import static org.springframework.data.couchbase.core.query.QueryCriteria.where;
import java.util.List;
import java.util.Optional;
import com.baeldung.spring.data.couchbase.model.Student;
import org.joda.time.DateTime;
@@ -9,8 +12,6 @@ import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.couchbase.core.CouchbaseTemplate;
import org.springframework.stereotype.Service;
import com.couchbase.client.java.view.ViewQuery;
@Service
@Qualifier("StudentTemplateService")
public class StudentTemplateService implements StudentService {
@@ -24,33 +25,33 @@ public class StudentTemplateService implements StudentService {
this.template = template;
}
public Student findOne(String id) {
return template.findById(id, Student.class);
public Optional<Student> findOne(String id) {
return Optional.of(template.findById(Student.class).one(id));
}
public List<Student> findAll() {
return template.findByView(ViewQuery.from(DESIGN_DOC, "all"), Student.class);
return template.findByQuery(Student.class).all();
}
public List<Student> findByFirstName(String firstName) {
return template.findByView(ViewQuery.from(DESIGN_DOC, "byFirstName"), Student.class);
return template.findByQuery(Student.class).matching(where("firstName").is(firstName)).all();
}
public List<Student> findByLastName(String lastName) {
return template.findByView(ViewQuery.from(DESIGN_DOC, "byLastName"), Student.class);
return template.findByQuery(Student.class).matching(where("lastName").is(lastName)).all();
}
public void create(Student student) {
student.setCreated(DateTime.now());
template.insert(student);
template.insertById(Student.class).one(student);
}
public void update(Student student) {
student.setUpdated(DateTime.now());
template.update(student);
template.upsertById(Student.class).one(student);
}
public void delete(Student student) {
template.remove(student);
template.removeById(Student.class).oneEntity(student);
}
}
@@ -1,5 +1,6 @@
package com.baeldung.spring.data.couchbase2b.service;
import java.util.Optional;
import java.util.Set;
import com.baeldung.spring.data.couchbase.model.Campus;
@@ -8,7 +9,7 @@ import org.springframework.data.geo.Point;
public interface CampusService {
Campus find(String id);
Optional<Campus> find(String id);
Set<Campus> findByName(String name);
@@ -2,6 +2,7 @@ package com.baeldung.spring.data.couchbase2b.service;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Optional;
import java.util.Set;
import com.baeldung.spring.data.couchbase2b.repos.CampusRepository;
@@ -22,8 +23,8 @@ public class CampusServiceImpl implements CampusService {
}
@Override
public Campus find(String id) {
return repo.findOne(id);
public Optional<Campus> find(String id) {
return repo.findById(id);
}
@Override
@@ -1,12 +1,13 @@
package com.baeldung.spring.data.couchbase2b.service;
import java.util.List;
import java.util.Optional;
import com.baeldung.spring.data.couchbase.model.Person;
public interface PersonService {
Person findOne(String id);
Optional<Person> findOne(String id);
List<Person> findAll();
@@ -3,6 +3,7 @@ package com.baeldung.spring.data.couchbase2b.service;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import com.baeldung.spring.data.couchbase2b.repos.PersonRepository;
import com.baeldung.spring.data.couchbase.model.Person;
@@ -20,12 +21,12 @@ public class PersonServiceImpl implements PersonService {
this.repo = repo;
}
public Person findOne(String id) {
return repo.findOne(id);
public Optional<Person> findOne(String id) {
return repo.findById(id);
}
public List<Person> findAll() {
List<Person> people = new ArrayList<Person>();
List<Person> people = new ArrayList<>();
Iterator<Person> it = repo.findAll().iterator();
while (it.hasNext()) {
people.add(it.next());
@@ -1,12 +1,13 @@
package com.baeldung.spring.data.couchbase2b.service;
import java.util.List;
import java.util.Optional;
import com.baeldung.spring.data.couchbase.model.Student;
public interface StudentService {
Student findOne(String id);
Optional<Student> findOne(String id);
List<Student> findAll();
@@ -3,6 +3,7 @@ package com.baeldung.spring.data.couchbase2b.service;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import com.baeldung.spring.data.couchbase2b.repos.StudentRepository;
import com.baeldung.spring.data.couchbase.model.Student;
@@ -20,12 +21,12 @@ public class StudentServiceImpl implements StudentService {
this.repo = repo;
}
public Student findOne(String id) {
return repo.findOne(id);
public Optional<Student> findOne(String id) {
return repo.findById(id);
}
public List<Student> findAll() {
List<Student> people = new ArrayList<Student>();
List<Student> people = new ArrayList<>();
Iterator<Student> it = repo.findAll().iterator();
while (it.hasNext()) {
people.add(it.next());