Entity এবং Table Mapping কি?
Entity হল একটি Java ক্লাস যা JPA এর মাধ্যমে ডাটাবেস টেবিলের সাথে ম্যাপ করা হয়। JPA ডাটাবেসের টেবিলের প্রতিটি রেকর্ডকে Entity অবজেক্ট হিসেবে রিপ্রেজেন্ট করে।
Table Mapping হল Entity ক্লাস এবং ডাটাবেস টেবিলের মধ্যে সম্পর্ক তৈরি করার প্রক্রিয়া। Spring Boot এবং JPA তে এই সম্পর্কটি @Entity এবং @Table অ্যানোটেশন ব্যবহার করে তৈরি করা হয়।
@Entity: একটি ক্লাসকে Entity হিসেবে চিহ্নিত করার জন্য ব্যবহৃত হয়।@Table: Entity ক্লাসকে নির্দিষ্ট টেবিলের সাথে মেপিং করতে ব্যবহৃত হয় (যদি টেবিলের নাম Entity ক্লাসের নামের সাথে মিলে না থাকে)।
Entity এবং Table Mapping এর উদাহরণ
ধরা যাক, আমাদের একটি Employee Entity আছে, যা ডাটাবেসে employee টেবিলের সাথে মাপ করা হবে।
১. Entity ক্লাস তৈরি করা
Employee.java (Entity Class)
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Table;
@Entity
@Table(name = "employee") // Optional if class name matches table name
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String department;
// Default constructor
public Employee() {}
// Parameterized constructor
public Employee(String name, String department) {
this.name = name;
this.department = department;
}
// 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 String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
}
এখানে, @Entity অ্যানোটেশন ক্লাসটিকে JPA Entity হিসেবে চিহ্নিত করছে এবং @Table(name = "employee") অ্যানোটেশন দ্বারা employee টেবিলের সাথে Entity ক্লাসের সম্পর্ক তৈরি করা হয়েছে।
২. Repository Interface তৈরি করা
Spring Data JPA এর মাধ্যমে Employee Entity এর জন্য JpaRepository ব্যবহার করতে হবে, যা স্বয়ংক্রিয়ভাবে CRUD অপারেশন করবে।
EmployeeRepository.java
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
// Custom query methods can be defined here
}
এখানে, JpaRepository ইন্টারফেস Employee Entity-র জন্য CRUD অপারেশন সরবরাহ করে।
৩. Spring Boot অ্যাপ্লিকেশন তৈরি করা
এখন, Spring Boot অ্যাপ্লিকেশন তৈরি করতে হবে, যেখানে EmployeeController ব্যবহার করা হবে EmployeeService এর মাধ্যমে Employee Entity থেকে ডেটা পরিচালনা করার জন্য।
EmployeeService.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class EmployeeService {
@Autowired
private EmployeeRepository employeeRepository;
public Employee saveEmployee(Employee employee) {
return employeeRepository.save(employee);
}
public Employee getEmployeeById(Long id) {
return employeeRepository.findById(id).orElse(null);
}
public List<Employee> getAllEmployees() {
return employeeRepository.findAll();
}
}
EmployeeController.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/employees")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@PostMapping
public Employee createEmployee(@RequestBody Employee employee) {
return employeeService.saveEmployee(employee);
}
@GetMapping("/{id}")
public Employee getEmployeeById(@PathVariable Long id) {
return employeeService.getEmployeeById(id);
}
@GetMapping
public List<Employee> getAllEmployees() {
return employeeService.getAllEmployees();
}
}
Application.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Spring Boot Configuration (application.properties)
application.properties
# JPA & Database Configuration
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
এখানে, application.properties ফাইলে H2 ইন-মেমরি ডাটাবেস কনফিগারেশন করা হয়েছে। আপনি যদি অন্য কোন ডাটাবেস ব্যবহার করতে চান, যেমন MySQL, PostgreSQL ইত্যাদি, তাহলে সেই অনুযায়ী কনফিগারেশন করতে হবে।
সারাংশ
Spring Boot এবং JPA এর মাধ্যমে Entity এবং Table Mapping খুব সহজে করা যায়। @Entity অ্যানোটেশন Entity ক্লাস তৈরি করার জন্য এবং @Table অ্যানোটেশন দিয়ে Entity কে ডাটাবেস টেবিলের সাথে মেপিং করা হয়। Spring Data JPA এর JpaRepository ইন্টারফেস ব্যবহার করে CRUD অপারেশন সহজে সম্পাদন করা যায়। Spring Boot কনফিগারেশন এবং JPA Entity মডেল ব্যবহার করে আপনি খুব দ্রুত এবং সহজে ডেটাবেসের সাথে কাজ করতে পারবেন।
Read more