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:
-
You should have JDK 17+ Installed. (Click Here).
-
You should create a Maven Project.
-
You should enable snapshot versions. (Click Here).
Project Content:
This section contains what you should add to your code structure for this example.
-
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>
-
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
-
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();
}
}
-
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. |