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

Last Updated: March 12, 2021

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-jpa-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:

hibernate.connection.driver_class = org.h2.Driver
hibernate.connection.url = jdbc:h2:file:./h2db.data
hibernate.dialect = org.hibernate.dialect.H2Dialect

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

hibernate.hbm2ddl.auto=update
db-entities-packages=com.app
  1. Create Model with the following contents:

package com.app;

import java.io.Serializable;
import javax.persistence.*;

@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. Create Main class with the following contents:

package com.app;

import java.util.List;

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

public class App {

	private static Integer id;

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

		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());
		model.setAddress("Updated " + model.getAddress());
		model.setEmail("Updated " + model.getEmail());
		model.setNationalId("Updated " + model.getNationalId());
		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);
	}

}

Thats it, now run your App class.

Output

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


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