Hibernate
Hibernate
session.beginTransaction();
Session.save(st);
Session.getTransaction().commit();
Fis.read();
Ad.setImage(data);
FETCHING DATA
Get returns null when object is not found in cache as well as db while load method throws
ObjectNotFoundException if object is not found on cache as well as db but never returns null.
Use get when you are not sure object is avialabe or not
@Embeddable
Class certificate{
String course;
String duration;}
@Entity
Class student{
Int id;
String name;
## **Basics of Hibernate**
**Answer:** Hibernate is an Object-Relational Mapping (ORM) tool for Java that simplifies database
interactions by mapping Java objects to database tables. Unlike JDBC, Hibernate provides high-level
APIs for database access, reduces boilerplate code, and includes features like caching, lazy loading,
and transaction management.
### 2. Explain the advantages and disadvantages of Hibernate.
**Advantages:**
**Disadvantages:**
### 3. What is ORM (Object-Relational Mapping), and how does Hibernate implement it?
**Answer:** ORM is a programming technique that maps Java objects to database tables. Hibernate
implements ORM by using mapping files or annotations to establish the relationship between Java
classes and database tables.
**Answer:**
---
## **Configuration**
**Answer:**
- Example:
```xml
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydb</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
```
**Answer:** This file contains database configurations and mappings required by Hibernate. It
typically includes properties like dialect, connection URL, username, password, and mapping files.
### 8. Can you use Hibernate without an XML configuration file?
**Answer:** Yes, Hibernate can be configured programmatically using the `Configuration` class:
```java
configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
```
**Answer:** Annotate entity classes using annotations like `@Entity`, `@Table`, and `@Id`. Example:
```java
@Entity
@Table(name = "users")
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "name")
```
## **Mappings**
**Answer:**
- One-to-One
- One-to-Many
- Many-to-One
- Many-to-Many
```java
@Entity
@Table(name = "employee")
@Id
@GeneratedValue
```
### 13. What is the difference between `@OneToMany` and `@ManyToOne` annotations?
**Answer:**
### 14. What is the role of the `@JoinTable` and `@JoinColumn` annotations?
**Answer:**
- `@JoinColumn`: Specifies the foreign key column for one-to-one or many-to-one relationships.
- **Single Table Strategy:** All classes in a hierarchy are mapped to a single table.
- **Table per Class Strategy:** Each class has its own table.
- **Joined Strategy:** Each class has its table, and relationships are joined.
---
### 20. What is a Hibernate `Session`, and what are its main methods?
**Answer:**
```java
Transaction tx = session.beginTransaction();
try {
// Perform operations
tx.commit();
} catch (Exception e) {
tx.rollback();
```
### 23. What is the difference between `save()` and `persist()` methods?
**Answer:**
- `persist()`: Does not return the ID and must be used inside a transaction.
### 24. Explain the difference between `update()` and `merge()`.
**Answer:**
---
## **Querying**
### 25. What are the different ways to query a database using Hibernate?
**Answer:**
- HQL
- Criteria API
- Native SQL
### 26. What is HQL (Hibernate Query Language), and how is it different from SQL?
**Answer:** HQL is an object-oriented query language similar to SQL but works with entity objects
instead of database tables.
---
Would you like detailed solutions for specific queries or more sections added here?