Hibernate Flashcards

1
Q

What is ORM ?

A

ORM stands for object/relational mapping. ORM is the automated persistence of objects in a Java application to the tables in a relational database.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is ORM (Object/Relational Mapping) used for?

A

Hibernate ORM is concerned with helping your application to achieve persistence. Specifically, Hibernate ORM is concerned with data persistence as it applies to relational databases (RDBMS).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is Persistence?

A

Persistence simply means that we would like our application’s data to outlive the applications process. In Java terms, we would like the state of (some of) our objects to live beyond the scope of the JVM so that the same state is available later.

RDBMS remain a very popular persistence mechanism and will so for the foreseeable future.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does ORM consists of ?

A

An ORM solution consists of the following four entities:
1 An API to perform basic CRUD operations on objects of persistent classes.
2 A language or API to specify queries that refer to classes and properties of classes.
3 A configurable facility for specifying mapping metadata.
4 A technique to interact with transactional objects to perform dirty checking, lazy association fetching, and other optimization functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the CRUD operations

A

Create
Read
Update
Delete

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is Hibernate Dirty Checking?

A

Dirty Checking is one of the features of hibernate. In dirty checking, hibernate automatically detects whether an object is modified (or) not and need to be updated. As long as the object is in persistent state i.e., bound to a particular Session(org.hibernate.Session). Hibernate monitors any changes to the objects and executes sql.

Note: For dirty checking to work, the object must exist in cache.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a Transaction and a Transaction object?

A

A transaction represents a unit of work. If you want the database to do some work for you, then you need to start a transaction. When you are finished interacting with the database, you end the transaction, and let the database know you are done.

A transaction is associated with a Session and is usually instantiated by a call to Session.beginTransaction(). A single session might span multiple transactions since the notion of a session (a conversation between the application and the datastore) is of coarser granularity than the notion of a transaction. However, it is intended that there be at most one uncommitted Transaction associated with a particular Session at any time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is Hibernate Lazy Association Fetching?

A

Lazy loading means that an entity will be loaded only when you actually accesses the entity for the first time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the ORM levels ?

A
The ORM levels are: 
● Pure relational (stored procedure.) 
● Light objects mapping (JDBC) 
● Medium object mapping 
● Full object Mapping (composition,inheritance, polymorphism, persistence by reachability)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is Hibernate?

A

Hibernate is a high-performance Object/Relational persistence. Hibernate not only takes care of the mapping from Java classes to database tables (and from Java data types to SQL data types), but also provides data query and retrieval facilities.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Why do you need ORM tools like Hibernate?

A
The main advantage of ORM like hibernate is that it shields developers from messy SQL. 
● Improved productivity 
○ High-level object-oriented API 
○ Less Java code to write 
○ No SQL to write 

● Improved performance
○ Sophisticated caching
○ Lazy loading
○ Eager loading

● Improved maintainability
○ A lot less code to write

● Improved portability
○ ORM framework generates database-specific SQL for you

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the core interfaces are of the Hibernate framework?

A

The five core interfaces are used in just about every Hibernate application. Using these interfaces, you can store and retrieve persistent objects and control transactions.

  1. Session Interface
  2. SessionFactory interface
  3. Configuration Interface
  4. Transaction Interface
  5. Query and Criteria interface
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the types of Hibernate instance states ?

A

Three types of instance states:
1. Transient : This instance is never been associated with any one of the persistence process. This does not have persistent identity like primary key value.

  1. Persistent : A persistent context is made to associate with the current instance. It has persistent identity like primary key value and a corresponding row of a table in the data base. Hibernate guarantees the persistent identity is equivalent to the java Identity [object], for a particular persistence context
  2. Detatched : This instance association with a persistence context is only once and the context was closed or serialized to another process. The persistent identity is retained and it can be a corresponding row in a database.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the essential properties of a transaction?

A

Atomicity
Atomicity takes individual operations and turns them into an all-or-nothing unit of work, succeeding if and only if all contained operations succeed.

A transaction might encapsulate a state change (unless it is a read-only one). A transaction must always leave the system in a consistent state, no matter how many concurrent transactions are interleaved at any given time.

Consistency
Consistency means that constraints are enforced for every committed transaction. That implies that all Keys, Data types, Checks and Trigger are successful and no constraint violation is triggered.

Isolation
Transactions are concurrency control mechanisms, and they deliver consistency even when being interleaved. Isolation brings us the benefit of hiding uncommitted state changes from the outside world, as failing transactions shouldn’t ever corrupt the state of the system. Isolation is achieved through concurrency control using pessimistic or optimistic locking mechanisms.

Durability
A successful transaction must permanently change the state of a system, and before ending it, the state changes are recorded in a persisted transaction log. If our system is suddenly affected by a system crash or a power outage, then all unfinished committed transactions may be replayed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are Transaction Isolation Levels?

A

The transaction isolation level indicates the degree to which two transactions interact with each other over the same data. The transaction problems that can occur are: lost update, dirty read, unrepeatable read, and phantom read.

Read uncommitted isolation levels This is the lowest isolation level which can also be called as dirty read. Using this, you can read uncommitted data which can be rolled back at any point. With this level, SQL server uses share lock while reading data.

Read committed isolation levels With this level, uncommitted data can’t be read. This is default isolation level and uses shared lock while reading data.
Repeatable read isolation levels It locks all the data that is used in the query.

Serializable isolation levels It locks data set until the transaction will be completed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is Transaction dirty read?

A

Dirty read occurs wherein one transaction is changing the tuple/record, and a second transaction can read this tuple/record before the original change has been committed or rolled back.

17
Q

What is Transaction lost update?

A

A lost update occurs when two different transactions are trying to update the same column on the same row within a database at the same time. Typically, one transaction updates a particular column in a particular row, while another that began very shortly afterward did not see this update before updating the same value itself. The result of the first transaction is then “lost”, as it is simply overwritten by the second transaction.

18
Q

What is Transaction repeatable read?

A

Specifies that statements cannot read data that has been modified but not yet committed by other transactions and that no other transactions can modify data that has been read by the current transaction until the current transaction completes.

19
Q

What is Transaction Phantom Read?

A

Data getting changed in current transaction by other transactions is called Phantom Reads. New rows can be added by other transactions, so you get different number of rows by firing same query in current transaction.

20
Q

What are some common JPA annotations used in Hibernate?

A

javax.persistence.*

@Entity: Annotate all your entity beans with @Entity.

@Table: Specify the database table this Entity maps to using the name attribute of @Table annotation. In the example below, the data will be stored in ‘company’ table in the database.

@Column: Specify the column mapping using @Column annotation.

@Id: Annotate the id column using @Id.

@GeneratedValue: Let database generate (auto-increment) the id column.

@OneToOne
@ManyToOne
@OneToMany
@ManyToMany

21
Q

What is the difference in the hibernate.cfg.xml for the mapped (hbm) and annotation driven class?

A

Mapping file implementation: mapping resource=”myTable.hbm.xml” Annotated POJO implementation: mapping class=”myTable”

22
Q

What is the need for Hibernate xml mapping file?

A

Hibernate mapping file tells Hibernate which tables and columns to use to load and store objects.

23
Q

What are the important tags of hibernate.cfg.xml?

A

Hibernate Configuration
Session Factory
Property Name
Mapping Resources

24
Q

What role does the Session interface play in Hibernate?

A

The Session interface is the primary interface used by Hibernate applications. It is a single-threaded, short-lived object representing a conversation between the application and the persistent store. It allows you to create query objects to retrieve persistent objects. Session session = sessionFactory.openSession(); Session interface role:
● Wraps a JDBC connection
● Factory for Transaction
● Holds a mandatory (first-level) cache of persistent objects, used when navigating the object graph or looking up objects by identifier

25
Q

What role does the SessionFactory interface play in Hibernate?

A

