Hibernate Flashcards
What is ORM ?
ORM stands for object/relational mapping. ORM is the automated persistence of objects in a Java application to the tables in a relational database.
What is ORM (Object/Relational Mapping) used for?
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).
What is Persistence?
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.
What does ORM consists of ?
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.
What are the CRUD operations
Create
Read
Update
Delete
What is Hibernate Dirty Checking?
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.
What is a Transaction and a Transaction object?
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.
What is Hibernate Lazy Association Fetching?
Lazy loading means that an entity will be loaded only when you actually accesses the entity for the first time.
What are the ORM levels ?
The ORM levels are: ● Pure relational (stored procedure.) ● Light objects mapping (JDBC) ● Medium object mapping ● Full object Mapping (composition,inheritance, polymorphism, persistence by reachability)
What is Hibernate?
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.
Why do you need ORM tools like Hibernate?
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
What are the core interfaces are of the Hibernate framework?
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.
- Session Interface
- SessionFactory interface
- Configuration Interface
- Transaction Interface
- Query and Criteria interface
What are the types of Hibernate instance states ?
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.
- 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
- 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.
What are the essential properties of a transaction?
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.
What are Transaction Isolation Levels?
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.