0% found this document useful (0 votes)
8 views12 pages

Understanding ORM and Hibernate Basics

Uploaded by

arbayezid0
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views12 pages

Understanding ORM and Hibernate Basics

Uploaded by

arbayezid0
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Object Relational Mapping (ORM) and how it works

Answer:​
Object Relational Mapping (ORM) is a programming technique used to map the relationship
between object-oriented programming languages (like Java) and relational databases. Normally,
relational databases store data in tables consisting of rows and columns, whereas Java
applications represent data in classes and objects. ORM bridges this gap.

ORM frameworks like Hibernate allow developers to work with objects without worrying about
writing SQL queries manually. Each Java class maps to a database table, each object instance
corresponds to a table row, and object fields map to table columns. When a developer saves an
object, the ORM automatically generates and executes the SQL INSERT statement. When
fetching objects, the ORM executes the SQL SELECT query and converts rows into objects.

Working steps:

1.​ Developer defines classes (entities).​

2.​ ORM maps these entities to DB tables using annotations or XML.​

3.​ Developer performs CRUD operations on objects.​

4.​ ORM framework automatically translates them into SQL queries.​

This abstraction layer saves time, reduces errors, and makes applications more maintainable.

2. Advantages and disadvantages of using ORM frameworks like Hibernate

Answer:​
ORM frameworks like Hibernate provide many benefits, but also come with some trade-offs.

Advantages:

1.​ Productivity: Developers do not need to write long and repetitive SQL queries. They
can simply work with Java objects.​

2.​ Database independence: Hibernate supports multiple databases. Applications can


switch from MySQL to Oracle with minimal changes.​
3.​ Caching and performance: Hibernate uses caching to reduce database hits, improving
efficiency.​

4.​ Transaction and connection management: Developers do not need to handle JDBC
connections manually. Hibernate manages transactions internally.​

5.​ Faster development: As less boilerplate code is written, projects can be completed
quickly.​

Disadvantages:

1.​ Performance overhead: Hibernate is slower than raw SQL for complex queries
because of abstraction.​

2.​ Learning curve: Understanding Hibernate concepts (sessions, transactions, caching,


mappings) is challenging.​

3.​ Debugging difficulty: The SQL generated by Hibernate may be complex and harder to
debug.​

4.​ Not ideal for high-performance systems: For real-time or extremely


performance-critical systems, Hibernate may not be the best option.​

3. Workflow of dirty checking in Hibernate. How does it ensure that only


modified entities are updated?

Answer:​
Hibernate has a powerful feature called dirty checking that automatically detects changes in
persistent objects.

When an entity object is loaded into the persistence context (session), Hibernate keeps a
snapshot of its initial state. During a transaction, if the developer modifies some fields of the
object (e.g., changing an employee’s salary), Hibernate compares the object’s current state with
its snapshot when the transaction is committed.

If Hibernate finds that one or more fields have changed, it marks the object as dirty. At commit
time, Hibernate generates an UPDATE SQL query only for the modified entities. If no changes
are found, no UPDATE query is issued.

Example:
Employee emp = [Link]([Link], 1);
[Link](50000); // changed salary
[Link](); // Hibernate updates only salary field

Benefit: This mechanism avoids unnecessary database updates, improves performance, and
ensures data integrity.

4. Role of the Persistence Context in Hibernate

Answer:​
The persistence context is the core of Hibernate’s working. It is a memory cache maintained by
Hibernate where all entity objects are stored and managed during a session.

Roles:

1.​ Identity guarantee: Ensures that only one Java object represents one database row at
a time. If you fetch the same row multiple times, Hibernate returns the same object
instance.​

2.​ Change tracking: Hibernate keeps a snapshot of objects in the persistence context,
which allows dirty checking.​

3.​ First-level cache: Objects stored in the persistence context are cached, reducing the
need for multiple database queries.​

4.​ Transaction management: Entities remain managed until the session ends. After the
session closes, objects become detached.​

This context is the reason Hibernate can automatically synchronize Java objects with the
database.

5. Define an I/O Stream in Java. Differentiate between input and output


streams with examples

Answer:​
In Java, an I/O stream is a flow of data from a source to a destination. Streams make it
possible to read and write data, whether from files, network, or memory.
Input Stream: Used to read data from a source. It represents data flowing into the program.​
Example:​

FileInputStream fin = new FileInputStream("[Link]");
int data = [Link](); // reads one byte

●​

Output Stream: Used to write data to a destination. It represents data flowing out of the
program.​
Example:​

FileOutputStream fout = new FileOutputStream("[Link]");
[Link](65); // writes 'A'

●​

Difference: Input stream = reading, Output stream = writing.

6. Process-based and thread-based multitasking

Answer:​
Multitasking means executing multiple tasks at the same time. In Java, there are two main
types:

1.​ Process-based multitasking: Multiple independent programs (processes) run


simultaneously. For example, opening MS Word and a web browser at the same time.
Each process has its own memory space and resources.​

2.​ Thread-based multitasking: Multiple threads of the same program run simultaneously.
For example, in a text editor, one thread handles typing, another checks spelling in the
background. Threads share the same memory but execute independently.​

Comparison: Processes are heavyweight and need more resources, while threads are
lightweight and faster to create and manage.

7. Thread life-cycle states

Answer:​
A thread in Java goes through several states in its lifecycle:
1.​ New: When a thread object is created using new Thread().​

2.​ Runnable: After calling start(), the thread is ready to run but waiting for CPU
scheduling.​

3.​ Running: When the thread scheduler selects the thread, it executes the run() method.​

4.​ Waiting: Thread is waiting indefinitely for another thread to signal it.​

5.​ Timed Waiting: Thread waits for a specified time, e.g., sleep() or join(timeout).​

6.​ Terminated: After completing execution, the thread dies.​

This cycle helps the JVM manage thread execution efficiently.

8. How deadlocks are created and ways to handle them

Answer:​
Deadlock creation: A deadlock occurs when two or more threads wait for each other’s
resources indefinitely. Example:

●​ Thread 1 locks resource A and waits for B.​

●​ Thread 2 locks resource B and waits for A.​


Both threads keep waiting forever → deadlock.​

Ways to handle deadlock:

1.​ Avoid nested locks: Try not to lock multiple resources at the same time.​

2.​ Use lock ordering: Always acquire locks in the same order to prevent circular wait.​

3.​ Use timeout with tryLock(): If a lock cannot be acquired within a certain time, the
thread gives up.​

4.​ Deadlock detection: Periodically check for circular waits and take action (e.g., killing
one thread).​

Deadlocks must be handled properly, otherwise applications may freeze.


9. Dependency Injection and its types

Answer:​
Dependency Injection (DI) is a design pattern where objects are provided with their
dependencies rather than creating them inside. It ensures loose coupling between
components.

Types of DI:

1.​ Constructor Injection: Dependencies are passed through the class constructor.​

2.​ Setter Injection: Dependencies are provided through setter methods.​

3.​ Field Injection: Dependencies are injected directly into class fields using annotations
(e.g., @Autowired).​

DI is widely used in the Spring Framework to make applications modular and testable.

10. Spring Beans and significance of bean definition

Answer:​
A Spring Bean is a Java object that is managed by the Spring IoC container. Beans are
created, configured, and managed by the container, not by the developer.

Significance of Bean Definition:

●​ Defines how a bean should be instantiated (constructor, factory, etc.).​

●​ Specifies bean scope (singleton, prototype, etc.).​

●​ Manages bean lifecycle (initialization and destruction).​

●​ Provides dependency injection configuration.​

Without bean definitions, Spring would not know how to create and wire beans properly.
📘 Bangla (Long Answers for 4 Marks)
1. Object Relational Mapping (ORM) এবং এটি কিভাবে কাজ করে

উত্তর:​
Object Relational Mapping (ORM) হলো একটি প্রোগ্রামিং টেকনিক যা অবজেক্ট-ওরিয়েন্টেড প্রোগ্রামিং ল্যাঙ্গুয়েজ
(যেমন Java) এবং relational database-এর মধ্যে সম্পর্ক তৈরি করে। সাধারণত relational database-এ ডেটা
টেবিল আকারে (row এবং column) রাখা হয়, কিন্তু Java অ্যাপ্লিকেশন ডেটা ক্লাস এবং অবজেক্ট আকারে উপস্থাপন
করে। ORM এই দুইয়ের মধ্যে সেতু বন্ধন করে।

Hibernate-এর মতো ORM ফ্রেমওয়ার্ক ব্যবহার করলে ডেভেলপারদের আর ম্যানুয়ালি SQL কোয়েরি লিখতে হয় না।
প্রতিটি Java ক্লাস একটি ডাটাবেস টেবিলের সাথে, প্রতিটি অবজেক্ট একটি রো-এর সাথে, এবং অবজেক্টের ফিল্ডগুলো
টেবিল কলামের সাথে ম্যাপ হয়। যখন ডেভেলপার অবজেক্ট সেভ করেন, ORM স্বয়ংক্রিয়ভাবে INSERT কুয়েরি তৈরি
করে। যখন অবজেক্ট ফেচ করা হয়, ORM SELECT কুয়েরি চালিয়ে রো-কে অবজেক্টে রূপান্তর করে।

কাজ করার ধাপ:

1.​ ডেভেলপার entity ক্লাস তৈরি করে।​

2.​ ORM entity গুলোকে টেবিলের সাথে ম্যাপ করে (annotation বা XML দ্বারা)।​

3.​ ডেভেলপার অবজেক্টের উপর CRUD অপারেশন করে।​

4.​ ORM স্বয়ংক্রিয়ভাবে সেগুলোকে SQL কোয়েরিতে রূপান্তর করে।​

এই abstraction লেয়ার ডেভেলপমেন্টের সময় বাঁচায়, ভু ল কমায় এবং অ্যাপ্লিকেশনকে maintainable করে।

2. Hibernate-এর মতো ORM ব্যবহারের সুবিধা ও অসুবিধা

উত্তর:

সুবিধা:

1.​ Productivity: ডেভেলপারদের বারবার লম্বা SQL কোয়েরি লিখতে হয় না। শুধু অবজেক্ট ব্যবহার করলেই
চলে।​

2.​ Database independence: Hibernate একাধিক ডাটাবেস সাপোর্ট করে। MySQL থেকে Oracle-এ
পরিবর্ত ন করলে খুব কম পরিবর্ত ন দরকার হয়।​

3.​ Caching ও Performance: Hibernate caching ব্যবহার করে ডাটাবেস হিট কমায়, ফলে গতি বাড়ে।​
4.​ Transaction ও Connection Management: JDBC connection ম্যানেজ করতে হয় না,
Hibernate নিজেই ট্রানজ্যাকশন হ্যান্ডেল করে।​

5.​ Faster development: কম boilerplate কোড লিখতে হয়, ফলে দ্রুত ডেভেলপমেন্ট সম্ভব।​

অসুবিধা:

1.​ Performance overhead: Hibernate raw SQL এর তু লনায় ধীর কাজ করে কারণ এতে abstraction
থাকে।​

2.​ Learning curve: Hibernate-এর concepts (session, transaction, caching, mapping) বোঝা
কঠিন।​

3.​ Debugging difficulty: Hibernate-generated SQL প্রায়শই জটিল হয় এবং debug করা কষ্টকর।​

4.​ High-performance system-এর জন্য উপযুক্ত নয়: real-time বা অত্যন্ত পারফরম্যান্স দরকার এমন
সিস্টেমে Hibernate ভাল কাজ নাও করতে পারে।​

3. Hibernate-এ Dirty Checking Workflow এবং কিভাবে শুধুমাত্র পরিবর্তি ত entity


update হয়

উত্তর:​
Hibernate-এর একটি শক্তিশালী ফিচার হলো dirty checking। এটি স্বয়ংক্রিয়ভাবে persistent object-এর
পরিবর্ত ন শনাক্ত করে।

যখন একটি entity অবজেক্ট persistence context (session)-এ লোড হয়, Hibernate এর একটি snapshot
সংরক্ষণ করে। ট্রানজ্যাকশন চলাকালীন যদি ডেভেলপার অবজেক্টের কিছু ফিল্ড পরিবর্ত ন করে (যেমন কর্মচারীর বেতন
পরিবর্ত ন করা), Hibernate ট্রানজ্যাকশন commit হওয়ার সময় বর্ত মান অবস্থা এবং snapshot তু লনা করে।