What is the general flow of a Hibernate communication with the RThe application obtains Session instances from a SessionFactory. There is typically a single SessionFactory for the whole application created during application initialization. The SessionFactory caches generate SQL statements and other mapping metadata that Hibernate uses at runtime. It also holds cached data that has been read in one unit of work and may be reused in a future unit of work SessionFactory sessionFactory = configuration.buildSessionFactory();DBMS?

26
Q

What is the general flow of a Hibernate communication with the RDBMS?

A

The general flow of Hibernate communication with RDBMS is :
● Load the Hibernate configuration file and create configuration object. It will automatically load all hbm mapping files
● Create session factory from configuration object
● Get one session from this session factory
● Create HQL Query
● Execute query to get list containing Java objects

27
Q

What is Hibernate Query Language (HQL)?

A

Hibernate offers a query language that embodies a very powerful and flexible mechanism to query, store, update, and retrieve objects from a database.

Hibernate Query Language (HQL) is an object-oriented query language, similar to SQL, but instead of operating on tables and columns, HQL works with persistent objects and their properties. HQL queries are translated by Hibernate into conventional SQL queries which in turns perform action on database.

Although you can use SQL statements directly with Hibernate using Native SQL but I would recommend to use HQL whenever possible to avoid database portability hassles, and to take advantage of Hibernate’s SQL generation and caching strategies.

Keywords like SELECT , FROM and WHERE etc. are not case sensitive but properties like table and column names are case sensitive in HQL.

28
Q

How do you map Java Objects with Database tables?

A
● First we need to write Java domain objects (beans with setter and getter). 
● Write hbm.xml, where we map java class to table and database columns to Java class variables.
29
Q

What’s the difference between load() and get()?

A

● Only use the load() method if you are sure that the object exists.
● load() method will throw an exception if the unique id is not found in the database.
● load() just returns a proxy by default and database won’t be hit until the proxy is first invoked. get()
● If you are not sure that the object exists, then use one of the get() methods.
● get() method will return null if the unique id is not found in the database.
● get() will hit the database immediately.

30
Q

What is the difference between and merge and update?

A

update () : When the session does not contain the persistent instance with the same identifier, and if it is sure use update for the data persistence in the hibernate.

merge () : Irrespective of the state of a session, if there is a need to save the modifications at any given time, use merge().

31
Q

How do you define sequence generated primary key in hibernate?

A

Using generator tag
@GeneratedValue(strategy=GenerationType.AUTO)

@GenericGenerator(name=”kaugen” , strategy=”increment”) @GeneratedValue(generator=”kaugen”)

32
Q

Explain Criteria API

A

Hibernate provides alternate ways of manipulating objects and in turn data available in RDBMS tables. One of the methods is Criteria API which allows you to build up a criteria query object programmatically where you can apply filtration rules and logical conditions.

The Hibernate Session interface provides createCriteria() method which can be used to create a Criteria object that returns instances of the persistence object’s class when your application executes a criteria query.

33
Q

What is the advantage of Hibernate over jdbc?

A
  1. We no longer have to write SQL in java
  2. We can perform transactions on the database using java objects
  3. Hibernate methods can be used to map Java classes and fields to database tables and columns
34
Q

What is Session Interface?

A

Session Interface : The basic interface for all hibernate applications. The instances are light weighted and can be created and destroyed without expensive process.

35
Q

What is SessionFactory interface?

A

SessionFactory interface : The delivery of session objects to hibernate applications is done by this interface. For the whole application, there will be generally one SessionFactory and can be shared by all the application threads.

36
Q

What is Configuration Interface?

A

Configuration Interface : Hibernate bootstrap action is configured by this interface. The location specification is specified by specific mapping documents, is done by the instance of this interface.

37
Q

What is Transaction Interface?

A

Transaction Interface : This is an optional interface. This interface is used to abstract the code from a transaction that is implemented such as a JDBC / JTA transaction.

38
Q

Query and Criteria interface

A

Query and Criteria interface : The queries from the user are allowed by this interface apart from controlling the flow of the query execution.