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.
What is the difference between the first and second level caches in Hibernate?
First level cache
● Default
● Transaction scoped.
● Formed by the session object.
● Single session availability.
● Guarantees object identity inside a transaction. Second level cache
● Optional/pluggable
● Might be scoped to the process or cluster.
● Formed by the SessionFactory object.
● Multiple session availability.
● Can be configured on per class or per association basis. First-level cache First-level cache always Associates with the Session object. Hibernate uses this cache by default. Here, it processes one transaction after another one, means wont process one transaction many times. Mainly it reduces the number of SQL queries it needs to generate within a given transaction. That is instead of updating after every modification done in the transaction, it updates the transaction only at the end of the transaction. Second-level cache
Second-level cache always associates with the Session Factory object. While running the transactions, in between it loads the objects at the Session Factory level, so that those objects will be available to the entire application, not bound to single user. Since the objects are already loaded in the cache, whenever an object is returned by the query, at that time no need to go for a database transaction. In this way the second level cache works.
Name some second level (L2) cache providers for Hibernate.
Ehcache: is an open source, standards-based cache that boosts performance, offloads your database, and simplifies scalability.
OSCache: is a caching solution that includes a JSP tag library and set of classes to perform fine grained dynamic caching of JSP content, servlet responses or arbitrary objects.
SwarmCache: is a simple but effective distributed cache. It uses IP multicast to efficiently communicate with any number of hosts on a LAN. It is specifically designed for use by clustered, database-driven web applications.
JBoss Cache’s: goal is to provide enterprise-grade clustering solutions to Java-based frameworks, application servers or custom-designed Java SE applications.
What is Transaction dirty read?
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.
What is Transaction lost update?
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.
What is Transaction repeatable read?
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.
What is Transaction Phantom Read?
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.
How do you configure an L2 cache in Hibernate?
● Choose an L2 cache vendor that has the desired properties.
● In hibernate.cfg.xml include a cache provider tag:
“property name=”“hibernate.cache.provider_class”
org.hibernate.cache.EhCacheProvider property “
● Set the cache properties in the cache provider’s xml or use annotations in the persistent class. Example:
“cachename=”“com.somecompany.someproject.domain.Country”” maxElementsInMemory=”“10000”” eternal=”“false”” timeToIdleSeconds=”“300”” timeToLiveSeconds=”“600”” overflowToDisk=”“true”” /”