যদি Hibernate পরিবর্ত ন খুজে


ঁ পায়, অবজেক্টকে dirty হিসেবে মার্ক করে। commit সময় Hibernate শুধুমাত্র
পরিবর্তি ত entity-এর জন্য UPDATE SQL কুয়েরি জেনারেট করে। পরিবর্ত ন না থাকলে কোন update হয় না।

উদাহরণ:

Employee emp = [Link]([Link], 1);


[Link](50000); // বেতন পরিবর্ত ন
[Link](); // Hibernate শুধু salary ফিল্ড update করে

সুবিধা: এটি অপ্রয়োজনীয় update এড়ায়, পারফরম্যান্স বাড়ায় এবং data integrity বজায় রাখে।
4. Hibernate-এ Persistence Context-এর ভূ মিকা

উত্তর:​
Persistence context হলো Hibernate-এর মূল অংশ। এটি একটি মেমোরি cache, যেখানে session চলাকালীন
entity object গুলো রাখা ও ম্যানেজ করা হয়।

ভূ মিকা:

1.​ Identity guarantee: একটি ডাটাবেস row-এর জন্য সবসময় একটি মাত্র Java অবজেক্ট থাকে। একই
row একাধিকবার ফেচ করলে Hibernate একই অবজেক্ট instance দেয়।​

2.​ Change tracking: Hibernate persistence context-এ snapshot রাখে, যা dirty checking সক্ষম
করে।​

3.​ First-level cache: persistence context caching ব্যবহার করে ডাটাবেস query কমায়।​

4.​ Transaction management: entity গুলো session শেষ না হওয়া পর্যন্ত managed থাকে। session
বন্ধ হলে object detached হয়।​

Hibernate-এর এই context এর কারণেই Java object স্বয়ংক্রিয়ভাবে database-এর সাথে sync হয়।

5. Java I/O Stream সংজ্ঞা ও Input/Output Stream এর পার্থক্য উদাহরণসহ

উত্তর:​
Java-তে I/O stream মানে data একটি source থেকে একটি destination-এ প্রবাহিত হওয়া। stream ব্যবহার
করে ফাইল, নেটওয়ার্ক বা মেমোরি থেকে ডেটা পড়া এবং লেখা সম্ভব।

Input Stream: source থেকে data পড়ার জন্য ব্যবহৃত হয়। এটি data প্রোগ্রামে প্রবেশ করায়।​
উদাহরণ:​

FileInputStream fin = new FileInputStream("[Link]");
int data = [Link](); // এক বাইট পড়ে

●​

Output Stream: destination-এ data লেখার জন্য ব্যবহৃত হয়। এটি data প্রোগ্রাম থেকে বের করে দেয়।​
উদাহরণ:​

FileOutputStream fout = new FileOutputStream("[Link]");
[Link](65); // 'A' লিখবে
●​

পার্থক্য: Input stream = data পড়া, Output stream = data লেখা।

6. Process-based এবং Thread-based Multitasking

উত্তর:​
Multitasking মানে একসাথে একাধিক কাজ চালানো। Java-তে এটি দুইভাবে হয়:

1.​ Process-based multitasking: একাধিক আলাদা প্রোগ্রাম (process) একসাথে চলে। যেমন একই সময়ে
MS Word এবং একটি ব্রাউজার খোলা। প্রতিটি process-এর নিজস্ব মেমোরি ও রিসোর্স থাকে।​

2.​ Thread-based multitasking: একটি প্রোগ্রামের ভেতরে একাধিক কাজ (thread) একই সাথে চলে।
যেমন একটি text editor-এ typing করার সময় spell checking চলে। Thread একই মেমোরি শেয়ার করে
কিন্তু আলাদা কাজ চালায়।​

Comparison: Process ভারী এবং বেশি রিসোর্স লাগে, Thread হালকা এবং দ্রুত।

7. Thread Life-cycle States

