forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
example code for BAEL-1961 (eugenp#5781)
* 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
- Loading branch information
Showing
7 changed files
with
222 additions
and
0 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
spring-boot/src/main/java/com/baeldung/mongodb/Application.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,19 @@ | ||
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); | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
spring-boot/src/main/java/com/baeldung/mongodb/daos/UserRepository.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
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> { | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
spring-boot/src/main/java/com/baeldung/mongodb/events/UserModelListener.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
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)); | ||
} | ||
|
||
|
||
} |
32 changes: 32 additions & 0 deletions
32
spring-boot/src/main/java/com/baeldung/mongodb/models/DatabaseSequence.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
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; | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
spring-boot/src/main/java/com/baeldung/mongodb/models/User.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
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 + '\'' + | ||
'}'; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
spring-boot/src/main/java/com/baeldung/mongodb/services/SequenceGeneratorService.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
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; | ||
|
||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
spring-boot/src/test/java/com/baeldung/mongodb/MongoDbAutoGeneratedFieldIntegrationTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.baeldung.mongodb; | ||
|
||
import com.baeldung.mongodb.daos.UserRepository; | ||
import com.baeldung.mongodb.models.User; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.List; | ||
|
||
|
||
@RunWith(SpringRunner.class) | ||
public class MongoDbAutoGeneratedFieldIntegrationTest { | ||
|
||
@Autowired | ||
private UserRepository userRepository; | ||
|
||
@Test | ||
public void contextLoads() {} | ||
|
||
@Test | ||
public void givenUserObject_whenSave_thenCreateNewUser() { | ||
|
||
User user = new User(); | ||
user.setFirstName("John"); | ||
user.setLastName("Doe"); | ||
user.setEmail("[email protected]"); | ||
userRepository.save(user); | ||
|
||
assertThat(userRepository.findAll().size()).isGreaterThan(0); | ||
} | ||
|
||
|
||
} |