JPA Example

This example demonstrates the usage of the j-framework in creating a project that uses JPA Object Relational Mapping (ORM) to persist and retrieve data from locally created H2 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-jpa-example</artifactId>
</project>
  1. Database configurations file located at src/main/resources/config.properties that has the following content:

hibernate.connection.url = jdbc:h2:file:./h2db.data

hibernate.connection.username=sa
hibernate.connection.password=sa

hibernate.hbm2ddl.auto=update
db-entities-packages=com.app
  1. Java Model class located at src/main/com/app/Model.java that has the following content:

package com.app;

import java.io.Serializable;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

@Entity
@Table(name="person")
public class Model implements Serializable {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name="id")
	private Integer id;

	@Column(name="national_id")
	private String nationalId;

	@Column(name="name")
	private String name;

	@Column(name="email")
	private String email;

	@Column(name="address")
	private String address;


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

	public Integer getId(){
	 return this.id;
	}

	public void setNationalId(String nationalId){
	  this.nationalId=nationalId;
	}

	public String getNationalId(){
	 return this.nationalId;
	}

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

	public String getName(){
	 return this.name;
	}

	public void setEmail(String email){
	  this.email=email;
	}

	public String getEmail(){
	 return this.email;
	}

	public void setAddress(String address){
	  this.address=address;
	}

	public String getAddress(){
	 return this.address;
	}


	@Override
	public boolean equals(Object obj) {
	  if (obj == null) {
		return false;
	  }
	  return this.getId().equals(((Model) obj).getId());
	}

	@Override
	public int hashCode() {
	  if(this.id==null) {
	    return toString().hashCode();
	   }
	  return this.id.hashCode();
	}

	@Override
	public String toString(){
	  StringBuffer buf=new StringBuffer();
	  buf.append(this.id).append(",");
	  buf.append(this.nationalId).append(",");
	  buf.append(this.name).append(",");
	  buf.append(this.email).append(",");
	  buf.append(this.address).append(",");
	  return buf.toString();
	}
}
  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.orm.JKObjectDataAccess;

public class App {

	private static Integer id;

	public static void main(String[] args) {
		JKObjectDataAccess da = JKDataAccessFactory.getObjectDataAccessService();

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

	private static void insert(JKObjectDataAccess da) {
		Model model = new Model();
		model.setNationalId("12345678");
		model.setName("Jalal");
		model.setAddress("Reno, NV");
		model.setEmail("[email protected]");
		id= da.insert(model).getId();
	}

	private static void find(JKObjectDataAccess da) {
		Model model = da.find(Model.class, id);
		JK.printBlock(model);
	}

	private static void update(JKObjectDataAccess da) {
		Model model = da.find(Model.class, id);
		model.setName("Updated " + model.getName());
		da.update(model);
	}

	private static void getAll(JKObjectDataAccess da) {
		List<Model> list = da.getList(Model.class);
		for (Model model : list) {
			JK.printBlock(model);
		}
	}

	private static void delete(JKObjectDataAccess da) {
		da.delete(Model.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.

localhost:8080

Example Explanation

  1. This example demonstrates how to use J-Framework to use JPA Object Relational Mapping (ORM) to persist and retrieve data from locally created database. Check the main app class for more details on how this output was generated.