Java ORM परिचय (Hibernate)
In this page:
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
// 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");
}
}
Login to try C/C++/Java/PHP code in the editor
SessionFactory Setup
SessionFactory आपकी सभी mapping configurations को startup पर एक बार compile करता है और उसके बाद database connections establish करने के लिए ज़िम्मेदार होता है। यह एक heavyweight object है जिसे application में एक बार बनाया जाना चाहिए और ज़रूरत के अनुसार lightweight Session instances generate करने के लिए reuse किया जाना चाहिए।
उदाहरण: SessionFactory Setup
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");
}
}
Login to try C/C++/Java/PHP code in the editor
Persisting Objects (Save)
Hibernate आपके entity annotations के आधार पर किसी object की properties को सीधे target table के columns पर map करता है। session.save() call करना एक populated entity object लेता है और इसे एक INSERT में बदल देता है जो database में एक संबंधित row लिखता है।
उदाहरण: Persisting Objects (Save)
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);
}
}
Login to try C/C++/Java/PHP code in the editor
Fetching Objects (Get)
पहले से मौजूद एक persistent entity निकालने के लिए, session.get() का उपयोग करें, entity की class और उसका unique identifier (primary key) value पास करते हुए। Hibernate इसे एक SELECT में translate करता है और returned row से एक पूरी तरह populated object फिर से बनाता है।
उदाहरण: Fetching Objects (Get)
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);
}
}
Login to try C/C++/Java/PHP code in the editor
Managing Database Transactions
हमेशा अपने persistence operations को एक explicit transaction boundary के अंदर wrap करें। यह सुनिश्चित करता है कि संबंधित database writes का एक set या तो साथ commit हो या बीच में कुछ fail होने पर सुरक्षित रूप से roll back हो जाए, database को inconsistent half-updated state में छोड़ने के बजाय।
उदाहरण: Managing Database Transactions
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");
}
}
Login to try C/C++/Java/PHP code in the editor
session.save(...)के बाद transaction commit न करना, जिससे data कभी database में नहीं लिखा जाता।- हर operation के लिए एक नया
SessionFactoryबनाना, जबकि यह महंगा है और startup पर एक बार बनाया जाना चाहिए। - बिना
@Idfield या बिना किसी no-argument constructor के entity declare करना, जिसकी Hibernate को इसके साथ काम करने के लिए ज़रूरत है।
Chapter Quiz — Complete all 4 topics to unlock
0/4 topics done
Complete these topics first: