Code, JavaEE

Mongo DB NoSQL Basics

MAVEN CONFIGURATION

Starting with the project which is a maven project, add the necessary dependencies for JUnit and Mongo as shown.


<dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>4.11</version>
 <scope>test</scope>
 </dependency>

<dependency>
 <groupId>org.springframework.data</groupId>
 <artifactId>spring-data-mongodb</artifactId>
 <version>1.2.0.RELEASE</version>
 </dependency>

<dependency>
 <groupId>org.mongodb</groupId>
 <artifactId>mongo-java-driver</artifactId>
 <version>2.11.0</version>
 </dependency>

 

SPRING CONFIG USING JAVA


@Configuration
public class MongoConfig {

@Bean
 public MongoDbFactory mongoDbFactory() throws Exception {
 return new SimpleMongoDbFactory(new MongoClient(), "core");
 }

@Bean
 public MongoTemplate mongoTemplate() throws Exception {
 MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory());
 return mongoTemplate;
 }

@Bean
 public UserMongoDao userMongoDao() throws Exception {
 UserMongoDao userMongoDao = new UserMongoDao();
 userMongoDao.setMongoOperations(mongoTemplate());
 return userMongoDao;
 }

}

// ANNOTATED POJO: User

@Document(collection="users")
public class User implements Serializable {
 @Id
 private String id;
 private String firstName;
 private String lastName;
 private String email;

public User() {
 setId(UUID.randomUUID().toString());
 }

public User(String id, String firstName, String lastName, String email) {
 this.id = id;
 this.firstName = firstName;
 this.lastName = lastName;
 this.email = email;
 }

public String getId() {
 return id;
 }

public void setId(String id) {
 if (null != 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 boolean equals(Object o) {
 if (this == o) return true;
 if (o == null || getClass() != o.getClass()) return false;

User user = (User) o;

if (!email.equals(user.email)) return false;
 if (!firstName.equals(user.firstName)) return false;
 if (!id.equals(user.id)) return false;
 if (!lastName.equals(user.lastName)) return false;

return true;
 }

@Override
 public int hashCode() {
 int result = id.hashCode();
 result = 31 * result + firstName.hashCode();
 result = 31 * result + lastName.hashCode();
 result = 31 * result + email.hashCode();
 return result;
 }

@Override
 public String toString() {
 return "User{" +
 "id='" + id + '\'' +
 ", firstName='" + firstName + '\'' +
 ", lastName='" + lastName + '\'' +
 ", email='" + email + '\'' +
 '}';
 }
}

// USER MONGO DAO

public class UserMongoDao {
 private MongoOperations mongoOperations;

public void setMongoOperations(MongoOperations mongoOperations) {
 this.mongoOperations = mongoOperations;
 }

public void create(User user) {
 if (null != user) {

// Make sure we don't duplicate this user
 Query findUser = new Query(Criteria.where("email").is(user.getEmail()));
 User comparisonUser = mongoOperations.findOne(findUser, User.class);

if (comparisonUser == null) {
 // create the new user, it's not a duplicate
 // Persist
 mongoOperations.save(user);
 System.out.printf("Creating user with e-mail address - %s%n", user.getEmail());
 } else {
 // this is a duplicate, note it and move on.
 System.out.println("Not creating, this is a duplicate, note it and move on.");
 }

}
 }

public List<User> find() {
 List<User> retList = new LinkedList<>();
 List<User> usersList = mongoOperations.findAll(User.class);
 if (usersList.isEmpty()) {
 System.out.println("There were no user's found.");
 } else {
 retList.addAll(usersList);
 }

return retList;
 }
}

// JUNIT MONGO DB TEST

public class MongoDBTest {
 private ApplicationContext applicationContext;
 private MongoOperations mongoOperations;
 private UserMongoDao dao;

@Test
 public void testApplicationContext() {
 assertNotNull("ApplicationContext was null", applicationContext);
 System.out.printf("ApplicationContext-> %s%n", applicationContext);
 }

@Test
 public void testMongoOperations() {
 assertNotNull("MongoOperations was null", mongoOperations);
 System.out.printf("MongoOperations-> %s%n", mongoOperations);
 }

@Test
 public void testUserMongoDao() {
 assertNotNull("UserMongoDao was null", dao);
 System.out.printf("UserMongoDao-> %s%n", dao);
 }

&nbsp;

@Test
 public void testMongoPersistence() {
 // User
 dao.create(testUser());
 }

@Test
 public void testMongoQuery() {
 // User
 List<User> userList = dao.find();
 if (userList != null && !userList.isEmpty()) {
 System.out.printf("UserList: %s%n", userList);
 }
 }

private User testUser() {

// Create User - simulated data from the UI or some other service/app
 User user = new User();
 user.setFirstName("David");
 user.setLastName("King");
 user.setEmail("david.king@software.net");
 return user;
 }

<span style="line-height: 1.5em;">@Before
</span>public void setUp() {

applicationContext = new AnnotationConfigApplicationContext(MongoConfig.class);
 mongoOperations = applicationContext.getBean("mongoTemplate", MongoOperations.class);
 dao = applicationContext.getBean("userMongoDao", UserMongoDao.class);
}

@After
 public void tearDown() {
 applicationContext = null;
 mongoOperations = null;
 dao = null;
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s