Java Spring Boot का Data JPA
In this page:
@Entity
class EntityName {
@Id
private Long id;
}
interface EntityRepository extends JpaRepository<EntityName, Long> { }
What is Spring Boot Data JPA?
Spring Data JPA Hibernate के ऊपर एक abstraction layer के रूप में काम करके Spring applications में database access को काफी सरल बनाता है। यह basic CRUD operations के लिए आप हाथ से जो boilerplate code लिखते उसमें से ज़्यादातर खत्म कर देता है, आपको repetitive query code के बजाय अपने असली data model पर ध्यान देने देते हुए।
उदाहरण: What is Spring Boot Data JPA?
import org.springframework.data.jpa.repository.JpaRepository;
public class Main {
static class User { Long id; String name; }
interface UserRepository extends JpaRepository<User, Long> {} // no implementation code required
public static void main(String[] args) {
System.out.println("Spring Data JPA eliminates hand-written CRUD boilerplate");
}
}
Login to try C/C++/Java/PHP code in the editor
Repository CRUD Methods
Spring Data JPA repository interfaces में save(), findById(), और delete() जैसे operations built-in होकर आते हैं, सिर्फ JpaRepository जैसे interface को extend करके -- इन आम cases के लिए किसी implementation code की ज़रूरत नहीं।
उदाहरण: Repository CRUD Methods
import org.springframework.data.jpa.repository.JpaRepository;
public class Main {
static class User { Long id; String name; }
interface UserRepository extends JpaRepository<User, Long> {}
public static void main(String[] args) {
// userRepository.save(user); userRepository.findById(1L); userRepository.delete(user);
System.out.println("save(), findById(), delete() come built in");
}
}
Login to try C/C++/Java/PHP code in the editor
Custom Query Methods
built-in methods के अलावा, आप Spring के method-name convention का पालन करके (जैसे findByLastNameAndAgeGreaterThan) या उन cases के लिए जो naming convention express नहीं कर सकता @Query annotation से एक explicit query लिखकर custom query methods declare कर सकते हैं।
उदाहरण: Custom Query Methods
import org.springframework.data.jpa.repository.*;
import java.util.List;
public class Main {
static class User { String lastName; int age; }
interface UserRepository extends JpaRepository<User, Long> {
List<User> findByLastNameAndAgeGreaterThan(String lastName, int age); // derived from the method name
}
public static void main(String[] args) {
System.out.println("Query derived automatically from the method name");
}
}
Login to try C/C++/Java/PHP code in the editor
JPA Transactions
Transactions सुनिश्चित करते हैं कि संबंधित database operations का एक set या तो साथ सफल हो या कोई एक fail होने पर पूरी तरह roll back हो जाए, आपके data को consistent रखते हुए। किसी service method को @Transactional से annotate करें ताकि Spring उसके आस-पास transaction boundary को अपने आप manage करे।
उदाहरण: JPA Transactions
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
public class Main {
@Service
static class UserService {
@Transactional // all writes inside commit together, or all roll back
void createUser(String name) {
System.out.println("Creating " + name + " inside a managed transaction");
}
}
public static void main(String[] args) {
new UserService().createUser("Riya");
}
}
Login to try C/C++/Java/PHP code in the editor
Pagination and Sorting
Spring Data JPA, Pageable और Sort types के ज़रिए, बड़े result sets को एक समय में एक हिस्सा pages में देखने और उन्हें किसी भी field से dynamically sort करने के लिए साधारण built-in utilities प्रदान करता है, बिना आपके manual LIMIT/OFFSET SQL लिखे।
उदाहरण: Pagination and Sorting
import org.springframework.data.domain.*;
public class Main {
public static void main(String[] args) {
Pageable pageable = PageRequest.of(0, 10, Sort.by("name")); // page 0, 10 per page, sorted by name
System.out.println(pageable);
}
}
Login to try C/C++/Java/PHP code in the editor
findByNmaeजैसी derived query में किसी property की गलत spelling लिखना, जिससे application startup पर fail हो जाता है क्योंकि Spring को वह property नहीं मिलती।- पहले page के लिए page number 1 पास करना, जबकि
PageRequest.of(page, size)0 से शुरू होता है। findByIdको entity return करता हुआ मानना, जबकि यह एकOptionalreturn करता है जो खाली हो सकता है।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: