← Back to Advanced Java Course | Chapter 8: Database & JPA | Lesson 3 of 4

Java ORM परिचय (Hibernate)

Hibernate जैसा एक ORM आपको SQL हाथ से लिखे बिना database से Java objects save और load करने देता है, जैसे दो languages के बीच एक translator। आपके objects किसी table की rows बन जाते हैं।

Entity Declarations

Object-Relational Mapping (ORM) Java objects और relational database tables के बीच की खाई को पाटता है। आप अपनी domain classes को @Entity और @Id जैसे annotations से चिह्नित करते हैं ताकि Hibernate को पता चले कि कौन सी class किस table से map होती है और कौन सा field primary key है, बिना आपको वह mapping SQL हाथ से लिखे।

उदाहरण: Entity Declarations

java
// Import javax.persistence.* so it can be used by its short name
import javax.persistence.*;
// Define the class Main
public class Main {
	@Entity
	// Define the class User
	static class User {
		@Id
		private Long id;
		private String name;
	}
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		System.out.println("@Entity and @Id map this class to a table, without hand-written mapping SQL");
	}
}

SessionFactory Setup

SessionFactory आपकी सभी mapping configurations को startup पर एक बार compile करता है और उसके बाद database connections establish करने के लिए ज़िम्मेदार होता है। यह एक heavyweight object है जिसे application में एक बार बनाया जाना चाहिए और ज़रूरत के अनुसार lightweight Session instances generate करने के लिए reuse किया जाना चाहिए।

उदाहरण: SessionFactory Setup

java
public class Main {
	// SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
	// created once per application, heavyweight, reused to open lightweight Sessions
	public static void main(String[] args) {
		System.out.println("SessionFactory compiles mappings once at startup");
	}
}

Persisting Objects (Save)

Hibernate आपके entity annotations के आधार पर किसी object की properties को सीधे target table के columns पर map करता है। session.save() call करना एक populated entity object लेता है और इसे एक INSERT में बदल देता है जो database में एक संबंधित row लिखता है।

उदाहरण: Persisting Objects (Save)

java
public class Main {
	static class User { Long id; String name; }
	public static void main(String[] args) {
		User user = new User();
		user.name = "Riya";
		// session.save(user); // Hibernate converts this into an INSERT
		System.out.println("Saving " + user.name);
	}
}

Fetching Objects (Get)

पहले से मौजूद एक persistent entity निकालने के लिए, session.get() का उपयोग करें, entity की class और उसका unique identifier (primary key) value पास करते हुए। Hibernate इसे एक SELECT में translate करता है और returned row से एक पूरी तरह populated object फिर से बनाता है।

उदाहरण: Fetching Objects (Get)

java
public class Main {
	static class User { Long id; String name; }
	public static void main(String[] args) {
		Long id = 1L;
		// User user = session.get(User.class, id); // translated into a SELECT
		System.out.println("Fetching User with id " + id);
	}
}

Managing Database Transactions

हमेशा अपने persistence operations को एक explicit transaction boundary के अंदर wrap करें। यह सुनिश्चित करता है कि संबंधित database writes का एक set या तो साथ commit हो या बीच में कुछ fail होने पर सुरक्षित रूप से roll back हो जाए, database को inconsistent half-updated state में छोड़ने के बजाय।

उदाहरण: Managing Database Transactions

java
public class Main {
	public static void main(String[] args) {
		// Transaction tx = session.beginTransaction();
		// try {
		//     session.save(new User());
		//     tx.commit();
		// } catch (Exception e) {
		//     tx.rollback(); // rolls back all related writes together
		// }
		System.out.println("Wrap persistence operations in an explicit transaction boundary");
	}
}
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. session.save(...) के बाद transaction commit न करना, जिससे data कभी database में नहीं लिखा जाता।
  2. हर operation के लिए एक नया SessionFactory बनाना, जबकि यह महंगा है और startup पर एक बार बनाया जाना चाहिए।
  3. बिना @Id field या बिना किसी no-argument constructor के entity declare करना, जिसकी Hibernate को इसके साथ काम करने के लिए ज़रूरत है।
🔒

Chapter Quiz — Complete all 4 topics to unlock

0/4 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.