- What is Hibernate ?
- Briefly explain Hibernate Architecture ?
- What is the use of ORM ?
- What is Java Persistence API(JPA) ?
- What is JDBC ?
- Why are you using hibernate when we have JDBC ?
- What are the advantages of hibernate ?
- Tell me drawbacks or disadvantages of hibernate ?
- Mention design patterns used in hibernate ?
- What is the general flow of Hibernate communication with RDBMS?
- How do you map Java Objects with Database tables?
- What are the most common methods of Hibernate configuration?
- What is HQL ?
- What is hibernate configuration file ?
- What is hibernate mapping file ?
- What is the root node in hbm.xml file ?
- Some important tags or elements used in hbm.xml ? Following are the important tags of hibernate.cfg.xml:
- Mention some important annotations used in hiberante mapping ?
- What are the core interfaces of hibernate ? or What are the key components/elements/objects of hibernate ?
- What are the key components of hibernate Configuration Object ?
- Name some properties required to configure database in standalone situation ?
- Some important methods and their usage ? Session.beginTransaction method begins a unit of work and returns the associated Transaction object. Session.createCriteria creates a new Criteria instance, for the given entity class, or a superclass of an entity class. Session.createQuery creates a new instance of Query for the given HQL query string. Session.createSQLQuery creates a new instance of SQLQuery for the given SQL query string. Session.delete removes a persistent instance from the datastore. Session.get returns the persistent instance of the given named entity with the given identifier, or null if there is no such persistent instance. Session.refresh re-reads the state of the given instance from the underlying database. Session.save saves the state of the given instance from the underlying database. Session.update updates the state of the given instance from the underlying database. Session.saveOrUpdate either saves(Object) or updates(Object) the given instance.
- What is meant by persistent class in hibernate ?
- What are the best practices hibernate recommend for persistant classes ?
- What is meant by serializable ?
- What are the states of object in hibernate ?
- Whether Session Factory and Session are thread safe objects ?
- Difference between openSession and getCurrentSession ?
- Difference between session.save() and session.persist() methods ?
- Difference between get() and load() methods in hibernate ?
- Difference between update() and merge() methods in hibernate ?
- Difference between save() , saveOrUpdate() , persist() methods ?
- Mention the types of association mapping available in hibernate ?
- How do you implement associations or relationships in hibernate ?
- Mention the inheritance models in hibernate ?
- What is meant by immutable class ? How can you make a class immutable in hibernate ?
- What is meant by automatic dirty check in hibernate ?
- Which association's are possible with Collections interface or with Collection classes ?
- What is hibernate Proxy ?
- What is meant by lazy loading in hibernate ?
- What is meant by hibernate caching ?
- What is meant by first level cache ?
- What is meant by second level cache ?
- What is meant by Query cache in hiberante ?
- Mention collection types in hibernate ?
- What is bag collection in hibernate ?
- How to fetch ordered collection from database in hibernate ?
- How can Hibernate be configured to access an instance variable directly and not through a setter method ?
- Difference between ordered collection and sorted collection ? Which one is better in hibernate ?
- Tell me about joins in hibernate ?
- What is native sql query ? Can we execute them in hibernate ?
- What is the advantage of native sql query support in hibernate ?
- What is Named SQL Query ?
- What are the Mandatory things for a java object to become Hibernate Entity object ?
- What is Entity class ?
- What happens if we make our Entity class as final in hibenate ?
- Define cascade and inverse option in one-many mapping?
- Explain about log4j logging in hibernate ?
- How to log SQL queries into log files in hibernate ?
- What is transaction Management ? How it works in hibernate ?
- What is Cascading ? Mention different types of Cascading ?
- How to configure application server JNDI DataSource with Hibernate Framework ?
- How to integrate Spring and Hibernate frameworks ?
- What is hibernate Validator Framework ?
- What are concurrency strategies ?
- What is N+1 Select problem in hibernate ? Mention some strategies to solve it ?
- How to implement Optimistic locking in Database?
- What are Callback interfaces? Callback interfaces allow the application to receive a notification when something interesting happens to an object—for example, when an object is loaded, saved, or deleted. Hibernate applications don't need to implement these callbacks, but they're useful for implementing certain kinds of generic functionality.
- What is transactional write-behind? Hibernate uses a sophisticated algorithm to determine an efficient ordering that avoids database foreign key constraint violations but is still sufficiently predictable to the user. This feature is called transactional write-behind.
- What do you mean by fetching strategy ? A fetching strategy is the strategy Hibernate will use for retrieving associated objects if the application needs to navigate the association. Fetch strategies may be declared in the O/R mapping metadata, or over-ridden by a particular HQL or Criteria query.
- What is the use of dynamic-insert and dynamic-update attributes in a class mapping? Criteria is a simplified API for retrieving entities by composing Criterion objects. This is a very convenient approach for functionality like "search" screens where there is a variable number of conditions to be placed upon the result set. dynamic-update (defaults to false): Specifies that UPDATE SQL should be generated at runtime and contain only those columns whose values have changed dynamic-insert (defaults to false): Specifies that INSERT SQL should be generated at runtime and contain only the columns whose values are not null.
- How do you define sequence generated primary key in hibernate?
- Define HibernateTemplate? org.springframework.orm.hibernate.HibernateTemplate is a helper class which provides different methods for querying/retrieving data from the database. It also converts checked HibernateExceptions into unchecked DataAccessExceptions.
- What are the benefits does HibernateTemplate provide? The benefits of HibernateTemplate are : HibernateTemplate, a Spring Template class simplifies interactions with Hibernate Session. Common functions are simplified to single method calls. Sessions are automatically closed. Exceptions are automatically caught and converted to runtime exceptions.
- How do you switch between relational databases without code changes? Using Hibernate SQL Dialects , we can switch databases. Hibernate will generate appropriate hql queries based on the dialect defined.
- If you want to see the Hibernate generated SQL statements on console, what should we do? In Hibernate configuration file set as follows:
- What are derived properties? The properties that are not mapped to a column, but calculated at runtime by evaluation of an expression are called derived properties. The expression can be defined using the formula attribute of the element.
- What is component mapping in Hibernate? A component is an object saved as a value, not as a reference. A component can be saved directly without needing to declare interfaces or identifier properties. Required to define an empty constructor. Shared references not supported.
- What is Hibernate Criteria API ? Criteria is a simplified API for retrieving entities by composing Criterion objects. This is a very convenient approach for functionality like "search" screens where there is a variable number of conditions to be placed upon the result set. Example : List employees = session.createCriteria(Employee.class) .add(Restrictions.like("name", "a%") ) .add(Restrictions.like("address", "Boston")) .addOrder(Order.asc("name") ) .list();
- How do you invoke Stored Procedures?
The general flow of Hibernate communication with RDBMS is : Load the Hibernate configuration file and create configuration object. It will automatically load all hbm mapping files Create session factory from configuration object Get one session from this session factory Create HQL Query Execute query to get list containing Java objects
First we need to write Java domain objects (beans with setter and getter). Write hbm.xml, where we map java class to table and database columns to Java class variables. Example :
The most common methods of Hibernate configuration are: Programmatic configuration XML configuration (hibernate.cfg.xml)
By mapping the property with access="field" in Hibernate metadata. This forces hibernate to bypass the setter method and access the instance variable directly while initializing a newly loaded object.
HQL provides four ways of expressing (inner and outer) joins:- An implicit association join An ordinary join in the FROM clause A fetch join in the FROM clause. A theta-style join in the WHERE clause.
cascade - enable operations to cascade to child entities. cascade="all|none|save-update|delete|all-delete-orphan" inverse - mark this collection as the "inverse" end of a bidirectional association. inverse="true|false" Essentially "inverse" indicates which end of a relationship should be ignored, so when persisting a parent who has a collection of children, should you ask the parent for its list of children, or ask the children who the parents are?
A org.hibernate.Session is designed to represent a single unit of work (a single atmoic piece of work to be performed. Sample code of handling transaction from hibernate session. Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); Event theEvent = new Event(); theEvent.setTitle(title); theEvent.setDate(theDate); session.save(theEvent); session.getTransaction().commit();
You can implement optimistic locks in your DB table in this way (This is how optimistic locking is done in Hibernate): - Add integer "version" column to your table. - Increase value of this column with each update of corresponding row. - To obtain lock, just read "version" value of row. - Add "version = obtained_version" condition to where clause of your update statement. - Verify number of affected rows after update. If no rows were affected - someone has already modified your entry. Your update should look like UPDATE mytable SET name = 'Andy', version = 3 WHERE id = 1 and version = 2;
Usingtag. Example:- SEQUENCE_NAME
{ ? = call selectAllEmployees() }