NoSQL (MongoDB) Example

This example demonstrates the usage of the j-framework to data access on a MongoDB instead of a relational database.

Prerequisites:

  1. You should have JDK 17+ Installed. (Click Here).

  2. You should create a Maven Project.

  3. You should enable snapshot versions. (Click Here).

Project Content:

This section contains what you should add to your code structure for this example.

  1. Maven Project with pom.xml that has the following contents:

<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>
	<parent>
		<groupId>com.jalalkiswani</groupId>
		<artifactId>j-app-webstack</artifactId>
		<version>7.0.0-SNAPSHOT</version>
	</parent>
	<artifactId>j-framework-data-nosql-mongo-example</artifactId>

</project>
  1. Database configurations file located at src/main/resources/config.properties that has the following content:

nosql.url=mongodb://localhost:27017/
nosql.db.name=app
nosql.db.user=
nosql.db.password=
  1. Account Model class located at src/main/app/Account.java that has the following content:

package com.app;

import java.util.UUID;

import com.jk.core.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. Main java class located at src/main/java/com/app/App.java which contains the following:

package com.app;

import java.util.List;

import com.jk.core.util.JK;
import com.jk.data.dataaccess.JKDataAccessFactory;
import com.jk.data.dataaccess.nosql.JKNoSqlDataAccess;

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);
	}

}

Alternatively, you can clone or download the tutorial repository then import the project into your IDE.

How to run Project:

  1. Set up the project with the content shown above.

  2. Inside your IDE, go to the src/main/java/com/app/App.java class.

Main Class

  1. Next, run it as a Java Application.

Run as Java Application

  1. Your program will start running and the output result will show in the console.

Example Explanation

  1. This example demonstrates how to use J-Framework to data access on a MongoDB instead of a relational database, check the main app class for more details on how this output was generated.