Spring Data JPA සිංහලෙන් | ආරම්භකයින්ට SC Guide

ආයුබෝවන් යාළුවනේ! කොහොමද ඉතිං? අද අපි කතා කරන්න යන්නේ Spring Framework එකේ තියෙන බලගතුම සහ වැඩියෙන්ම පාවිච්චි වෙන කොටසක් ගැන – ඒ තමයි Spring Data JPA. Software Development කරනකොට databases එක්ක වැඩ කරන එක නිකං දෛනික කාර්යයක් වගේ නේද? Database එකට data save කරන එක, update කරන එක වගේ දේවල් තමයි අපිට ගොඩක් වෙලාවට කරන්න තියෙන්නේ. මේ හැමදේටම අපි SQL queries ලියන්න ගියාම වෙලාව නාස්ති වෙනවා වගේම, code එකේ maintainability එකත් අඩු වෙනවා. ඉතිං මේ හැමදේම ලේසි කරන සුපිරිම ටූල් එකක් තමයි Spring Data JPA කියන්නේ. මේක කොච්චර ලේසිද කියනවා නම්, ඔයාලට පුළුවන් SQL query එකක්වත් ලියන්නේ නැතුව database එකත් එක්ක වැඩ කරන්න!
මේ post එකෙන් අපි බලමු:
- JPA (Java Persistence API) කියන්නේ මොකක්ද?
- Spring Data JPA කියන්නේ මොකක්ද?
- පොඩි Spring Boot project එකක් පටන් ගන්නේ කොහොමද?
- H2 database එකක් configure කරගන්නේ කොහොමද?
- අපේ application එකට Entity එකක් හදාගන්නේ කොහොමද කියලා.
එහෙනම්, අපි කිසිම වෙලාවක් නාස්ති නොකර කෙලින්ම පාඩමට බහිමු!
1. JPA (Java Persistence API) කියන්නේ මොකක්ද?
ඉස්සෙල්ලම අපි කතා කරමු JPA (Java Persistence API) කියන්නේ මොකක්ද කියලා. සරලවම කියනවා නම්, මේක Java applications වල Object-Relational Mapping (ORM) කරන්න පාවිච්චි කරන standard එකක්. ORM කියන්නේ අපේ Java objects (උදා: Student
object එකක්) database එකේ තියෙන tables එක්ක map කරන ක්රමයක්. ඒ කියන්නේ, අපිට Java object එකක් විදියට data එක්ක වැඩ කරන්න පුළුවන්, database එකේ tables විදියට ඒ data store වෙනවා. මේ නිසා අපිට SQL queries ලියන එක හුඟාක් අඩු වෙනවා, ඒ වගේම code එකේ maintainability එකත් වැඩි වෙනවා.
JPA කියන්නේ API එකක් මිසක් implementation එකක් නෙවෙයි. Hibernate, EclipseLink වගේ ඒවා තමයි JPA වල implementation frameworks. Spring Data JPA වලදී Hibernate තමයි default JPA provider විදියට පාවිච්චි වෙන්නේ.
2. Spring Data JPA වලට මාරු වෙමු!
දැන් අපි JPA ගැන පොඩි අදහසක් ගත්තා නේද? දැන් බලමු Spring Data JPA කියන්නේ මොකක්ද කියලා. Spring Data JPA කියන්නේ Spring Framework එකේ කොටසක්. මේකෙන් කරන්නේ JPA භාවිතය තවත් සරල කරන එක. Spring Data JPA වලදී Repository pattern එක හඳුන්වා දෙනවා. අපිට CRUD operations (Create, Read, Update, Delete) කරන්න ඕනේ වෙලාවට, Spring Data JPA අපිට standard interfaces ටිකක් දීලා තියෙනවා. අපිට අවශ්ය දේට අනුව ඒ interfaces extend කරලා ගන්න විතරයි තියෙන්නේ. මේකෙන් boilerplate code හුඟාක් අඩු වෙනවා වගේම, custom queries ලියන්න, pagination, sorting වගේ advanced features සරලව implement කරන්නත් පුළුවන්. Spring Data JPA පාවිච්චි කරන්න, අපිට ඕනේ වෙන්නේ spring-boot-starter-data-jpa
කියන dependency එක විතරයි.
3. Project එක පටන් ගමු!
හරි, දැන් අපි theory ඇති තරම් කතා කළා. දැන් අපි practical පැත්තට යමු. මුලින්ම අපි පොඩි Spring Boot project එකක් හදාගමු. මේකට අපි පාවිච්චි කරන්නේ Spring Initializr (start.spring.io) කියන tool එක.
පහත step ටික follow කරන්න:
- ඔයාගේ web browser එකේ https://start.spring.io/ open කරගන්න.
- පහත විදියට details ටික configure කරන්න (Group සහ Artifact ඔයාට කැමති විදියට වෙනස් කරගන්න පුළුවන්):
- Project: Maven Project
- Language: Java
- Spring Boot: 3.x.x (latest stable version)
- Group:
com.example
- Artifact:
jpaguide
- Java: 17 (හෝ ඔයාට කැමති version එකක්)
- “Add Dependencies…” button එක click කරලා පහත dependencies add කරන්න:
- Spring Web
- Spring Data JPA
- H2 Database
- Lombok (Optional)
- අවසානයේ “Generate” button එක click කරන්න. එතකොට ඔයාට project එක download වෙයි.
- ඒක extract කරලා ඔයාගේ IDE එක (IntelliJ IDEA, VS Code, Eclipse වගේ) පාවිච්චි කරලා project එක open කරගන්න.
දැන් ඔයාට basic Spring Boot project structure එකක් ලැබිලා ඇති.
4. H2 Database එක Configure කරමු
Spring Data JPA එක්ක වැඩ කරන කොට database එකක් අවශ්යයි නේද? මේ project එකට අපි H2 database එක පාවිච්චි කරනවා. H2 කියන්නේ in-memory database එකක්. ඒ කියන්නේ, ඔයාගේ application එක run වෙනකම් විතරයි data තියෙන්නේ. Application එක shut down කළොත් data ටික නැති වෙලා යනවා. Development සහ testing වලට මේක හරිම පහසුයි.
H2 database එක configure කරන්න, ඔයාගේ project එකේ src/main/resources/application.properties
file එක open කරලා පහත properties ටික add කරන්න:
# H2 Database Configuration
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
# DataSource Configuration
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
# JPA/Hibernate Configuration
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
මේ Properties ගැන පොඩ්ඩක් බලමු:
spring.h2.console.enabled=true
: මේකෙන් H2 console එක enable කරනවා. ඒකෙන් browser එකෙන් database එකේ tables, data වගේ දේවල් බලන්න පුළුවන්.spring.h2.console.path=/h2-console
: H2 console එකට access වෙන්න ඕනේ URL path එක. (http://localhost:8080/h2-console
)spring.datasource.url=jdbc:h2:mem:testdb
: Database එකේ URL එක.spring.datasource.username=sa
සහspring.datasource.password=
: H2 database එකට login වෙන්න අවශ්ය credentials.spring.jpa.show-sql=true
: Spring Data JPA generate කරන SQL queries console එකේ print කරනවා. Debugging වලට ප්රයෝජනවත්.spring.jpa.hibernate.ddl-auto=update
: Hibernate වලින් Database Schema එක manage කරන්න මේක පාවිච්චි කරනවා.update
කියන්නේ Entity class වලට අනුව database schema එක update කරනවා. (Production වලට ප්රවේශමෙන් භාවිතා කරන්න)
5. Entity එකක් හදමු
දැන් අපි database එක configure කරගත්තා. දැන් අපි අපේ application එකට Entity එකක් හදමු. Entity කියන්නේ database table එකක representation එකක්. සරලව කිව්වොත්, අපේ Java object එකක් තමයි database table එකක් විදියට හැසිරෙන්නේ.
අපි Student
කියන Entity එකක් හදමු. src/main/java/com/example/jpaguide/model
කියන package එක ඇතුලේ Student.java
කියලා file එකක් හදලා මේ code එක paste කරන්න:
package com.example.jpaguide.model;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private int age;
private String email;
}
භාවිතා කරපු Annotations ගැන:
@Entity
: මේ Class එක database table එකකට map වෙනවා කියලා කියනවා.@Id
:id
field එක Primary Key එක කියලා කියනවා.@GeneratedValue(strategy = GenerationType.IDENTITY)
:id
එක auto-increment වෙන්න ඕනේ කියලා කියනවා.@Data
,@NoArgsConstructor
,@AllArgsConstructor
(Lombok): මේවා getters, setters, constructors වගේ boilerplate code automatically generate කරනවා.
Repository Interface එක හදමු:
දැන් අපි Student
Entity එකට අදාළව database operations කරන්න Repository interface එකක් හදමු. src/main/java/com/example/jpaguide/repository
කියන package එක ඇතුලේ StudentRepository.java
කියලා file එකක් හදලා මේ code එක paste කරන්න:
package com.example.jpaguide.repository;
import com.example.jpaguide.model.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface StudentRepository extends JpaRepository<Student, Long> {
// Spring Data JPA automatically provides CRUD methods
// You can add custom query methods here if needed
}
StudentRepository
interface එක JpaRepository<Student, Long>
extend කළාම Spring Data JPA එකෙන් save()
, findById()
, findAll()
, deleteById()
වගේ methods ගොඩක් අපිට provide කරනවා. ඒ නිසා අපිට ඒවා separately implement කරන්න අවශ්ය වෙන්නේ නෑ.
Controller එකක් හදලා Test කරමු:
දැන් අපි Entity එකයි, Repository එකයි හදාගත්තා. දැන් අපි පොඩි REST Controller එකක් හදලා මේවා වැඩ කරනවද කියලා බලමු. src/main/java/com/example/jpaguide/controller
කියන package එක ඇතුලේ StudentController.java
කියලා file එකක් හදලා මේ code එක paste කරන්න:
package com.example.jpaguide.controller;
import com.example.jpaguide.model.Student;
import com.example.jpaguide.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/students")
public class StudentController {
@Autowired
private StudentRepository studentRepository;
@PostMapping
public Student addStudent(@RequestBody Student student) {
return studentRepository.save(student);
}
@GetMapping
public List<Student> getAllStudents() {
return studentRepository.findAll();
}
@GetMapping("/{id}")
public Student getStudentById(@PathVariable Long id) {
return studentRepository.findById(id).orElse(null);
}
}
Project එක run කරලා, Postman වගේ REST client එකකින් මේ endpoints test කරන්න. http://localhost:8080/h2-console
එකට ගිහින් database එක බලන්නත් පුළුවන්.
අවසානයට…
ඉතිං යාළුවනේ, මේ post එකෙන් අපි Spring Data JPA කියන්නේ මොකක්ද, ඒකෙන් අපේ database operations කොච්චර ලේසි වෙනවද, වගේම පොඩි Spring Boot project එකක් හදලා, H2 database එකක් configure කරලා, Entity එකක් හදාගන්නේ කොහොමද, ඒක test කරන්නේ කොහොමද කියලා බැලුවා. මේක Spring Boot ecosystem එකේ තියෙන බලගතුම features වලින් එකක්. මේකෙන් අපිට boilerplate code අඩු කරලා, ඉක්මනින් application develop කරන්න පුළුවන්.
මේක තවදුරටත් පුරුදු වෙන්න, ඔයාලට පුළුවන් මේ project එකේම තව entities හදලා බලන්න, custom queries ලියලා බලන්න, updates, deletes වගේ operations add කරන්න. කිසිම ප්රශ්නයක් ආවොත්, පහළින් comment එකක් දාන්න. මට පුළුවන් විදියට උදව් කරන්නම්. තවත් මේ වගේ article එකකින් හමුවෙමු! ඔයාලට සුභ දවසක්, සහ happy coding!