Home | Tutorials | Smart-Cloud | Smart-EIS | License | FAQ | Contact

Last Updated: March 12, 2021

Pre-Requisites:

  1. Have your MongoDB installed and running

Steps:

  1. Create Maven project with the following pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.jalalkiswani</groupId>
	<version>4.0.4</version>
	<artifactId>smart-cloud-data-nosql-mongo-example</artifactId>
	<dependencies>
		<dependency>
			<groupId>com.jalalkiswani</groupId>
			<artifactId>smart-cloud-data</artifactId>
			<version>4.0.8</version>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

If you are using Eclipse, be sure to refresh your project (select your project→ right click→ Maven→ Update Project)

  1. Create config file at src/main/resources/config.properties with the following contents:

nosql.url=mongodb://localhost:27017/
nosql.db.name=app
nosql.db.user=
nosql.db.password=
  1. Create Model Account with the following contents:

package com.app;

import java.util.UUID;

import com.jk.util.JKObjectUtil;

public class Account {
	String id = UUID.randomUUID().toString();
	String name;
	double balance;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getBalance() {
		return balance;
	}

	public void setBalance(double balance) {
		this.balance = balance;
	}

	@Override
	public String toString() {
		return JKObjectUtil.toString(this, false);
	}

}
  1. Create Main class with the following contents:

package com.app;

import java.util.List;

import com.jk.db.dataaccess.nosql.JKNoSqlDataAccess;
import com.jk.db.dataaccess.orm.JKObjectDataAccess;
import com.jk.db.datasource.JKDataAccessFactory;
import com.jk.util.JK;

public class App {
	private static String id;

	public static void main(String[] args) {
		JKNoSqlDataAccess dataAccess = JKDataAccessFactory.getNoSqlDataAccess();

		insert(dataAccess);
		getAll(dataAccess);
		find(dataAccess);
		update(dataAccess);
		delete(dataAccess);
	}

	private static void insert(JKNoSqlDataAccess dataAccess) {
		Account account = new Account();
		account.setName("Jalal");
		account.setBalance(100);

		// insert the object in the database using JPA/Hibernate implementation
		dataAccess.insert(account);
		id = account.getId();
	}

	private static void getAll(JKNoSqlDataAccess dataAccess) {
		// Retrieve list of objects from database using JPA
		List<Account> list = dataAccess.getList(Account.class);
		for (Account account : list) {
			JK.print(account);
		}
	}

	private static Account find(JKNoSqlDataAccess dataAccess) {
		// Find an object from database using JPA
		Account std = dataAccess.find(Account.class, id);
		return std;
	}

	private static void update(JKNoSqlDataAccess dataAccess) {
		Account std = find(dataAccess);
		std.setName("Updated Jalal");

		// update record in the databse
		dataAccess.update(std);
	}

	private static void delete(JKNoSqlDataAccess dataAccess) {
		dataAccess.delete(Account.class, id);
	}

}

Thats it, now run your App class.

Output

Full example source-code can be found at https://github.com/smartapi-com/smart-cloud-data-nosql-mongo


Home | Tutorials | Smart-Cloud | Smart-EIS | License | FAQ | Contact