Hibernate Flashcards
What is Hibernate?
- 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 do you configure Hibernate?
- 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.
What is ORM?
- 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.
What are the ORM Levels?
The ORM levels are: Pure Relational Mapping - stored procedure. Light ORM Mapping - like JDBC. Medium ORM Mapping. Full ORM Mapping - like Hibernate.
What are the Hibernate Object States?
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.
What is HQL?
- 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
What is Native SQL?
Native SQL is a way of running regular SQL queries in Hibernate.
What is the Criteria API? How does it differ from HQL?
- 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.
What do you need to include in the hibernate.Cloud Foundryg.xml file?
- 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
What are some common Hibernate annotations?
- @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.
What are the important classes and interfaces in Hibernate?
- Configuration class
- Session Interface
- SessionFactory Interface
- Transaction Interface
- Query Interface
- Criteria Interface
What are the types of Caches?
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.
get() vs load() in Hibernate
- 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.
What is Transactional-Write Behind?
- 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
What is a Callback Interface?
- 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.