Hibernate Flashcards

1
Q

What is Hibernate?

A

Hibernate is an Object Relationship Mapping Framework/Tool. It allows a program to map a class to a relational database. In doing so, the programmer creates a sessionfactory. The sessionfactory contains the connection info for the database, as well as the driver, and dialect. You can map files either by a mapping file, or by JPA annotations in the classes you want mapped.

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

What does it abstract?

A

Hibernate is an abstraction of JDBC.

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

How do you configure Hibernate?

A
  • You can configure hibernate via a configuration xml, or programmatically. In the xml, you would set up the session factory with the sessionfactory tag. It would contain all the info for your database, like driver, dialect, connection info, and any and all mappings.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why do you need ORM tools like Hibernate?

A

You need ORM tools like Hibernate because it would take a long time to setup all of the DAOs in a JEE project/environment. Because it abstracts JDBC, it sets up all of your daos after you map your classes. Hibernate can even create the database for you.

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

What is ORM?

A

ORM stands for Object Relational Mapping. It is a way to relate a programs beans to a relational database. You can you annotations like @Id to specify that a particular variable is the ID for that table and so on.

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

What does ORM consists of?

A

ORM consists of taking an existing bean, and mapping it to a relational database. To start, you would annotate the class with the @Entity annotation. You would then annotate the class @Table and give it a table name. As for the variables of the class, you must annotate the ID with @Id, and all of the other variables with @column.

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

What are the ORM Levels?

A
The ORM levels are:
		Pure Relational Mapping - stored procedure
		Light ORM Mapping - like JDBC
		Medium ORM Mapping
		Full ORM Mapping alike Hibernate
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the Hibernate Object States?

A

The Hibernate Object States are:
Transient - Object exists outside of session and the row
Persistent - Object in the session that represents a row in the database
Detached - Object is removed from the session, any actions on the object are not saved to the database.

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

What are Application Transactions?

A

Application transactions are any action that needs to modify data stored in the database. There are different ways of ensure ACID properties of these transactions.

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

What is HQL?

A

HQL is Hibernate Query Language. HQL is hibernates version of SQL. It is made to act more like OOP.

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

What is Native SQL?

A

Native SQL is a way of running regular SQL queries in Hibernate

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

What is the Criteria API? How does it differ from HQL?

A

The Criteria API is one main interfaces of Hibernate. It allows for programmatic querying of the database. They use restrictions and projections. Restrictions act like a where clause, while projections act like aggregate functions. Criteria API differs from HQL in that it is fully programmatic. HQL is still structured like SQL. It just understands OOP principles..

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

What do you need to include in the hibernate.cfg.xml file?

A
  • The Hibernate config file must have all the details to setup the sessionfactory. These details are the connection info, dialect, and driver. It must also have all mappings in a mapping tag.
    • Driver
    • Dialect
    • Username
    • Password
    • URL
    • Link to mapping file
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is Hibernate.hbm2ddl?

A

Hibernate.hbm2ddl is a property you can set in the cfg that will tell hibernate whether or not to make the database. It is best not to use this in anything that isn’t a development environment.

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

What is HBM?

A

HBM stands for Hibernate Mapping. This tells hibernate how to map a given object to a relational database.

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

What do you need to include in the HBM?

A

The HBM must have a class name, and what entity it relates to in the database. It must have at least an ID specified, and how to generate the ID.

17
Q

What are some common annotations?

A
@OnetoMany
@ManytoOne
@ManytoMany
@OnetoOne
@Entity
@Table
@Column
18
Q

Merge vs Update

A

Both are updating information that exists in the database
Merge - can be called on objects that are persisted
Update - can only be called on objects that are transient
SaveOrUpdate - calls either save or update based on if the identifier exists
Save - store the object into the database; persists transient instance and returns id of the entity created

19
Q

get() vs load()

A

load() returns a proxy, if no row is found throws ObjectNotFoundException. get() always hits the database returns null if no row found.

20
Q

What’s the difference between sorted and ordered collections?

A

Sorted - natural ordering; use it for smaller collections; expensive to sort
Ordered - use it for larger collections; specify by the ORDER BY clause
Collections - resultset

21
Q

What is a hibernate proxy?

A

The proxy attribute enables lazy initialization of persistent instances of the class

22
Q

What is a transactional write-behind?

A

What Hibernate does is collect all database operations associated with a transaction, creating the minimum set of sql queries and executing them

23
Q

Different kinds of fetching

A

Lazy fetching - fetched when needed; used in conjunction with joins
Lazy initialization exception
Eager fetching - fetched at the start; automatically joins