উত্তর:​
Java-তে একটি thread তার লাইফসাইকেলে কয়েকটি ধাপের মধ্য দিয়ে যায়:

1.​ New: new Thread() দিয়ে অবজেক্ট তৈরি হলে।​

2.​ Runnable: start() কল করার পর thread রান করার জন্য প্রস্তুত থাকে কিন্তু CPU scheduling-এর
জন্য অপেক্ষা করে।​

3.​ Running: CPU scheduler thread-কে চালানোর জন্য বেছে নিলে run() মেথড execute হয়।​

4.​ Waiting: Thread অনির্দি ষ্ট সময়ের জন্য অপেক্ষা করে অন্য thread-এর signal এর জন্য।​

5.​ Timed Waiting: Thread নির্দি ষ্ট সময়ের জন্য অপেক্ষা করে (যেমন sleep() বা
join(timeout))।​

6.​ Terminated: Execution শেষ হলে thread মারা যায়।​

এই states JVM-কে thread execution দক্ষতার সাথে ম্যানেজ করতে সাহায্য করে।
8. Deadlock কিভাবে সৃষ্টি হয় এবং কিভাবে হ্যান্ডেল করা যায়

উত্তর:​
Deadlock সৃষ্টি: Deadlock ঘটে যখন দুই বা তার বেশি thread একে অপরের resource-এর জন্য অনন্তকাল
অপেক্ষা করে। উদাহরণ:

●​ Thread 1 resource A লক করে এবং resource B এর জন্য অপেক্ষা করে।​

●​ Thread 2 resource B লক করে এবং resource A এর জন্য অপেক্ষা করে।​


ফলে উভয় thread অপেক্ষা করতে থাকে এবং আর কাজ এগোয় না।​

Deadlock হ্যান্ডেল করার উপায়:

1.​ Nested lock এড়ানো: একসাথে একাধিক resource লক করা থেকে বিরত থাকা।​

2.​ Lock ordering ব্যবহার: সব thread একই ক্রমে resource লক করলে circular wait হবে না।​

3.​ Timeout সহ tryLock(): নির্দি ষ্ট সময়ের মধ্যে লক না পাওয়া গেলে thread চেষ্টা ছেড়ে দেয়।​

4.​ Deadlock detection: সময়ে সময়ে circular wait চেক করে একটি thread বন্ধ করে দেওয়া।​

Deadlock সঠিকভাবে হ্যান্ডেল না করলে প্রোগ্রাম স্থবির হয়ে যায়।

9. Dependency Injection এবং এর ধরন

উত্তর:​
Dependency Injection (DI) হলো একটি design pattern যেখানে কোনো ক্লাসের dependency (অন্য object)
বাইরে থেকে সরবরাহ করা হয়, ভেতরে তৈরি করা হয় না। এতে ক্লাসগুলোর মধ্যে loose coupling হয়।

Dependency Injection এর ধরন:

1.​ Constructor Injection: dependency constructor এর মাধ্যমে দেয়া হয়।​

2.​ Setter Injection: dependency setter মেথডের মাধ্যমে দেয়া হয়।​

3.​ Field Injection: dependency সরাসরি field-এ দেয়া হয় annotation (@Autowired) ব্যবহার
করে।​
Spring Framework এ DI বহুল ব্যবহৃত হয় যাতে অ্যাপ্লিকেশন modular ও testable হয়।

10. Spring Bean এবং Bean Definition-এর গুরুত্ব

উত্তর:​
Spring Bean হলো একটি Java object যা Spring IoC container দ্বারা পরিচালিত হয়। Bean container
অবজেক্ট তৈরি, configure এবং lifecycle ম্যানেজ করে।

Bean Definition-এর গুরুত্ব:

●​ Bean কিভাবে তৈরি হবে (constructor, factory ইত্যাদি) তা নির্ধারণ করে।​

●​ Bean এর scope (singleton, prototype) নির্ধারণ করে।​

●​ Bean এর lifecycle (initialize এবং destroy) নিয়ন্ত্রণ করে।​

●​ Dependency injection কনফিগার করে।​

Bean definition ছাড়া Spring container জানত না কিভাবে Bean তৈরি ও ম্যানেজ করতে হবে।

You might also like