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

How do you configure Hibernate?

A
  • Via a configuration xml, or programmatically.
  • In the xml, you would set up the SessionFactory 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
3
Q

What is ORM?

A
  • ORM stands for Object Relational Mapping.
  • It is a way to relate a program’s beans to a relational database.
  • You can use 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
4
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 - like Hibernate.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
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
6
Q

What is HQL?

A
  • HQL is Hibernate Query Language.
  • HQL is Hibernate’s version of SQL.
  • It is made to act more like OOP.
  • Uses bean names and properties
  • Native SQL
  • Criteria - programmatically create queries
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
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
8
Q

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

A
  • The Criteria API is one of the main interfaces of Hibernate.
  • It allows for programmatic querying of the database using 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
9
Q

What do you need to include in the hibernate.Cloud Foundryg.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
10
Q

What are some common Hibernate annotations?

A
  • @Entity: Specifies that the class is an entity. It can be applied on Class, Interface of Enums.
  • @Table: It specifies the table in the database with which this entity is mapped.
  • @Column: Specify the column mapping using this annotation. Name attribute of this annotation is used for specifying the table’s column name.
  • @Id: This annotation specifies the primary key of the entity.
  • @GeneratedValue: This annotation specifies the generation strategies for the values of primary keys.
  • @One-to-one: Each entity instance is related to a single instance of another entity.
  • @One-to-many: An entity instance can be related to multiple instances of the other entities.
  • @Many-to-one: multiple instances of an entity can be related to a single instance of the other entity.
  • @Many-to-many: The entity instances can be related to multiple instances of each other.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the important classes and interfaces in Hibernate?

A
  • Configuration class
  • Session Interface
  • SessionFactory Interface
  • Transaction Interface
  • Query Interface
  • Criteria Interface
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the types of Caches?

A

Three types:

  • L1 Cache (Session Cache) – caches objects within the current session. It is enabled by default in Hibernate.
  • L2 Cache (SessionFactory Cache) -responsible for caching objects across sessions.
  • Query Cache – used to cache the results of a query.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

get() vs load() in Hibernate

A
  • Only use the load() method if you are sure that the object exists.
  • load() just returns a proxy by default and database will not be hit until the proxy is first invoked.

get()

  • If you are not sure that the object exists, then use one of the get() methods.
  • get() method will return null if the unique id is not found in the database.
  • get() will hit the database immediately.
  • get() returns a proxy, if no row is found throws ObjectNotFoundException. Load always hits the database returns null if no row found.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is Transactional-Write Behind?

A
  • Hibernate uses a sophisticated algorithm to determine an efficient ordering that avoids DB foreign key constraint violation, but it is still sufficiently predictable to the user
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a Callback Interface?

A
  • This interface is used in the application to receive a notification when some object events occur. Like when an object is loaded, saved or deleted.
  • There is no need to implement a callback in hibernate applications, but it is useful for implementing certain kinds of generic functionality.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the types of Inheritance Models?

A
  • Table Per Hierarchy – here, a single table is required to map the whole hierarchy. An extra column (known as discriminator column) is added to identify the class. But nullable values are stored in the table.
  • Table Per Concrete class – tables are created as per class. But a duplicate column is added in subclass tables.
  • Table Per Subclass – In this strategy, tables are created as per class but related by foreign key. So, there are no duplicate columns.
17
Q

How do you create a bean in Hibernate?

A
  • Annotations
  • @Entity
  • @Table (name=””)
  • @Column
  • @Id
  • @GeneratedValue (specify strategy)
  • @JoinColumn
  • Specify multiplicity @OneToMany, @ManyToOne, @ManyToMany
  • @MappeDBy
  • XML