Hibernate

0.0(0)
studied byStudied by 0 people
full-widthCall with Kai
GameKnowt Play
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/61

flashcard set

Earn XP

Description and Tags

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

62 Terms

1
New cards

What is Hibernate?

An open-source Object-Relational Mapping (ORM) framework for Java.

2
New cards

What is the main purpose of Hibernate?

To map Java objects to relational database tables and manage persistence.

3
New cards

Is Hibernate a JPA implementation?

Yes, it is one of the most popular JPA providers.

4
New cards

What is the core interface in Hibernate?

Session

5
New cards

What is a Hibernate Session?

An interface for interacting with the database and managing persistent objects.

6
New cards

How do you obtain a Hibernate Session?

From a SessionFactory

7
New cards

What is a SessionFactory in Hibernate?

A factory for Session objects, created once per application.

8
New cards

What is the configuration file used by Hibernate?

hibernate.cfg.xml

9
New cards

Which file defines database connection properties for Hibernate?

hibernate.cfg.xml or persistence.xml (if using JPA)

10
New cards

What is HQL?

Hibernate Query Language, similar to JPQL but specific to Hibernate.

11
New cards

How is HQL different from SQL?

HQL operates on entity objects and their properties, not tables and columns.

12
New cards

What is the purpose of @Entity in Hibernate?

Marks a class as a persistent entity mapped to a database table.

13
New cards

What does @Table(name = "table_name") do in Hibernate?

Specifies the name of the table to which the entity is mapped.

14
New cards

What is the use of @Id in Hibernate?

It specifies the primary key of an entity.

15
New cards

What is the purpose of @GeneratedValue?

Defines the strategy for primary key generation.

16
New cards

What are the key generation strategies in Hibernate?

AUTO, IDENTITY, SEQUENCE, TABLE

17
New cards

What is hibernate.dialect?

A configuration property that tells Hibernate how to generate SQL for a specific database.

18
New cards

What does hibernate.show_sql do?

Logs the generated SQL statements to the console.

19
New cards

What does hibernate.hbm2ddl.auto control?

It controls schema generation: values include update, create, validate, none, etc.

20
New cards

What is lazy loading in Hibernate?

Objects are not fetched from the database until they are accessed.

21
New cards

What is eager loading in Hibernate?

Objects and their relationships are fetched immediately.

22
New cards

How do you control fetch type in Hibernate?

Using fetch = FetchType.LAZY or FetchType.EAGER in relationship annotations.

23
New cards

What is @OneToMany used for?

To map one-to-many relationships between entities.

24
New cards

What is @ManyToOne used for?

To define many-to-one relationships in entity mapping.

25
New cards

What is a proxy object in Hibernate?

A placeholder object used for lazy loading until the actual data is fetched.

26
New cards

What is the first-level cache in Hibernate?

A mandatory cache associated with the Session object.

27
New cards

What is the second-level cache in Hibernate?

An optional shared cache across sessions managed by SessionFactory.

28
New cards

Name common second-level cache providers.

Ehcache, Infinispan, Redis, Hazelcast.

29
New cards

What is query cache in Hibernate?

A cache for storing query result sets to improve performance.

30
New cards

How do you enable second-level cache in Hibernate?

Via hibernate.cfg.xml and appropriate annotations like @Cache.

31
New cards

What is cascading in Hibernate?

The ability to propagate operations (persist, merge, remove, etc.) from one entity to its relationships.

32
New cards

What is the difference between save() and persist() in Hibernate?

save() returns ID and can be used outside transactions; persist() is JPA-compliant and works within transactions.

33
New cards

What is the difference between merge() and update() in Hibernate?

merge() copies changes to a managed instance; update() reattaches a detached object.

34
New cards

What is a detached object in Hibernate?

An entity that is no longer associated with an active Hibernate session.

35
New cards

What does flush() do in Hibernate?

Synchronizes the session state with the database.

36
New cards

What is clear() in Hibernate?

Removes all entities from the session cache.

37
New cards

What is evict() in Hibernate?

Removes a specific object from the session cache.

38
New cards

What is optimistic locking in Hibernate?

Prevents lost updates using version control (e.g., @Version annotation).

39
New cards

What is pessimistic locking in Hibernate?

Prevents concurrent access using database-level locks.

40
New cards

What is @Version used for in Hibernate?

To implement optimistic locking with a version column.

41
New cards

How can you execute native SQL in Hibernate?

Using createNativeQuery() method from Session or EntityManager.

42
New cards

What is a Named Query in Hibernate?

A statically defined HQL or SQL query annotated with @NamedQuery or @NamedNativeQuery.

43
New cards

What is the purpose of Criteria API in Hibernate?

To create object-oriented, type-safe queries without writing HQL or SQL.

44
New cards

What replaced Criteria API in JPA 2.0?

JPA Criteria API, which is more type-safe and strongly typed.

45
New cards

What is the Hibernate Validator?

A framework that integrates Bean Validation (JSR 380) into Hibernate entities.

46
New cards

How do you define constraints in Hibernate Validator?

Using annotations like @NotNull, @Size, @Min, @Email, etc.

47
New cards

What is @NaturalId in Hibernate?

Used to define a unique business key alternative to the primary key.

48
New cards

Can Hibernate map composite primary keys?

Yes, using @EmbeddedId or @IdClass.

49
New cards

What is an embeddable class in Hibernate?

A reusable value object annotated with @Embeddable and used with @Embedded.

50
New cards

What is batch processing in Hibernate?

Processing multiple entities in batches to improve performance.

51
New cards

How do you configure batch size in Hibernate?

Using @BatchSize(size = N) or hibernate.jdbc.batch_size property.

52
New cards

What is N+1 Select problem in Hibernate?

A performance issue where fetching a collection results in one query for the parent and N queries for the children.

53
New cards

How do you avoid N+1 problem in Hibernate?

By using JOIN FETCH, batch fetching, or entity graphs.

54
New cards

What is dirty checking in Hibernate?

Automatic detection and synchronization of changed fields with the database.

55
New cards

What is dynamic update in Hibernate?

An optimization where only modified fields are included in the SQL UPDATE statement.

56
New cards

What annotation enables dynamic update?

@DynamicUpdate

57
New cards

What is the difference between load() and get() in Hibernate?

load() returns a proxy and may throw exception if not found; get() returns null if the entity doesn't exist.

58
New cards

How do you configure Hibernate in a Spring Boot project?

Using application.properties or application.yml along with Spring Data JPA annotations.

59
New cards

What is the default fetch type for @OneToMany in Hibernate?

LAZY

60
New cards

What is the default fetch type for @ManyToOne in Hibernate?

EAGER

61
New cards

What does @Where annotation do in Hibernate?

Adds a SQL fragment to filter entity associations or collections.

62
New cards

What is Hibernate Envers?

A module for auditing and versioning entity changes.