JPA Repositories: ඔබේ Data Manage කරන්න! - A Sinhala Guide

JPA Repositories: ඔබේ Data Management Simplified!
කොහොමද යාලුවනේ! අද අපි කතා කරන්න යන්නේ, Spring Boot project එකක් කරද්දී, data base එකත් එක්ක වැඩ කරන එක හුඟක්ම ලේසි කරන සුපිරි feature එකක් ගැන. ඒ තමයි JPA Repositories.
හිතලා බලන්න, ඔයාලා project එකක් කරනකොට, data base එකෙන් data ගන්න, දාන්න, update කරන්න, delete කරන්න කොච්චර code ලියන්න ඕනද කියලා. Traditional DAO (Data Access Object) pattern එකක් පාවිච්චි කරනවා නම්, හැම entity එකකටම වෙනම class එකක් හදලා, ඒ හැම class එකකම CRUD (Create, Read, Update, Delete) operations වලට methods ලියන්න වෙනවා. ඒක හුඟක් වෙලාවට boring වැඩක් නේද? ඒ වගේම code ගොඩක් වැඩි වෙනවා, maintain කරන්න අමාරු වෙනවා.
නමුත් JPA Repositories ආවට පස්සේ, මේ වැඩේ completely වෙනස් වුණා. Spring Data JPA කියන්නේ මේ වැඩේ හරිම smart විදිහට කරන framework එකක්. මේකෙන් අපි කරන්නේ පොඩි Interface එකක් හදන එක විතරයි. එතකොන් අනිත් හැමදේම Spring Data JPA බලාගන්නවා. නියමයි නේද? අද අපි මේක ගැන සවිස්තරාත්මකව කතා කරමු. කෙලින්ම වැඩේට බහිමු!
JPA Repositories කියන්නේ මොනවද? (What are JPA Repositories?)
සරලවම කිව්වොත්, JPA Repository කියන්නේ Spring Data JPA වල තියෙන interface එකක්. මේකෙන් අපිට data base එකත් එක්ක interact කරන්න අවශ්ය පොදු methods (like save()
, findById()
, findAll()
, delete()
) ready-made විදිහට ලැබෙනවා. අපි කරන්න ඕන මේ interface එක extend කරන අපේම interface එකක් හදාගන්න එක විතරයි.
මීට කලින් අපි කරපු විදිහට, data access layer එක හදනවා නම්, හැම entity එකකටම වෙනම class එකක් හදලා, ඒ class එක ඇතුලේ EntityManager
එක පාවිච්චි කරලා queries ලියන්න ඕන වෙනවා. ඒ වගේම ඒ methods වලට transactions manage කරන්නත් සිද්ධ වෙනවා. මේ හැමදේටම Spring Data JPA අපිට solutions දෙනවා. අපේ කාලයත්, code line ගාණත් අඩු වෙනවා. Developer productivity එක වැඩි වෙනවා.
ඔබ Spring Boot project එකක් පටන් ගනිද්දී spring-boot-starter-data-jpa
dependency එක add කරගත්තා නම්, මේක automatically configure වෙනවා. Database connectivity settings (e.g., application.properties
file එකේ spring.datasource.url
, spring.datasource.username
වගේ ඒවා) හරියට configure කරලා තිබ්බොත්, ඔක්කොම හරි.
සරලව Repository එකක් හදාගමු! (Let's create a simple Repository!)
දැන් අපි බලමු කොහොමද මේ repository එකක් හදාගන්නේ කියලා. මුලින්ම අපිට database එකේ store කරන්න ඕන entity එකක් ඕනේ. අපි Product
කියලා entity එකක් හදාගමු.
1. Product Entity එක හදමු
Product.java
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private double price;
private String description;
// Constructors
public Product() {
}
public Product(String name, double price, String description) {
this.name = name;
this.price = price;
this.description = description;
}
// Getters and Setters
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "Product{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
", description='" + description + '\'' +
'}';
}
}
2. ProductRepository Interface එක හදමු
දැන් තමයි අපේ වැඩේට වැදගත්ම කොටස. අපි ProductRepository
කියලා interface එකක් හදමු. මේක JpaRepository
එක extend කරන්න ඕන.
ProductRepository.java
import org.springframework.data.jpa.repository.JpaRepository;
import org.stereotype.Repository;
@Repository // Optional but good practice for clarity
public interface ProductRepository extends JpaRepository<Product, Long> {
// That's it! No methods needed here for basic CRUD.
// Spring Data JPA will provide them automatically.
}
ඔබට පේනවා ඇති, අපි ProductRepository
interface එක ඇතුලේ කිසිම method එකක් ලිව්වේ නැහැ. ඒත් මේක compile කරාම, Spring Data JPA automatically අපිට Product
entity එකට අදාල CRUD operations වලට අවශ්ය methods ටික generate කරනවා. JpaRepository<Product, Long>
කියන එකෙන් අපි කියන්නේ, මේ repository එක Product
entity එකත් එක්ක වැඩ කරනවා, ඒ වගේම ඒ entity එකේ primary key type එක Long
කියලයි.
CRUD Operations සුළු මොහොතින්! (CRUD Operations in a Flash!)
දැන් අපි බලමු කොහොමද මේ repository එක පාවිච්චි කරලා data base operations කරන්නේ කියලා. අපි ProductService
කියලා service class එකක් හදලා ඒක ඇතුලේ මේ repository එක inject (dependency injection) කරගමු.
ProductService.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class ProductService {
private final ProductRepository productRepository;
@Autowired
public ProductService(ProductRepository productRepository) {
this.productRepository = productRepository;
}
// Create/Update
public Product saveProduct(Product product) {
return productRepository.save(product);
}
// Read all products
public List<Product> getAllProducts() {
return productRepository.findAll();
}
// Read product by ID
public Optional<Product> getProductById(Long id) {
return productRepository.findById(id);
}
// Delete product by ID
public void deleteProduct(Long id) {
productRepository.deleteById(id);
}
// Check if product exists
public boolean productExists(Long id) {
return productRepository.existsById(id);
}
}
මේ ProductService
එක ඔයාලගේ Controller එකකට inject කරලා පාවිච්චි කරන්න පුළුවන්. බලන්න, save()
, findAll()
, findById()
, deleteById()
වගේ methods අපිට කිසිම code එකක් නොලියාම ලැබිලා තියෙනවා. නියම පහසුවක් නේද?
Custom Queries ලියමු! (Let's write Custom Queries!)
සමහර වෙලාවට අපිට id
එකෙන්, නැත්නම් හැම product එකක්ම ගන්නවට වඩා සංකීර්ණ queries ලියන්න ඕනේ වෙනවා. Spring Data JPA මේකටත් අපිට නියම විදිහට උදව් කරනවා.
1. Method Naming Conventions පාවිච්චි කරමු
Spring Data JPA වල තියෙන සුපිරිම feature එකක් තමයි method naming conventions. අපිට අවශ්ය query එකේ logic එක method name එකෙන් provide කරන්න පුළුවන්. Spring Data JPA ඒ method name එක parse කරලා ඒකට අදාල query එක automatically generate කරනවා.
උදාහරණයක් විදිහට, Product
එකේ name
එකෙන් search කරන්න ඕන නම්, අපේ ProductRepository
interface එකට මෙහෙම method එකක් add කරන්න පුළුවන්:
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface ProductRepository extends JpaRepository<Product, Long> {
List<Product> findByName(String name);
List<Product> findByPriceGreaterThan(double price);
List<Product> findByNameContainingIgnoreCase(String keyword);
Optional<Product> findByNameAndPrice(String name, double price);
}
බලන්න, findByName
, findByPriceGreaterThan
වගේ නම් වලින් Spring Data JPA අඳුනගන්නවා මොන query එකද execute කරන්න ඕන කියලා. මේකෙන් අපේ කාලය ගොඩක් ඉතුරු වෙනවා. පුළුවන් නම් මේ වගේ queries වලට මේ naming convention එක පාවිච්චි කරන්න.
2. @Query Annotation එක පාවිච්චි කරමු
සමහර වෙලාවට method naming convention එකෙන් අපිට ඕන query එක හදන්න බැරි වෙන්න පුළුවන්. එහෙම වෙලාවට අපිට @Query
annotation එක පාවිච්චි කරලා JPQL (Java Persistence Query Language) හෝ native SQL queries ලියන්න පුළුවන්.
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
public interface ProductRepository extends JpaRepository<Product, Long> {
// Custom JPQL query to find products with price less than given amount
@Query("SELECT p FROM Product p WHERE p.price < :price")
List<Product> findProductsByPriceLessThan(@Param("price") double price);
// Custom native SQL query to find products by description keyword
@Query(value = "SELECT * FROM product WHERE description LIKE %:keyword%", nativeQuery = true)
List<Product> searchProductsByDescriptionNative(@Param("keyword") String keyword);
}
මේ විදිහට @Query
annotation එක පාවිච්චි කරලා ඕනම සංකීර්ණ query එකක් ලියාගන්න පුළුවන්. nativeQuery = true
දැම්මොත් අපිට database specific SQL query එකක් ලියන්නත් පුළුවන්.
ඔබේ Entities වලට Repository එකක් Implement කරන්න!
දැන් ඔයාලට පැහැදිලි ඇති JPA Repositories කියන්නේ මොනවද, ඒවා කොච්චර ප්රයෝජනවත්ද කියලා. දැන් තියෙන්නේ මේ concepts ටික ඔයාලගේ project වලට apply කරන එක. මේ ලිපියේ තියෙන Product
entity එක වෙනුවට, ඔයාලා දැනට වැඩ කරන project එකක තියෙන entity එකක් අරගෙන, ඒකට අදාල repository interface එක හදලා බලන්න. Basic CRUD operations ටික try කරලා බලන්න. ඊට පස්සේ method naming conventions සහ @Query
annotation එක පාවිච්චි කරලා custom queries ලියන්න උත්සාහ කරන්න.
මේවා practice කරන තරමට තමයි ඔයාලා මේ concepts වලට හුරු වෙන්නේ. මතක තියාගන්න, coding කියන්නේ practice වලින්ම දියුණු වෙන දෙයක්!
නිගමනය (Conclusion)
JPA Repositories, විශේෂයෙන් Spring Data JPA සමඟ එකතු වුණාම, Java Backend development වලදී data persistence layer එක හදන විදිහ සම්පූර්ණයෙන්ම වෙනස් කරලා තියෙනවා. මේක අපේ code base එක clean කරනවා වගේම, development process එකත් වේගවත් කරනවා. boilerplate code අඩු කරනවා. ඒක තමයි මේකේ ලොකුම වාසිය.
ඉතින්, ඔයාලා Spring Boot project කරනවා නම්, JPA Repositories අනිවාර්යයෙන්ම පාවිච්චි කරන්න. ඒකෙන් ඔයාලගේ ජීවිතය ලේසි වෙනවා, project එකත් ඉක්මනින් ඉවර කරගන්න පුළුවන්. මේ ගැන ඔයාලට තව ප්රශ්න තියෙනවා නම්, නැත්නම් ඔයාලගේ අත්දැකීම් ගැන කියන්න ඕන නම්, පහලින් comment එකක් දාගෙන යන්න. ඒ වගේම මේ ලිපිය ඔයාලගේ යාළුවෝ අතරේ share කරන්නත් අමතක කරන්න එපා. අලුත් දෙයක් එක්ක නැවත හමුවෙමු, හැමෝටම ජය!