refactor the spring boot persistence mongodb module (#5829)
* added example code for BAEL-2366 * moved example code for BAEL-2366 * example code for BAEL-1961 * moved example code into integration test * updated the test assertions * refactor the spring boot persistence mongodb module * remove redundant example code * declared the spring boot persistence module in the root pom * fixed issue with non-imported file
This commit is contained in:
@@ -1,19 +0,0 @@
|
||||
package com.baeldung.mongodb;
|
||||
|
||||
import com.baeldung.mongodb.daos.UserRepository;
|
||||
import com.baeldung.mongodb.models.User;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package com.baeldung.mongodb.daos;
|
||||
|
||||
|
||||
import com.baeldung.mongodb.models.User;
|
||||
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||
|
||||
|
||||
public interface UserRepository extends MongoRepository<User, Long> {
|
||||
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
package com.baeldung.mongodb.events;
|
||||
|
||||
|
||||
import com.baeldung.mongodb.models.User;
|
||||
import com.baeldung.mongodb.services.SequenceGeneratorService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener;
|
||||
import org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
|
||||
@Component
|
||||
public class UserModelListener extends AbstractMongoEventListener<User> {
|
||||
|
||||
private SequenceGeneratorService sequenceGenerator;
|
||||
|
||||
@Autowired
|
||||
public UserModelListener(SequenceGeneratorService sequenceGenerator) {
|
||||
this.sequenceGenerator = sequenceGenerator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBeforeConvert(BeforeConvertEvent<User> event) {
|
||||
event.getSource().setId(sequenceGenerator.generateSequence(User.SEQUENCE_NAME));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package com.baeldung.mongodb.models;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.mongodb.core.mapping.Document;
|
||||
|
||||
|
||||
@Document(collection = "database_sequences")
|
||||
public class DatabaseSequence {
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
|
||||
private long seq;
|
||||
|
||||
public DatabaseSequence() {}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public long getSeq() {
|
||||
return seq;
|
||||
}
|
||||
|
||||
public void setSeq(long seq) {
|
||||
this.seq = seq;
|
||||
}
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
package com.baeldung.mongodb.models;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.annotation.Transient;
|
||||
import org.springframework.data.mongodb.core.mapping.Document;
|
||||
|
||||
|
||||
@Document(collection = "users")
|
||||
public class User {
|
||||
|
||||
@Transient
|
||||
public static final String SEQUENCE_NAME = "users_sequence";
|
||||
|
||||
@Id
|
||||
private long id;
|
||||
|
||||
private String firstName;
|
||||
|
||||
private String lastName;
|
||||
|
||||
private String email;
|
||||
|
||||
public User() { }
|
||||
|
||||
public User(String firstName, String lastName, String email) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long 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 String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" +
|
||||
"id=" + id +
|
||||
", firstName='" + firstName + '\'' +
|
||||
", lastName='" + lastName + '\'' +
|
||||
", email='" + email + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
package com.baeldung.mongodb.services;
|
||||
|
||||
import com.baeldung.mongodb.models.DatabaseSequence;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.mongodb.core.MongoOperations;
|
||||
import org.springframework.data.mongodb.core.query.Update;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import static org.springframework.data.mongodb.core.FindAndModifyOptions.options;
|
||||
import static org.springframework.data.mongodb.core.query.Criteria.where;
|
||||
import static org.springframework.data.mongodb.core.query.Query.query;
|
||||
|
||||
|
||||
@Service
|
||||
public class SequenceGeneratorService {
|
||||
|
||||
|
||||
private MongoOperations mongoOperations;
|
||||
|
||||
@Autowired
|
||||
public SequenceGeneratorService(MongoOperations mongoOperations) {
|
||||
this.mongoOperations = mongoOperations;
|
||||
}
|
||||
|
||||
public long generateSequence(String seqName) {
|
||||
|
||||
DatabaseSequence counter = mongoOperations.findAndModify(query(where("_id").is(seqName)),
|
||||
new Update().inc("seq",1), options().returnNew(true).upsert(true),
|
||||
DatabaseSequence.class);
|
||||
return !Objects.isNull(counter) ? counter.getSeq() : 1;
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user