Hibernate Flashcards

1
Q

What is Hibernate? What is JPA?

A

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.

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

What is the benefit of using Hibernate over JDBC?

A

Hibernate provides caching mechanism which helps to reduce the number of hits as much as possible that your application makes with the database server.

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

Tell me about some of the JPA annotations you have worked with? What do they do?

A

@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

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

What are the interfaces of Hibernate?

A

Configuration, SessionFactory, Session, Query, Criteria, transaction.

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

Tell me how you set up hibernate? What files are you editing, what goes in them, etc.

A

add jar files for hibernate
create the persistent clss
create the configuration file
create the class that retrieves or stores the persistent object

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

What ways are available to you to map an object to database entities in Hibernate?

A

Using annotations in java code and using xml files either jpa or hibernate

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

In the session interface, what is the difference between save and persist methods?what is the difference between get and load methods?

A

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

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

What are the different session methods?

A

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

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

What is the difference between Eager and Lazy fetching and how to setup either?

A

Lazy fetching only fetches when needed and is used in conjunction with joins. Eager fetching fetches at the start and automatically joins.

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

Under what circumstances would your program throw a LazyInitializationException?

A

Hibernate throws the LazyInitializationException when it needs to initialize a lazily fetched association to another entity without an active session context.

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

What are the 4 ways to make a query using Hibernate?

A

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)

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

What is HQL? What makes it different from SQL?

A

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

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

What is the Criteria API?

Can you perform all DDL and DML commands with it?

A

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.

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

What is caching? What is the difference between L1 and L2 cache?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you enable second level caching?

A

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

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

Tell me about NamedQueries.

A

A statically defined query with a prediefined unchangeable query string

@org.hibernate.annotations.NamedQuery(name = “DeptEmployee_FindAllByDepartment”,query = “from DeptEmployee where department = :department”,timeout = 1,fetchSize = 10)

17
Q

Can you write native SQL with Hibernate? Is this a good idea?

A

Yes you can.

The problem is that it’s Hibernate. It’s not just a dumb JDBC wrapper, but it has a lot more going on. It’s trying to keep caches and session state up to date. If you run a bare SQL update, it has no idea what table(s) you are updating, what it depends on and how it affects everything, so just in case it pretty much flushes everything.

18
Q

How would you configure Hibernate to print to the console all SQL statements run?

A

You can enable it by adding a “show_sql” property in the hibernate configuration file.

19
Q

What are the different object states in Hibernate? What methods move objects to different states?

A

1) Transient: An object that is not associated with a session or a database. A POJO.
2) Persistent: An object that is part of the session. Represents a row in a database, and is unique to a session. Saved to DB once committed.
3) Detached: An object that was once part of a session, but is not any longer. Doesn’t matter if it’s in DB or not.

20
Q

What is a proxy? When does the proxy resolve to the real object?

A

Proxy servers act as a firewall and web filter, provide shared network connections, and cache data to speed up common requests.

21
Q

What is automatic dirty checking?

A

Automatic dirty checking is a feature that saves us the effort of explicitly asking Hibernate to update the database when we modify the state of an object inside a transaction.