← Back to Advanced Java Course | Chapter 10: Spring Boot | Lesson 3 of 6

Java Spring Boot का Data JPA

Spring Data JPA आपको लगभग बिना कोई code लिखे database में जानकारी save और find करने देता है, जैसे कोई helper जो आपके कागज़ात फाइल कर देता है। आप बताते हैं कि आपको क्या चाहिए, और यह बाकी काम कर देता है।
Syntax
java
@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?

java
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");
	}
}

Repository CRUD Methods

Spring Data JPA repository interfaces में save(), findById(), और delete() जैसे operations built-in होकर आते हैं, सिर्फ JpaRepository जैसे interface को extend करके -- इन आम cases के लिए किसी implementation code की ज़रूरत नहीं।

उदाहरण: Repository CRUD Methods

java
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");
	}
}

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

java
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");
	}
}

JPA Transactions

Transactions सुनिश्चित करते हैं कि संबंधित database operations का एक set या तो साथ सफल हो या कोई एक fail होने पर पूरी तरह roll back हो जाए, आपके data को consistent रखते हुए। किसी service method को @Transactional से annotate करें ताकि Spring उसके आस-पास transaction boundary को अपने आप manage करे।

उदाहरण: JPA Transactions

java
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");
	}
}

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

java
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);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. findByNmae जैसी derived query में किसी property की गलत spelling लिखना, जिससे application startup पर fail हो जाता है क्योंकि Spring को वह property नहीं मिलती।
  2. पहले page के लिए page number 1 पास करना, जबकि PageRequest.of(page, size) 0 से शुरू होता है।
  3. findById को entity return करता हुआ मानना, जबकि यह एक Optional return करता है जो खाली हो सकता है।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.