Hibernate Flashcards
What is Hibernate? What is JPA?
Hibernate is an object-relational mapping tool for the Java programming language. It provides a framework for mapping an object oriented domain model to a relational database. It also extracts JDBC by automating processes in the repository layer of our application.
Java Persistence API is a Java specification for accessing, persisting, and managing data between Java objects/classes and a relational database.
What is the benefit of using Hibernate over JDBC?
Hibernate provides caching mechanism which helps to reduce the number of hits as much as possible that your application makes with the database server.
Tell me about some of the JPA annotations you have worked with? What do they do?
@Entity: Tells hibernate a class will be a persistent entity
@Id: Notifies hibernate this field will be a primary key
@GeneratedValue: Will be self-incrementing
@Table: Additional info about table goes here
@Column: Additional info about a column goes here
@ManyToMany/ManyToOne: Multiplicity
What are the interfaces of Hibernate?
Configuration, SessionFactory, Session, Query, Criteria, transaction.
Tell me how you set up hibernate? What files are you editing, what goes in them, etc.
add jar files for hibernate
create the persistent clss
create the configuration file
create the class that retrieves or stores the persistent object
What ways are available to you to map an object to database entities in Hibernate?
Using annotations in java code and using xml files either jpa or hibernate
In the session interface, what is the difference between save and persist methods?what is the difference between get and load methods?
1) save() Stays in the cache until the session is flushed
2) persist() Requires a transaction to be sent to the db
3) saveOrUpdate() Can also be used to update
4) get() Goes to the database immediately. Returns null if not found.
5) load() Uses a proxy until you need an actual value. Gives an object not found exception.
6) update() Will not allow any duplicates inside the cache
7) merge() Will override objects in the cache if they have duplicate ID’s
8) delete() Self-explanatory
What are the different session methods?
1) save() Stays in the cache until the session is flushed
2) persist() Requires a transaction to be sent to the db
3) saveOrUpdate() Can also be used to update
4) get() Goes to the database immediately. Returns null if not found.
5) load() Uses a proxy until you need an actual value. Gives an object not found exception.
Hibernate Session Methods (Update)
6) update() Will not allow any duplicates inside the cache
7) merge() Will override objects in the cache if they have duplicate ID’s
8) delete() Self-explanatory
What is the difference between Eager and Lazy fetching and how to setup either?
Lazy fetching only fetches when needed and is used in conjunction with joins. Eager fetching fetches at the start and automatically joins.
Under what circumstances would your program throw a LazyInitializationException?
Hibernate throws the LazyInitializationException when it needs to initialize a lazily fetched association to another entity without an active session context.
What are the 4 ways to make a query using Hibernate?
1) Session methods (OOP-style crud methods)
2) Native Queries (SQL syntax inside of session methods)
3) Criteria API (OOP design for complex queries)
4) HQL (Hibernate Query Language, a mix of OOP queries and SQL queries)
What is HQL? What makes it different from SQL?
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
What is the Criteria API?
Can you perform all DDL and DML commands with it?
Predefined API used to define queries for entitites.
No, you cannot perform DDL or DML commands with it.
It is only used for fetching or querying.
What is caching? What is the difference between L1 and L2 cache?
Instead of accessing the database each time for the same data, Hibernate by default stores data in a cache the first time it is requested for each individual session. This increases an application’s performance and is also good for concurrency, because each individual session can work from its own cache.
L1
A cache directly connected to a hibernate session. It runs throughout the lifecycle of the Session object. This cache cannot be accessed by other sessions created by the SessionFactory.
L2
A L2 cache is SessionFactory-scoped, meaning it is shared by all sessions created by that SessionFactory. If enabled, hibernate still looks to the L1 cache first, then refers to the L2 cache. There are different cache providers, but hibernate is ultimately unaware of the provider, it only looks for the implementation. Requires special annotations for eligible objects.
How do you enable second level caching?
Create the persistent class in maven, add project info and configuration in pom.xml, create the configuration file, create the class that retrieves the persistent object