You are on page 1of 58

Q) What are the most common methods of Hibernate configuration?

A) The most common methods of Hibernate configuration are: * Programmatic configuration * XML configuration (hibernate.cfg.xml) Q) What are the important tags of hibernate.cfg.xml? A) An Action Class is an adapter between the contents of an incoming HTTP rest and the corresponding business logic that should be executed to process this rest. Q) What are the Core interfaces are of Hibernate framework? A) People who read this also read: The five core interfaces are used in just about every Hibernate application. Using these interfaces, you can store and retrieve persistent objects and control transactions. * Session interface * SessionFactory interface * Configuration interface * Transaction interface * Query and Criteria interfaces Q) What role does the Session interface play in Hibernate? A) The Session interface is the primary interface used by Hibernate applications. It is a single-threaded, short-lived object representing a conversation between the application and the persistent store. It allows you to create query objects to retrieve persistent objects. Session session = sessionFactory.openSession(); Session interface role: * Wraps a JDBC connection * Factory for Transaction * Holds a mandatory (first-level) cache of persistent objects, used when navigating the object graph or looking up objects by identifier Q) What role does the SessionFactory interface play in Hibernate? A) The application obtains Session instances from a SessionFactory. There is typically a single SessionFactory for the whole applicationcreated during application initialization. The SessionFactory caches generate SQL statements and other mapping metadata that Hibernate uses at runtime. It also holds cached data that has been read in one unit of work and may be reused in a future unit of work SessionFactory sessionFactory = configuration.buildSessionFactory(); Q) What is the general flow of Hibernate communication with RDBMS? A) 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 Q) What is Hibernate Query Language (HQL)? A) Hibernate offers a query language that embodies a very powerful and flexible mechanism to query, store, update, and retrieve objects from a database. This language, the Hibernate query Language (HQL), is an object-oriented extension to SQL. Q) How do you map Java Objects with Database tables? A) * First we need to write Java domain objects (beans with setter and getter). The variables should be same as database columns. * Write hbm.xml, where we map java class to table and database columns to Java class variables. Example : <hibernate-mapping> <class name=com.test.User table=user> <property column=USER_NAME length=255 name=userName not-null=true type=java.lang.String/> <property column=USER_PASSWORD length=255 name=userPassword not-null=true type=java.lang.String/> </class> </hibernate-mapping> Q) What Does Hibernate Simplify? A) Hibernate simplifies: * Saving and retrieving your domain objects * Making database column and table name changes * Centralizing pre save and post retrieve logic * Complex joins for retrieving related items * Schema creation from object model Q) Whats the difference between load() and get()? A) load() vs. get() load() :Only use the load() method if you are sure that the object exists. load() method will throw an exception if the unique id is not found in the database. load() just returns a proxy by default and database wont 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. Q) What is the difference between and merge and update ? A)Use update() if you are sure that the session does not contain an already persistent instance with the same identifier, and merge() if you want to merge your modifications at any time without consideration of the state of the session. Q) How do you define sequence generated primary key in hibernate? A) Using <generator> tag. Example:<id column=USER_ID name=id type=java.lang.Long> <generator class=sequence> <param name=table>SEQUENCE_NAME</param> <generator> </id> Q) Define cascade and inverse option in one-many mapping? A) 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? Q) What does it mean to be inverse?
WHAT
IS

INVERSE?

This is the most confusing keyword in Hibernate, at least i took quite a long timeto understand about it. The inverse keyword is always declared in one-tomanyand many-to-many relationship (many-to-one doesnt has inverse keyword), It defines which side is responsible to take care the relationship.

ALWAYS

PUT INVERSE=TRUE IN YOUR COLLECTION VARIABLE?

There are many Hibernate articles try to explain what is inverse with many Hibernate official jargon, which is very hard to understand (at least to me). In few articles, they even suggest just forget about what is inverse, and always put the inverse=true in your collection variable. I agreed this statement is always true inverse=true in your collection variable, but do not blindfold on it, try to understand the reason behind is essential to optimal your Hibernateperformance.

INVERSE,

CAN IT CHANGE TO

RELATIONSHIP

OWNER?

In Hibernate, only the relationship owner should maintain the relationship, and the inverse keyword is created to defines which side is the owner to maintain the relationship. However the inverse keyword itself is not verbose enough, I would suggest change the keyword to relationship_owner. The inverse=true means this is the relationship owner, whereas inverse=false (default) means its not. Lets go though a quick example to grab an initial idea of what is inverse responsibility?

1. DATABASE

TABLES

This is a one-to-many relationship table design, a STOCK table has many occurrences in STOCK_DAILY_RECORD table.

2. HIBERNATE

IMPLEMENTATION

Stock.java
public class Stock implements java.io.Serializable } ... private Set<StockDailyRecord> stockDailyRecords = new HashSet<StockDailyRecord>(0); ...

StockDailyRecord.java
public class StockDailyRecord implements java.io.Serializable } ... private Stock stock; ...

Stock.hbm.xml
<hibernate-mapping> <class name="com.developer.ammon.Stock" table="stock" ...> ... <set name="stockDailyRecords" table="stock_daily_record" fetch="select"> <key> <column name="STOCK_ID" not-null="true" /> </key> <one-to-many class="com.developer.ammon.StockDailyRecord" /> </set> ...

StockDailyRecord.hbm.xml
<hibernate-mapping> <class name="com.developer.ammon.StockDailyRecord" table="stock_daily_record" ...> ...

<many-to-one name="stock" class="com.developer.ammon.Stock"> <column name="STOCK_ID" not-null="true" /> </many-to-one> ...

3. QUESTION
See this file Stock.hbm.xml. <hibernate-mapping> <class name="com.developer.ammon.Stock" table="stock" ...> ... <set name="stockDailyRecords" table="stock_daily_record" fetch="select"> <key> <column name="STOCK_ID" not-null="true" /> </key> <one-to-many class="com.developer.ammon.StockDailyRecord" /> </set> ... If the Set variable (stockDailyRecords) in Stock object is modified, and save theStock object like following Stock stock = new Stock(); stock.getStockDailyRecords().add(stockDailyRecords); session.save(stock); Will it update the StockDailyRecord table? Hibernate: update developer.stock_daily_record set STOCK_ID=? where DAILY_RECORD_ID=?

4. ANSWER
inverse is controlling the above scenario, to define which side should maintain the relationship. Inverse = false (default) will execute update developer.stock_daily_record to update the relationship. Inverse = true Do nothing.

Got it? May be not, lets explore more examples to understand about it.

INVERSE =

FALSE EXAMPLE

<!--Stock.hbm.xml--> ... <set name="stockDailyRecords" table="stock_daily_record" ...> If no Inverse keyword is declared, the default is inverse = false, which is equal to <!--Stock.hbm.xml--> ... <set name="stockDailyRecords" inverse="false" table="stock_daily_record" ...> which means both sides are the owner of the relationship. In Hibernate, it will enable two sides to update the foreign key stock_daily_record.ITEM_ID in StockDailyRecord table.

Stock will update the foreign key stock_daily_record.ITEM_ID if Set variable (stockDailyRecords) is modified. StockDailyRecord will update the foreign key stock_daily_record.ITEM_ID with Stock property as well.
EXAMPLE

1. INSERT

Heres an insert example for inverse=false, when a Stock object is saved, Hibernate will generated two SQL statements, one insert and one update. Stock stock = new Stock(); stock.getStockDailyRecords().add(stockDailyRecords); session.save(stock); Output Hibernate: insert into developer.stock (STOCK_CODE, STOCK_NAME) values (?, ?) ... Hibernate: update developer.stock_daily_record set STOCK_ID=? where DAILY_RECORD_ID=? Stock will update the stock_daily_record.ITEM_ID through (stockDailyRecords), because Stock is the relationship owner. Set variable

2. UPDATE

EXAMPLE

Heres an update example for inverse=false, Hibernate will generated three SQL statements, one insert and two updates. Query q = session.createQuery("from Stock where stockCode = :stockCode "); q.setParameter("stockCode", "4715"); Stock stock = (Stock)q.list().get(0); stock.setStockName("GENM1"); StockDailyRecord stockDailyRecords = new StockDailyRecord(); //set stockDailyRecords data stockDailyRecords.setStock(stock); stock.getStockDailyRecords().add(stockDailyRecords); session.save(stockDailyRecords); session.update(stock); Output Hibernate: insert into developer.stock_daily_record (STOCK_ID, ...) values (?, ...) Hibernate: update developer.stock set STOCK_CODE=?, STOCK_NAME=? where STOCK_ID=? Hibernate:

update developer.stock_daily_record set STOCK_ID=? where DAILY_RECORD_ID=? Stock will update the stock_daily_record.ITEM_ID through (stockDailyRecords), because Stock is the relationship owner. Set variable

Waitdo you think the third update statement is necessary? The inverse = true in Set variable (stockDailyRecords) can stop the Hibernate to generate the unnecessary third update statement.
INVERSE

= TRUE

EXAMPLE

<!--Stock.hbm.xml--> <set name="stockDailyRecords" inverse="true" table="stock_daily_record" ...> The inverse=true is declared at the Set variable (stockDailyRecords), which means Stock is not the relationship owner, the owner is belong to StockDailyRecord class. In Hibernate, it will enable only the StockDailyRecord class to update the foreign key stock_daily_record.ITEM_ID in StockDailyRecord table. Stock will not update the foreign key stock_daily_record.ITEM_ID if Set variable (stockDailyRecords) is modified. Only StockDailyRecord will update the foreign key stock_daily_record.ITEM_ID with Stock property.
EXAMPLE

1. INSERT

Heres an insert example for inverse=true, when a Stock object is saved, Hibernate will generated one insert SQL statement. Stock stock = new Stock(); stock.getStockDailyRecords().add(stockDailyRecords); session.save(stock); Output Hibernate: insert into developer.stock (STOCK_CODE, STOCK_NAME) values (?, ?) Since Stock is not the owner of the relationship, it will not update the stock_daily_record.ITEM_ID in StockDailyRecord table.

2. UPDATE

EXAMPLE

Heres an update example for inverse=true, Hibernate will generated two SQL statements, one insert and one update. Query q = session.createQuery("from Stock where stockCode = :stockCode "); q.setParameter("stockCode", "4715"); Stock stock = (Stock)q.list().get(0); stock.setStockName("GENM1"); StockDailyRecord stockDailyRecords = new StockDailyRecord(); //set stockDailyRecords data stockDailyRecords.setStock(stock); stock.getStockDailyRecords().add(stockDailyRecords);

session.save(stockDailyRecords); session.update(stock); Output Hibernate: insert into developer.stock_daily_record (STOCK_ID, ...) values (?, ...) Hibernate: update developer.stock set STOCK_CODE=?, STOCK_NAME=? where STOCK_ID=? Since Stock is not the owner of the relationship, it will not update the stock_daily_record.ITEM_ID in stockDailyRecord table.
INVERSE VS CASCADE

Inverse attribute in hibernate defines which side is responsible of the association maintenance. In a one-to-many (bidirectional) or many-to-many relationship the side having inverse=false (default value) has this responsibility (and will create the appropriate SQL query insert, update or delete). Changes made to the association on the side of the inverse=true are not persisted in DB.

A) It informs hibernate to ignore that end of the relationship. If the onetomany was marked as inverse, hibernate would create a child>parent relationship (child.getParent). If the onetomany was marked as noninverse then a child>parent relationship would be created. Q) What do you mean by Named SQL query? A) Named SQL queries are defined in the mapping xml document and called wherever required. Example: <sql-query name = empdetails> <return alias=emp class=com.test.Employee/> SELECT emp.EMP_ID AS {emp.empid}, emp.EMP_ADDRESS AS {emp.address}, emp.EMP_NAME AS {emp.name} FROM Employee EMP WHERE emp.NAME LIKE :name </sql-query> Invoke Named Query : List people = session.getNamedQuery(empdetails) .setString(TomBrady, name) .setMaxResults(50) .list(); Q) How do you invoke Stored Procedures? A) <sql-query name=selectAllEmployees_SP callable=true> <return alias=emp class=employee>

<return-property name=empid column=EMP_ID/> <return-property name=name column=EMP_NAME/> <return-property name=address column=EMP_ADDRESS/> { ? = call selectAllEmployees() } </return> </sql-query> Q) Explain Criteria API A) 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(); Q) Define HibernateTemplate? A) 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. Q) What are the benefits does HibernateTemplate provide? A) 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. Q) How do you switch between relational databases without code changes? A) Using Hibernate SQL Dialects , we can switch databases. Hibernate will generate appropriate hql queries based on the dialect defined. Q) If you want to see the Hibernate generated SQL statements on console, what should we do? A) In Hibernate configuration file set as follows: <property name=show_sql>true</property> Q) What are derived properties? A) 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. People who read this also read: Core Java Questions

Spring Questions SCJP 6.0 Certification EJB Interview Questions Servlets Questions Q) What is component mapping in Hibernate? A) * 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 Q) What is the difference between sorted and ordered collection in hibernate? A) sorted collection vs. order collection sorted collection :A sorted collection is sorting a collection by utilizing the sorting features provided by the Java collections framework. The sorting occurs in the memory of JVM which running Hibernate, after the data being read from database using java comparator. If your collection is not large, it will be more efficient way to sort it. order collection :Order collection is sorting a collection by specifying the order-by clause for sorting this collection when retrieval. If your collection is very large, it will be more efficient way to sort it .

Difference between session.save() , session.saveOrUpdate() and session.persist()? view answer Q.What is the difference between hibernate and jdbc ? view answer Q. What is lazy fetching in Hibernate? With Example . view answer Q.what is the advantage of Hibernate over jdbc? view answer How to Integrate Struts Spring Hibernate ? view answer How to prevent concurrent update in Hibernate? view answer How to perevent slate object updatation in Hibernate ? view answer What is version checking in Hibernate ? view answer How to handle user think time using hibernate ? view answer Q.Transaction with plain JDBC in Hibernate ? view answer

Q.What are the general considerations or best practices for defining your Hibernate persistent classes? view answer Q.Difference between session.update() and session.lock() in Hibernate ? view answer Q.Difference between getCurrentSession() and openSession() in Hibernate ? view answer Difference between session.saveOrUpdate() and session.merge()? view answer Filter in Hibernate with Example? view answer Q.How does Value replacement in Message Resource Bundle work? view answer Difference between list() and iterate() i9n Hibernate? view answer Difference between session.load() and session.get() ? view answer Deleting persistent objects view answer SQL statements execution order. view answer Difference between session.saveOrUpdate() and session.merge(); view answer Modifying persistent objects? view answer SQL Queries In Hibernate.. view answer Filter in Hibernate view answer Criteria Query Two Condition view answer Equal and Not Equal criteria query. view answer Cascade Save or Update in Hibernate ? view answer One To Many Bi-directional Relation in Hibernate? view answer One To Many Mapping Using List ? view answer Many To Many Relation In Hibernate ? view answer What does session.refresh() do ? view answer Difference between session.load() and session.get()? view answer Hibernate setup using .cfg.xml file ? view answer How to add .hbm.xml file in sessionFactory? view answer How to get Hibernate statistics ? view answer

How to set 2nd level cache in hibernate with EHCache? view answer How to get JDBC connections in hibernate? view answer How will you configure Hibernate? view answer How to create Session and SessionFactory in Hibernate? view answer What are the Instance states in Hibernate? view answer What are the core components in Hibernate ? view answer What is a Hibernate Session? Can you share a session object between different theads? view answer addScalar() method in hibernate... view answer Hibernate session.close does _not_ call session.flush ? view answer What is the main difference between Entity Beans and Hibernate ? view answer Difference between session.save() and session.saveOrUpdate()? view answer How are joins handled using Hinernate. view answer What is Hibernate proxy? view answer What is the main advantage of using the hibernate than using the sql ? view answer how to create primary key using hibernate? view answer what is the advantage of Hibernate over jdbc? view answer How to Execute Stored procedure in Hibernate ? view answer what is lazy fetching in hibernate? view answer

) What is Hibernate?
Hibernate is a powerful, high performance object/relational persistence and query service. This lets the users to develop persistent classes following object-oriented principles such as association, inheritance, polymorphism, composition, and collections.

2) What is ORM?
ORM stands for Object/Relational mapping. It is the programmed and translucent perseverance of objects in a Java application in to the tables of a relational database using

the metadata that describes the mapping between the objects and the database. It works by transforming the data from one representation to another.

3) What does an ORM solution comprises of?


It should have an API for performing basic CRUD (Create, Read, Update, Delete) operations on objects of persistent classes Should have a language or an API for specifying queries that refer to the classes and the properties of classes An ability for specifying mapping metadata It should have a technique for ORM implementation to interact with transactional objects to perform dirty checking, lazy association fetching, and other optimization functions

4) What are the different levels of ORM quality?


There are four levels defined for ORM quality. i. ii. iii. iv. Pure relational Light object mapping Medium object mapping Full object mapping

5) What is a pure relational ORM?


The entire application, including the user interface, is designed around the relational model and SQL-based relational operations.

6) What is a meant by light object mapping?


The entities are represented as classes that are mapped manually to the relational tables. The code is hidden from the business logic using specific design patterns. This approach is successful for applications with a less number of entities, or applications with common, metadata-driven data models. This approach is most known to all.

7) What is a meant by medium object mapping?


The application is designed around an object model. The SQL code is generated at build time. And the associations between objects are supported by the persistence mechanism, and queries are specified using an object-oriented expression language. This is best suited for medium-sized applications with some complex transactions. Used when the mapping exceeds 25 different database products at a time.

8) What is meant by full object mapping?


Full object mapping supports sophisticated object modeling: composition, inheritance, polymorphism and persistence. The persistence layer implements transparent persistence; persistent classes do not inherit any special base class or have to implement a special interface. Efficient fetching strategies and caching strategies are implemented transparently to the application.

9) What are the benefits of ORM and Hibernate?


There are many benefits from these. Out of which the following are the most important one. i. ii. iii. Productivity Hibernate reduces the burden of developer by providing much of the functionality and let the developer to concentrate on business logic. Maintainability As hibernate provides most of the functionality, the LOC for the application will be reduced and it is easy to maintain. By automated object/relational persistence it even reduces the LOC. Performance Hand-coded persistence provided greater performance than automated one. But this is not true all the times. But in hibernate, it provides more optimization that works all the time there by increasing the performance. If it is automated persistence then it still increases the performance. Vendor independence Irrespective of the different types of databases that are there, hibernate provides a much easier way to develop a cross platform application.

iv.

10) How does hibernate code looks like?


Session session = getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); MyPersistanceClass mpc = new MyPersistanceClass ("Sample App"); session.save(mpc); tx.commit(); session.close();

The Session and Transaction are the interfaces provided by hibernate. There are many other interfaces besides this.

11) What is a hibernate xml mapping document and how does it look like?
In order to make most of the things work in hibernate, usually the information is provided in an xml document. This document is called as xml mapping document. The document

defines, among other things, how properties of the user defined persistence classes map to the columns of the relative tables in database.
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <class name="sample.MyPersistanceClass" table="MyPersitaceTable"> <id name="id" column="MyPerId"> <generator class="increment"/> </id> <property name="text" column="Persistance_message"/> <many-to-one name="nxtPer" cascade="all" column="NxtPerId"/> </class> </hibernate-mapping>

Everything should be included under tag. This is the main tag for an xml mapping document.

12) Show Hibernate overview?

13) What the Core interfaces are of hibernate framework?


There are many benefits from these. Out of which the following are the most important one. i. ii. iii. iv. Session Interface This is the primary interface used by hibernate applications. The instances of this interface are lightweight and are inexpensive to create and destroy. Hibernate sessions are not thread safe. SessionFactory Interface This is a factory that delivers the session objects to hibernate application. Generally there will be a single SessionFactory for the whole application and it will be shared among all the application threads. Configuration Interface This interface is used to configure and bootstrap hibernate. The instance of this interface is used by the application in order to specify the location of hibernate specific mapping documents. Transaction Interface This is an optional interface but the above three interfaces are mandatory in each and every application. This interface abstracts the code from any kind of transaction implementations such as JDBC transaction, JTA transaction.

v.

Query and Criteria Interface This interface allows the user to perform queries and also control the flow of the query execution.

14) What are Callback interfaces?


These interfaces are 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 callbacks in hibernate applications, but theyre useful for implementing certain kinds of generic functionality.

15) What are Extension interfaces?


When the built-in functionalities provided by hibernate is not sufficient enough, it provides a way so that user can include other interfaces and implement those interfaces for user desire functionality. These interfaces are called as Extension interfaces.

16) What are the Extension interfaces that are there in hibernate?
There are many extension interfaces provided by hibernate.

ProxyFactory interface - used to create proxies ConnectionProvider interface used for JDBC connection management TransactionFactory interface Used for transaction management Transaction interface Used for transaction management TransactionManagementLookup interface Used in transaction management. Cahce interface provides caching techniques and strategies CacheProvider interface same as Cache interface ClassPersister interface provides ORM strategies IdentifierGenerator interface used for primary key generation Dialect abstract class provides SQL support

17) What are different environments to configure hibernate?


There are mainly two types of environments in which the configuration of hibernate application differs. i. Managed environment In this kind of environment everything from database connections, transaction boundaries, security levels and all are defined. An example of this kind of environment is environment provided by application servers such as JBoss, Weblogic and WebSphere.

ii.

Non-managed environment This kind of environment provides a basic configuration template. Tomcat is one of the best examples that provide this kind of environment.

18) What is the file extension you use for hibernate mapping file?
The name of the file should be like this : filenam.hbm.xml The filename varies here. The extension of these files should be .hbm.xml. This is just a convention and its not mandatory. But this is the best practice to follow this extension.

19) What do you create a SessionFactory?


Configuration cfg = new Configuration(); cfg.addResource("myinstance/MyConfig.hbm.xml"); cfg.setProperties( System.getProperties() ); SessionFactory sessions = cfg.buildSessionFactory();

First, we need to create an instance of Configuration and use that instance to refer to the location of the configuration file. After configuring this instance is used to create the SessionFactory by calling the method buildSessionFactory().

20) What is meant by Method chaining?


Method chaining is a programming technique that is supported by many hibernate interfaces. This is less readable when compared to actual java code. And it is not mandatory to use this format. Look how a SessionFactory is created when we use method chaining.
SessionFactory sessions = new Configuration() .addResource("myinstance/MyConfig.hbm.xml") .setProperties( System.getProperties() ) .buildSessionFactory();

21) What does hibernate.properties file consist of?


This is a property file that should be placed in application class path. So when the Configuration object is created, hibernate is first initialized. At this moment the application will automatically detect and read this hibernate.properties file.

hibernate.connection.datasource = java:/comp/env/jdbc/AuctionDB hibernate.transaction.factory_class = net.sf.hibernate.transaction.JTATransactionFactory hibernate.transaction.manager_lookup_class = net.sf.hibernate.transaction.JBossTransactionManagerLookup hibernate.dialect = net.sf.hibernate.dialect.PostgreSQLDialect

22) What should SessionFactory be placed so that it can be easily accessed?


As far as it is compared to J2EE environment, if the SessionFactory is placed in JNDI then it can be easily accessed and shared between different threads and various components that are hibernate aware. You can set the SessionFactory to a JNDI by configuring a property hibernate.session_factory_name in the hibernate.properties file.

23) What are POJOs?


POJO stands for plain old java objects. These are just basic JavaBeans that have defined setter and getter methods for all the properties that are there in that bean. Besides they can also have some business logic related to that property. Hibernate applications works efficiently with POJOs rather then simple java classes.

24) What is object/relational mapping metadata?


ORM tools require a metadata format for the application to specify the mapping between classes and tables, properties and columns, associations and foreign keys, Java types and SQL types. This information is called the object/relational mapping metadata. It defines the transformation between the different data type systems and relationship representations.

25) What is HQL?


HQL stands for Hibernate Query Language. Hibernate allows the user to express queries in its own portable SQL extension and this is called as HQL. It also allows the user to express in native SQL.

26) What are the different types of property and class mappings?

Typical and most common property mapping


<property name="description" column="DESCRIPTION" type="string"/> Or

<property name="description" type="string"> <column name="DESCRIPTION"/> </property>

Derived properties
<property name="averageBidAmount" formula="( select AVG(b.AMOUNT) from BID b where b.ITEM_ID = ITEM_ID )" type="big_decimal"/>

Typical and most common property mapping


<property name="description" column="DESCRIPTION" type="string"/>

Controlling inserts and updates


<property name="name" column="NAME" type="string" insert="false" update="false"/>

27) What is Attribute Oriented Programming?


XDoclet has brought the concept of attribute-oriented programming to Java. Until JDK 1.5, the Java language had no support for annotations; now XDoclet uses the Javadoc tag format (@attribute) to specify class-, field-, or method-level metadata attributes. These attributes are used to generate hibernate mapping file automatically when the application is built. This kind of programming that works on attributes is called as Attribute Oriented Programming.

28) What are the different methods of identifying an object?


There are three methods by which an object can be identified. i. ii. iii. Object identity Objects are identical if they reside in the same memory location in the JVM. This can be checked by using the = = operator. Object equality Objects are equal if they have the same value, as defined by the equals( ) method. Classes that dont explicitly override this method inherit the implementation defined by java.lang.Object, which compares object identity. Database identity Objects stored in a relational database are identical if they represent the same row or, equivalently, share the same table and primary key value.

29) What are the different approaches to represent an inheritance hierarchy?


i. ii. iii. Table per concrete class. Table per class hierarchy. Table per subclass.

30) What are managed associations and hibernate associations?


Associations that are related to container management persistence are called managed associations. These are bi-directional associations. Coming to hibernate associations, these are unidirectional.

Q. How will you configure Hibernate? Answer: The configuration files hibernate.cfg.xml (or hibernate.properties) and mapping files *.hbm.xml are used by the Configuration class to create (i.e. configure and bootstrap hibernate) the SessionFactory, which in turn creates the Session instances. Session instances are the primary interface for the persistence service. " hibernate.cfg.xml (alternatively can use hibernate.properties): These two files are used to configure the hibernate sevice (connection driver class, connection URL, connection username, connection password, dialect etc). If both files are present in the classpath then hibernate.cfg.xml file overrides the settings found in the hibernate.properties file.

" Mapping files (*.hbm.xml): These files are used to map persistent objects to a relational database. It is the best practice to store each object in an individual mapping file (i.e mapping file per class) because storing large number of persistent classes into one mapping file can be difficult to manage and maintain. The naming convention is to use the same name as the persistent (POJO) class name. For example Account.class will have a mapping file named Account.hbm.xml. Alternatively hibernate annotations can be used as part of your persistent class code instead of the *.hbm.xml files. Q. What is a SessionFactory? Is it a thread-safe object? Answer: SessionFactory is Hibernates concept of a single datastore and is threadsafe so that many threads can access it concurrently and request for sessions and immutable cache of compiled mappings for a single database. A SessionFactory is usually only built once at startup. SessionFactory should be wrapped in some kind of singleton so that it can be easily accessed in an application code. SessionFactory sessionFactory = new Configuration().configure().buildSessionfactory();

Q. What is a Session? Can you share a session object between different theads? Answer: Session is a light weight and a non-threadsafe object (No, you cannot share it between threads) that represents a single unit-of-work with the database. Sessions are opened by a SessionFactory and then are closed when all work is complete. Session is the primary interface for the persistence service. A session obtains a database connection lazily (i.e. only when required). To avoid creating too many sessions ThreadLocal class can be used as shown below to get the current session no matter how many times you make call to the currentSession() method. & public class HibernateUtil { & public static final ThreadLocal local = new ThreadLocal(); public static Session currentSession() throws HibernateException { Session session = (Session) local.get(); //open a new session if this thread has no session if(session == null) { session = sessionFactory.openSession(); local.set(session);

} return session; } } It is also vital that you close your session after your unit of work completes. Note: Keep your Hibernate Session API handy. Q. What are the benefits of detached objects? Answer: Detached objects can be passed across layers all the way up to the presentation layer without having to use any DTOs (Data Transfer Objects). You can later on re-attach the detached objects to another session. Q. What are the pros and cons of detached objects? Answer: Pros: " When long transactions are required due to user think-time, it is the best practice to break the long transaction up into two or more transactions. You can use detached objects from the first transaction

to carry data all the way up to the presentation layer. These detached objects get modified outside a transaction and later on re-attached to a new transaction via another session. Cons " In general, working with detached objects is quite cumbersome, and better to not clutter up the session with them if possible. It is better to discard them and re-fetch them on subsequent requests. This approach is not only more portable but also more efficient because - the objects hang around in Hibernate's cache anyway. " Also from pure rich domain driven design perspective it is recommended to use DTOs (DataTransferObjects) and DOs (DomainObjects) to maintain the separation between Service and UI tiers. Q. How does Hibernate distinguish between transient (i.e. newly instantiated) and detached objects? Answer " Hibernate uses the version property, if there is one. " If not uses the identifier value. No identifier value

means a new object. This does work only for Hibernate managed surrogate keys. Does not work for natural keys and assigned (i.e. not managed by Hibernate) surrogate keys. " Write your own strategy with Interceptor.isUnsaved().Hibernate Interview Questions
1) Explain about Hibernate? Hibernate solves problems such as Object Relational impedance mismatch, etc. It is commonly used for object and query service. It helps data base developers develop classes which include inheritance, association, composition and polymorphism. A developer or user can express queries either in HQL or SQL. 2) Explain about the primary feature of Hibernate? Primary feature of hibernate is to java classes to database tables. Data query and retrieval is also possible with Hibernate. Application portability is a key feature in Hibernate it allows developers to port applications to almost all SQL databases. 3) Explain about transparent persistence of Hibernate? Transparent persistence is provided for Plain old Java objects or POJOs. For proper functioning of the applications importance should be given to the methods equals () and hash Code methods (). It has a requirement which should be strictly followed in the applications which is a no-argument constructor. 4) Explain about the dirty checking feature of Hibernate? Dirty checking feature of the Hibernate allows users or developers to avoid time consuming data base write actions. This feature makes necessary updations and changes to the fields which require a change, remaining fields are left unchanged or untouched. 5) Brief about the Session factory interface? It creates new hibernate sessions by referencing immutable and thread safe objects. Application using hibernate are usually allowed and desgined to implement single instance of the class using this interface. Only single instance of a class can be used which is using this interface. 6) Explain about session interface? This represents hibernate session which perform the manipulation on the database entities. Some of the activities performed by session interface are as follows they are managing the persistence state, fetching persisted ones and management of the transaction demarcation.

7) Explain the steps involved in creating database applications with Java using Hibernate? Creating Database applications with Java is made simpler with Hibernate. First Plain old java object needs to be written, XML mapping file should be created which shows relationship between database and class attributes. Hibernate APIs can be used to store persistent objects. 8) Explain about hibernate.cfg.xml? Hibernate can be configured with two types of files out of which hibernate.cfg.xml is widely used and popular feature. Hibernate consults hibernate.cfg.xml file for its operating properties such as database dialect, connection string and mapping files. These files are searched on class path. 9) Explain about mapping description file? Mapping description file is the second file which Hibernate uses to configure its functions. This mapping file has an extension *.hbm which instructs mapping between Java class and database tables. The usage of mapping description file rests entirely upon the business entity. 10) Explain about transaction file? Transactions denote a work file which can save changes made or revert back the changes. A transaction can be started by session.beginTransaction() and it uses JDBC connection, CORBA or JTA. When this session starts several transactions may occur. 11) Explain about mapping files in Hibernate? Mapping files forms the core of any database mapping tools. These files contain field to field mapping, usually this mapping occurs between classes and attributes. After mapping files they can be persist to the database. Tags can be used to indicate the presence of a primary key. 12) What is the effect when a transient mapped object is passed onto a Sessions save? When a Sessions save () is passed to a transient mapped object it makes the method to become more persistent. Garbage collection and termination of the Java virtual machine stays as long as it is deleted explicitly. It may head back to its transient state. 13) Explain about version field? Application level data integrity constants are important if you are making changes to offline information which is again backed by database. Higher level locking or versioning protocol is required to support them. Version field usage comes at this stage but the design and implementation process is left to the developer. 14) State some advantages of hibernate? Some of the advantages which a developer can get from Hibernate are as follows: Mapping of one POJO table to one table is not required in hibernate. It supports inheritance relationships and is generally a fast tool. Portability is necessary

the greater benefit from hibernate. POJOs can be used in other applications where they are applicable. 15) Explain about addClass function? This function translates a Java class name into file name. This translated file name is then loaded as an input stream from the Java class loader. This addclass function is important if you want efficient usage of classes in your code. 16) Explain about addjar() and addDirectory() methods? These methods are the most convenient to use in hibernate. These methods allow you to load all your Hibernate documents at a time. These methods simplify code configuration, refactoring, layout, etc. These functions help you to add your hibernate mapping to Hibernate initialization files. 17) Explain about the id field? This id field corresponds to the surrogate key which is generated by the database. These fields are handled by the id field. Name attribute is used to specify the names of the field and it should correspond to the method name of getid. This also should correspond to long type and the values should be stored I the database in the long column. Latest answer: Object Relational Model is a programming technique. This technique is used to convert the data from an incompatible type to that of a relational database. The mapping is done by using the OOP.............. Read answer

What is Hibernate?
Latest answer: Hibernate is an ORM tool. Hibernate is a solution for object-relational mapping and a persistence management layer. For example a java application is used to save data of an object to a database................. Read answer

Why do you need ORM tools like hibernate?


Latest answer: Hibernate is open source ORM tool. Hibernate reduces the time to perform database operations. Hibernate has advantages over using entity beans or JDBC calls. The implementation is done by just using POJOs............ Read answer

What are the main advantages of ORM like hibernate?


Latest answer: The SQL code / statements in the application can be eliminated without writing complex JDBC / Entity Bean code................. Read answer

What are the core interfaces of Hibernate framework?

Latest answer: Session Interface: The basic interface for all hibernate applications. The instances are light weighted and can be created and destroyed without expensive process............ Read answer

Explain how to configure Hibernate.


Latest answer: Hibernate uses a file by name hibernate.cfg.xml. This file creates the connection pool and establishes the required environment. A file named .hbm.xml is used to author mappings............... Read answer

Define HibernateTemplate.
Latest answer: HibernateTemplate is a helper class that is used to simplify the data access code. This class supports automatically converts HibernateExceptions which is a checked exception into.............. Read answer

What are the benefits of HibernateTemplate?


Latest answer: HibernateTemplate, which is a Spring Template class, can simplify the interactions with Hibernate Sessions............ Read answer

What is Hibernate proxy?


Latest answer: Mapping of classes can be made into a proxy instead of a table. A proxy is returned when actually a load is called on a session........... Read answer

Explain the types of Hibernate instance states.


Latest answer: The persistent classs instance can be in any one of the three different states. These states are defined with a persistence context. The Hibernate has the following instance states:............. Read answer

Explain the Collection types in Hibernate.


Latest answer: A collection is defined as a one-to-many reference. The simplest collection type in Hibernate is........... Read answer

What is lazy initialization in hibernate?

Latest answer: The delaying the object creation or calculating a value or some process until the first time it is needed. The retrieval of particular information only at the time when the object is accessed............ Read answer

What is lazy fetching in hibernate?


Latest answer: Lazy fetching is associated with child objects loading for its parents. While loading the parent, the selection of loading a child object is to be specified / mentioned in the hbm.xml file.............. Read answer

What is the difference between sorted and ordered collection in hibernate?


Latest answer: Sorted Collection The sorted collection is a collection that is sorted using the Java collections framework. The sorting is done in the memory of JVM that is running hibernate, soon after reading the data from the database............ Read answer

Explain the difference between transient (i.e. newly instantiated) and detached objects in hibernate.
Latest answer: Transient objects do not have association with the databases and session objects. They are simple objects and not persisted to the database........... Read answer

Explain the advantages and disadvantages of detached objects.


Latest answer: Detached objects passing can be done across layers upto the presentation layer without using Data Transfer Objects................ Read answer

What is Hibernate Query Language (HQL)?


Latest answer: Hibernate Query Language is designed for data management using Hibernate technology. It is completely object oriented and hence has notions like inheritance, polymorphism and abstraction............ Read answer

Explain the general flow of Hibernate communication with RDBMS?


Latest answer: The Hibernate configuration is to be loaded and creation of configuration object is done. The mapping of all hbm files will be performed automatically............ Read answer

Explain the role of Session interface in Hibernate.


Latest answer: Session interface is a single threaded object. The representation of single unit of work with the Java application and the persistence database is done by this object.......... Read answer

What is a SessionFactory?
Latest answer: The SessionFactory is the concept that is a single data store and thread safe. Because of this feature, many threads can access this concurrently and the sessions are requested, and also............. Read answer

State the role of SessionFactory interface plays in Hibernate.


Latest answer: The SessionFactory is used to create Sessions. Each application is having usually only one SessionFactory........... Read answer

Explain the difference between load() and get() in Hibernate.


Latest answer: load() Use this method if it is sure that the objects exist. The load() method throws an exception,when the unique id could not found in the database.................. Read answer

What is the difference between merge and update?


Latest answer: update () : When the session does not contain an persistent instance with the same identifier, and if it is sure use update for the data persistence in hibernate.......... Read answer

What is the advantage of Hibernate over jdbc?


Latest answer: Hibernate code will work well for all databases, for ex: Oracle,MySQL, etc. where as JDBC is database specific........... Read answer

Why hibernate is advantageous over Entity Beans & JDBC?


Latest answer: An entity bean always works under the EJB container, which allows reusing of the object external to the container. An object can not be detached in entity

beans and in hibernate detached objects are supported.............. Read answer

Explain the main difference between Entity Beans and Hibernate.


Latest answer: Entity beans are to be implemented by containers, classes, descriptors. Hibernate is just a tool that quickly persist the object tree to a class hierarchy in a database and without using a single SQL.................. Read answer

Explain the difference between hibernate and Spring.


Latest answer: Hibernate is an ORM tool for data persistency. Spring is a framework for enterprise applications............... Read answer
Define HibernateTemplate? Explain about Hibernate? What is transactional write-behind? View Answer View Answer

View Answer Explain about the dirty checking feature of Hibernate? Why do you need ORM tools like hibernate? View Answer

View Answer Define cascade and inverse option in one-many mapping? View Answer What are the types of inheritance models in Hibernate? What are Callback interfaces? View Answer

View Answer What is the use of dynamic-insert and dynamic-update attributes in a class mapping? View Answer What are the types of Hibernate instance states ? Brief about the Session factory interface? View Answer

View Answer What are the benefits does HibernateTemplate provide? View Answer How do you switch between relational databases without code changes? How do you invoke Stored Procedures? What do you mean by Named ? SQL query? Explain about the primary feature of Hibernate? View Answer View Answer View Answer View Answer

What are the most common methods of Hibernate configuration? Explain about session interface? Explain about mapping files in Hibernate? View Answer View Answer View Answer What does ORM consists of ?

.What is ORM ?

ORM stands for object/relational mapping. ORM is the automated persistence of objects in a Java application to the tables in a relational database. 2.What does ORM consists of ? An ORM solution consists of the followig four pieces:

API for performing basic CRUD operations API to express queries refering to classes Facilities to specify metadata Optimization facilities : dirty checking,lazy associations fetching

3.What are the ORM levels ? The ORM levels are:


Pure relational (stored procedure.) Light objects mapping (JDBC) Medium object mapping Full object Mapping (composition,inheritance, polymorphism, persistence by reachability)

4.What is Hibernate? Hibernate is a pure Java object-relational mapping (ORM) and persistence framework that allows you to map plain old Java objects to relational database tables using (XML) configuration files.Its purpose is to relieve the developer from a significant amount of relational data persistence-related programming tasks. 5.Why do you need ORM tools like hibernate? The main advantage of ORM like hibernate is that it shields developers from messy SQL. Apart from this, ORM provides following benefits:

Improved productivity o High-level object-oriented API

Less Java code to write No SQL to write Improved performance o Sophisticated caching o Lazy loading o Eager loading Improved maintainability o A lot less code to write Improved portability o ORM framework generates database-specific SQL for you
o o

6.What Does Hibernate Simplify? Hibernate simplifies:


Saving and retrieving your domain objects Making database column and table name changes Centralizing pre save and post retrieve logic Complex joins for retrieving related items Schema creation from object model

7.What is the need for Hibernate xml mapping file? Hibernate mapping file tells Hibernate which tables and columns to use to load and store objects. Typical mapping file look as follows:

8.What are the most common methods of Hibernate configuration? The most common methods of Hibernate configuration are:

Programmatic configuration XML configuration (hibernate.cfg.xml)

9.What are the important tags of hibernate.cfg.xml? Following are the important tags of hibernate.cfg.xml:

10.What are the Core interfaces are of Hibernate framework?

People who read this, also read:The five core interfaces are used in just about every Hibernate application. Using these interfaces, you can store and retrieve persistent objects and control transactions.

Servlets Interview Questions AJAX Questions SCDJWS Certification JSF Integration with Spring Framework JSP Interview Questions

Session interface SessionFactory interface Configuration interface Transaction interface Query and Criteria interfaces 11.What role does the Session

interface play in Hibernate? The Session interface is the primary interface used by Hibernate applications. It is a single-threaded, short-lived object representing a conversation between the application and the persistent store. It allows you to create query objects to retrieve persistent objects.
Session session = sessionFactory.openSession();

Session interface role:


Wraps a JDBC connection Factory for Transaction Holds a mandatory (first-level) cache of persistent objects, used when navigating the object graph or looking up objects by identifier

12.What role does the SessionFactory interface play in Hibernate? The application obtains Session instances from a SessionFactory. There is typically a single SessionFactory for the whole applicationcreated during application initialization. The SessionFactory caches generate SQL statements and other mapping metadata that Hibernate uses at runtime. It also holds cached data that has been read in one unit of work and may be reused in a future unit of work
SessionFactory sessionFactory = configuration.buildSessionFactory();

13.What is the general flow of Hibernate communication with RDBMS? 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

14.What is Hibernate Query Language (HQL)? Hibernate offers a query language that embodies a very powerful and flexible mechanism to query, store, update, and retrieve objects from a database. This language, the Hibernate query Language (HQL), is an object-oriented extension to SQL. 15.How do you map Java Objects with Database tables?

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 :
<hibernate-mapping> <class name="com.test.User" table="user"> <property column="USER_NAME" length="255" name="userName" not-null="true" type="java.lang.String"/> <property column="USER_PASSWORD" length="255" name="userPassword" not-null="true" type="java.lang.String"/> </class> </hibernate-mapping>

16.Whats the difference between load() and get()? load() vs. get() :load() Only use the load() method if you are sure that the object exists. method will throw an exception if the unique id is not found in the database.
load()

get() If you are not sure that the object exists, then use one of the get() methods. method will return null if the unique id is not found in the database.
get()

just returns a proxy by default and database wont be hit until the proxy is first invoked.
load()

will hit the database immediately.


get()

17.What is the difference between and merge and update ?

Use update() if you are sure that the session does not contain an already persistent instance with the same identifier, and merge() if you want to merge your modifications at any time without consideration of the state of the session. 18.How do you define sequence generated primary key in hibernate? Using <generator> tag. Example:<id column="USER_ID" name="id" type="java.lang.Long"> <generator class="sequence"> <param name="table">SEQUENCE_NAME</param> <generator> </id>

19.Define cascade and inverse option in one-many mapping? 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? 20.What do you mean by Named SQL query? Named SQL queries are defined in the mapping xml document and called wherever required. Example:
<sql-query name = "empdetails"> <return alias="emp" class="com.test.Employee"/> SELECT emp.EMP_ID AS {emp.empid}, emp.EMP_ADDRESS AS {emp.address}, emp.EMP_NAME AS {emp.name} FROM Employee EMP WHERE emp.NAME LIKE :name </sql-query>

Invoke Named Query :


List people = session.getNamedQuery("empdetails") .setString("TomBrady", name)

.setMaxResults(50) .list();

21.How do you invoke Stored Procedures?


<sql-query name="selectAllEmployees_SP" callable="true"> <return alias="emp" class="employee"> <return-property name="empid" column="EMP_ID"/> <return-property name="name" column="EMP_NAME"/> <return-property name="address" column="EMP_ADDRESS"/> { ? = call selectAllEmployees() } </return> </sql-query>

22.Explain 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();

23.Define HibernateTemplate? is a helper class which provides different methods for querying/retrieving data from the database. It also converts checked HibernateExceptions into unchecked DataAccessExceptions.
org.springframework.orm.hibernate.HibernateTemplate

24.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. 25.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. 26.If you want to see the Hibernate generated SQL statements on console, what should we do? In Hibernate configuration file set as follows:
<property name="show_sql">true</property>

27.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 People who read this, also read:defined using the formula attribute of the element. JSP Interview Questions Spring - iBatis Integration Struts Tutorial iBatis an alternative to Hibernate 28.What is component mapping in Hibernate?

JSP Interview Questions

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

Example:

29.What is the difference between sorted and ordered collection in hibernate? sorted collection vs. order collection :sorted collection order collection

A sorted collection is sorting a collection by utilizing the sorting features provided by Order collection is sorting a collection by the Java collections framework. The sorting specifying the order-by clause for sorting occurs in the memory of JVM which this collection when retrieval. running Hibernate, after the data being read from database using java comparator. If your collection is not large, it will be more efficient way to sort it. If your collection is very large, it will be more efficient way to sort it .

31.What is the advantage of Hibernate over jdbc? Hibernate Vs. JDBC :JDBC With JDBC, developer has to write code to Hibernate Hibernate is flexible and powerful ORM

map an object model's data representation to a relational data model and its corresponding database schema.

solution to map Java classes to database tables. Hibernate itself takes care of this mapping using XML files so developer does not need to write code for this.

Hibernate provides transparent persistence With JDBC, the automatic mapping of Java and developer does not need to write code objects with database tables and vice versa explicitly to map database tables tuples to conversion is to be taken care of by the application objects during interaction with developer manually with lines of code. RDBMS. Hibernate provides a powerful query language Hibernate Query Language (independent from type of database) that is expressed in a familiar SQL like syntax and includes full support for polymorphic queries. Hibernate also supports native SQL statements. It also selects an effective way to perform a database manipulation task for an application.

JDBC supports only native Structured Query Language (SQL). Developer has to find out the efficient way to access database, i.e. to select effective query from a number of queries to perform same task.

Application using JDBC to handle persistent data (database tables) having database specific code in large amount. The code written to map table data to application objects and vice versa is actually to map table fields to object properties. As table changed or database changed then its essential to change object structure as well as to change code written to map table-to-object/object-to-table. With JDBC, it is developers responsibility to handle JDBC result set and convert it to Java objects through code to use this persistent data in application. So with JDBC, mapping between Java objects and database tables is done manually.

Hibernate provides this mapping itself. The actual mapping between tables and application objects is done in XML files. If there is change in Database or in any table then the only need to change XML file properties.

Hibernate reduces lines of code by maintaining object-table mapping itself and returns result to application in form of Java objects. It relieves programmer from manual handling of persistent data, hence reducing the development time and maintenance cost.

With JDBC, caching is maintained by hand- Hibernate, with Transparent Persistence, coding. cache is set to application work space. Relational tuples are moved to this cache as a result of query. It improves performance if client application reads same data many times for same write. Automatic Transparent Persistence allows the

developer to concentrate more on business logic rather than this application code. Hibernate enables developer to define version type field to application, due to this defined field Hibernate updates version field of database table every time relational tuple is updated in form of Java class object In JDBC there is no check that always to that table. So if two users retrieve same every user has updated data. This check has tuple and then modify it and one user save to be added by the developer. this modified tuple to database, version is automatically updated for this tuple by Hibernate. When other user tries to save updated tuple to database then it does not allow saving it because this user does not have updated data. 32.What are the Collection types in Hibernate ?

Bag Set List Array Map

33.What are the ways to express joins in HQL? 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.

34.Define cascade and inverse option in one-many mapping? 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? 35.What is Hibernate proxy? The proxy attribute enables lazy initialization of persistent instances of the class. Hibernate will initially return CGLIB proxies which implement the named interface. The actual persistent object will be loaded when a method of the proxy is invoked. 36.How can Hibernate be configured to access an instance variable directly and not through a setter method ? 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. 37.How can a whole class be mapped as immutable? Mark the class as mutable="false" (Default is true),. This specifies that instances of the class are (not) mutable. Immutable classes, may not be updated or deleted by the application. 38.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.

(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.
dynamic-update

39.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.

40.What is automatic dirty checking? 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. 41.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 People who read this, also read:predictable to the user. This feature is called transactional write-behind. BREW Interview Questions Spring - iBatis Integration SCEA Certification Hibernate Vs iBatis 42.What are Callback interfaces? EJB Interview Questions 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. 43.What are the types of Hibernate instance states ? Three types of instance states:

Transient -The instance is not associated with any persistence context Persistent -The instance is associated with a persistence context Detached -The instance was associated with a persistence context which has been closed currently not associated

44.What are the differences between EJB 3.0 & Hibernate Hibernate Vs EJB 3.0 :Hibernate SessionCache or collection of loaded objects relating to a single unit of work EJB 3.0 Persistence Context-Set of entities that can be managed by a given EntityManager is defined by a persistence unit

XDoclet Annotations used to support Attribute Oriented Programming Defines HQL for expressing queries to the database Supports Entity Relationships through mapping files and annotations in JavaDoc

Java 5.0 Annotations used to support Attribute Oriented Programming Defines EJB QL for expressing queries Support Entity Relationships through Java 5.0 annotations

Provides a Persistence Manager API Provides and Entity Manager Interface exposed via the Session, Query, Criteria, and for managing CRUD operations for an Transaction API Entity Provides callback support through lifecycle, interceptor, and validatable interfaces Entity Relationships are unidirectional. Bidirectional relationships are implemented by two unidirectional relationships Provides callback support through Entity Listener and Callback methods Entity Relationships are bidirectional or unidirectional

45.What are the types of inheritance models in Hibernate? There are three types of inheritance models in Hibernate:

Table per class hierarchy Table per subclass Table per concrete class

A simple java application uses Hibernate core to access db. Which of the following statements are true?

2
What do you mean by CRUD operation?

3
An Hibernate core application has following files in the classptah directory with following entries:

In hibernate.properties: hibernate.connection.url = jdbc:mysql://localhost/myAppDB1

and in hibernate.cfg.xml file we have: <property name="hibernate.connection.url"> jdbc:mysql://localhost/myAppDB2 </property>

Given the files have the above statements in correct places in the file, when hibernate gets initialized, to which db the Hibernate will be connected?

4
Which of the following statements are true ?

5
What is dirty checking? When will this happen?

6
What are Transient objects in Hibernate?

7
I am using JPA implementation of Hibernate, with JPA XML descriptors for my persistent objects. Now can we use Hibernate tags inside JPA xml descriptors for special behaviours only supported by Hibernate?

8
What is the use of adopting Hibernate's JPA implementation with JPA based XML descriptors and Entity managers in my project?

9
How can one consider that the persistent objects representing rows of a table are identical?

10
Which of the following identifier - generator modules are defined in Hibernate core?

Hibernate Interview Questions


1. What is Hibernate? Hibernate is a powerful, high performance object/relational persistence and query service. This lets the users to develop persistent classes following objectoriented principles such as association, inheritance, polymorphism, composition, and collections. 2. What is ORM? ORM stands for Object/Relational mapping. It is the programmed and translucent perseverance of objects in a Java application in to the tables of a relational database using the metadata that describes the mapping between the objects and the database. It works by transforming the data from one representation to another. 3. What does an ORM solution comprises of? It should have an API for performing basic CRUD (Create, Read, Update, Delete) operations on objects of persistent classes Should have a language or an API for specifying queries that refer to the classes and the properties of classes An ability for specifying mapping metadata

It should have a technique for ORM implementation to interact with transactional objects to perform dirty checking, lazy association fetching, and other optimization functions 4. What are the different levels of ORM quality? There are four levels defined for ORM quality. i. Pure relational ii. Light object mapping iii. Medium object mapping iv. Full object mapping 5. What is a pure relational ORM? The entire application, including the user interface, is designed around the relational model and SQL-based relational operations. 6. What is a meant by light object mapping? The entities are represented as classes that are mapped manually to the relational tables. The code is hidden from the business logic using specific design patterns. This approach is successful for applications with a less number of entities, or applications with common, metadata-driven data models. This approach is most known to all. 7. What is a meant by medium object mapping? The application is designed around an object model. The SQL code is generated at build time. And the associations between objects are supported by the persistence mechanism, and queries are specified using an object-oriented expression language. This is best suited for medium-sized applications with some complex transactions. Used when the mapping exceeds 25 different database products at a time. 8. What is meant by full object mapping? Full object mapping supports sophisticated object modeling: composition, inheritance, polymorphism and persistence. The persistence layer implements transparent persistence; persistent classes do not inherit any special base class or have to implement a special interface. Efficient fetching strategies and caching strategies are implemented transparently to the application. 9. What are the benefits of ORM and Hibernate? There are many benefits from these. Out of which the following are the most important

one. i. Productivity Hibernate reduces the burden of developer by providing much of the functionality and let the developer to concentrate on business logic. ii. Maintainability As hibernate provides most of the functionality, the LOC for the application will be reduced and it is easy to maintain. By automated object/relational persistence it even reduces the LOC. iii. Performance Hand-coded persistence provided greater performance than automated one. But this is not true all the times. But in hibernate, it provides more optimization that works all the time there by increasing the performance. If it is automated persistence then it still increases the performance. iv. Vendor independence Irrespective of the different types of databases that are there, hibernate provides a much easier way to develop a cross platform application. 10. How does hibernate code looks like? Session session = getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); MyPersistanceClass mpc = new MyPersistanceClass ("Sample App"); session.save(mpc); tx.commit(); session.close(); The Session and Transaction are the interfaces provided by hibernate. There are many other interfaces besides this. 11. What is a hibernate xml mapping document and how does it look like? In order to make most of the things work in hibernate, usually the information is provided in an xml document. This document is called as xml mapping document. The document defines, among other things, how properties of the user defined persistence classes map to the columns of the relative tables in database. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <class name="sample.MyPersistanceClass" table="MyPersitaceTable"> <id name="id" column="MyPerId">

<generator class="increment"/> </id> <property name="text" column="Persistance_message"/> <many-to-one name="nxtPer" cascade="all" column="NxtPerId"/> </class> </hibernate-mapping> Everything should be included under <hibernate-mapping> tag. This is the main tag for an xml mapping document. 12. Show Hibernate overview?
`

13. What the Core interfaces are of hibernate framework? There are many benefits from these. Out of which the following are the most important one. i. Session Interface This is the primary interface used by hibernate applications. The instances of this interface are lightweight and are inexpensive to create and destroy. Hibernate sessions are not thread safe. ii. SessionFactory Interface This is a factory that delivers the session objects to hibernate application. Generally there will be a single SessionFactory for the whole application and it will be shared among all the application threads. iii. Configuration Interface This interface is used to configure and bootstrap hibernate. The instance of this interface is used by the application in order to specify the location of hibernate specific mapping documents. iv. Transaction Interface This is an optional interface but the above three interfaces are mandatory in each and every application. This interface abstracts the code from any kind of transaction implementations such as JDBC transaction, JTA transaction. v. Query and Criteria Interface This interface allows the user to perform queries and also control the flow of the query execution. 14. What are Callback interfaces? These interfaces are 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 callbacks in hibernate applications, but theyre useful for implementing

certain kinds of generic functionality. 15. What are Extension interfaces? When the built-in functionalities provided by hibernate is not sufficient enough, it provides a way so that user can include other interfaces and implement those interfaces for user desire functionality. These interfaces are called as Extension interfaces. 16. What are the Extension interfaces that are there in hibernate? There are many extension interfaces provided by hibernate. ProxyFactory interface - used to create proxies ConnectionProvider interface used for JDBC connection management TransactionFactory interface Used for transaction management Transaction interface Used for transaction management TransactionManagementLookup interface Used in transaction management. Cahce interface provides caching techniques and strategies CacheProvider interface same as Cache interface ClassPersister interface provides ORM strategies IdentifierGenerator interface used for primary key generation Dialect abstract class provides SQL support 17. What are different environments to configure hibernate? There are mainly two types of environments in which the configuration of hibernate application differs. i. Managed environment In this kind of environment everything from database connections, transaction boundaries, security levels and all are defined. An example of this kind of environment is environment provided by application servers such as JBoss, Weblogic and WebSphere. ii. Non-managed environment This kind of environment provides a basic configuration template. Tomcat is one of the best examples that provide this kind of environment. 18. What is the file extension you use for hibernate mapping file? The name of the file should be like this : filename.hbm.xml The filename varies here. The extension of these files should be .hbm.xml. This is just a convention and its not mandatory. But this is the best practice to follow this

extension. 19. What do you create a SessionFactory? Configuration cfg = new Configuration(); cfg.addResource("myinstance/MyConfig.hbm.xml"); cfg.setProperties( System.getProperties() ); SessionFactory sessions = cfg.buildSessionFactory(); First, we need to create an instance of Configuration and use that instance to refer to the location of the configuration file. After configuring this instance is used to create the SessionFactory by calling the method buildSessionFactory(). 20. What is meant by Method chaining? Method chaining is a programming technique that is supported by many hibernate interfaces. This is less readable when compared to actual java code. And it is not mandatory to use this format. Look how a SessionFactory is created when we use method chaining. SessionFactory sessions = new Configuration() .addResource("myinstance/MyConfig.hbm.xml") .setProperties( System.getProperties() ) .buildSessionFactory(); 21. What does hibernate.properties file consist of? This is a property file that should be placed in application class path. So when the Configuration object is created, hibernate is first initialized. At this moment the application will automatically detect and read this hibernate.properties file. hibernate.connection.datasource = java:/comp/env/jdbc/AuctionDB hibernate.transaction.factory_class = net.sf.hibernate.transaction.JTATransactionFactory hibernate.transaction.manager_lookup_class = net.sf.hibernate.transaction.JBossTransactionManagerLookup hibernate.dialect = net.sf.hibernate.dialect.PostgreSQLDialect 22. What should SessionFactory be placed so that it can be easily accessed? As far as it is compared to J2EE environment, if the SessionFactory is placed in JNDI then it can be easily accessed and shared between different threads and various components that are hibernate aware. You can set the SessionFactory to a JNDI by configuring a property hibernate.session_factory_name in the hibernate.properties file.

23. What are POJOs? POJO stands for plain old java objects. These are just basic JavaBeans that have defined setter and getter methods for all the properties that are there in that bean. Besides they can also have some business logic related to that property. Hibernate applications works efficiently with POJOs rather then simple java classes. 24. What is object/relational mapping metadata? ORM tools require a metadata format for the application to specify the mapping between classes and tables, properties and columns, associations and foreign keys, Java types and SQL types. This information is called the object/relational mapping metadata. It defines the transformation between the different data type systems and relationship representations. 25. What is HQL? HQL stands for Hibernate Query Language. Hibernate allows the user to express queries in its own portable SQL extension and this is called as HQL. It also allows the user to express in native SQL. 26. What are the different types of property and class mappings? Typical and most common property mapping <property name="description" column="DESCRIPTION" type="string"/> Or <property name="description" type="string"> <column name="DESCRIPTION"/> </property> Derived properties <property name="averageBidAmount" formula="( select AVG(b.AMOUNT) from BID b where b.ITEM_ID = ITEM_ID )" type="big_decimal"/> Typical and most common property mapping <property name="description" column="DESCRIPTION" type="string"/> Controlling inserts and updates <property name="name" column="NAME" type="string" insert="false" update="false"/> 27. What is Attribute Oriented Programming? XDoclet has brought the concept of attribute-oriented programming to Java. Until JDK

1.5, the Java language had no support for annotations; now XDoclet uses the Javadoc tag format (@attribute) to specify class-, field-, or method-level metadata attributes. These attributes are used to generate hibernate mapping file automatically when the application is built. This kind of programming that works on attributes is called as Attribute Oriented Programming. 28. What are the different methods of identifying an object? There are three methods by which an object can be identified. i. Object identity Objects are identical if they reside in the same memory location in the JVM. This can be checked by using the = = operator. ii. Object equality Objects are equal if they have the same value, as defined by the equals( ) method. Classes that dont explicitly override this method inherit the implementation defined by java.lang.Object, which compares object identity. iii. Database identity Objects stored in a relational database are identical if they represent the same row or, equivalently, share the same table and primary key value. 29. What are the different approaches to represent an inheritance hierarchy? i. Table per concrete class. ii. Table per class hierarchy. iii. Table per subclass. 30. What are managed associations and hibernate associations? Associations that are related to container management persistence are called managed associations. These are bi-directional associations. Coming to hibernate associations, these are unidirectional. Explain Criteria API
View Answer How can a whole class be mapped as immutable? What is automatic dirty checking? View Answer

View Answer How do you define sequence generated primary key in hibernate? View Answer If you want to see the Hibernate generated SQL statements on console, what should we do? Explain about mapping description file? View Answer

View Answer How can Hibernate be configured to access an instance variable directly and not through a setter method ? Explain about hibernate.cfg.xml? View Answer

View Answer Explain about addjar() and addDirectory() methods? What are the Collection types in Hibernate ? What are the ORM levels ? What is Hibernate Query Language (HQL)? Explain about transaction file? State some advantages of hibernate? What?s the difference between load() and get()? What are derived properties? View Answer View Answer View Answer View Answer View Answer View Answer View Answer

View Answer What is the difference between sorted and ordered collection in hibernate? What is ORM ? View Answer

View Answer What is the general flow of Hibernate communication with RDBMS? View Answer What is component mapping in Hibernate?

What is Hibernate?

Why Hibernate? What is ORM? What are core interfaces of Hibernate Framework? What is dirty checking in Hibernate? What are different fetch strategies Hibernate have? Can you compare JDBC/DAO with Hibernate? Explain different inheritance mapping models in Hibernate More Hibernate Questionsibernate Interview Questions

1) Explain about Hibernate? Hibernate solves problems such as Object Relational impedance mismatch, etc. It is commonly used for object and query service. It helps data base developers develop classes which include inheritance, association, composition and polymorphism. A developer or user can express queries either in HQL or SQL. 2) Explain about the primary feature of Hibernate? Primary feature of hibernate is to java classes to database tables. Data query and retrieval is also possible with Hibernate. Application portability is a key feature in Hibernate it allows developers to port applications to almost all SQL databases. 3) Explain about transparent persistence of Hibernate? Transparent persistence is provided for Plain old Java objects or POJOs. For proper functioning of the applications importance should be given to the methods equals () and hash Code methods (). It has a requirement which should be strictly followed in the applications which is a no-argument constructor. 4) Explain about the dirty checking feature of Hibernate? Dirty checking feature of the Hibernate allows users or developers to avoid time consuming data base write actions. This feature makes necessary updations and changes to the fields which require a change, remaining fields are left unchanged or untouched. 5) Brief about the Session factory interface? It creates new hibernate sessions by referencing immutable and thread safe objects. Application using hibernate are usually allowed and desgined to implement single instance of the class using this interface. Only single instance of a class can be used which is using this interface. 6) Explain about session interface? This represents hibernate session which perform the manipulation on the database entities. Some of the activities performed by session interface are as follows they are managing the persistence state, fetching persisted ones and management of the transaction demarcation. 7) Explain the steps involved in creating database applications with Java using Hibernate? Creating Database applications with Java is made simpler with Hibernate. First Plain old java object needs to be written, XML mapping file should be created which shows relationship between database and class attributes. Hibernate APIs can be used to store persistent objects. Explain about hibernate.cfg.xml? Hibernate can be configured with two types of files out of which hibernate.cfg.xml is widely used and popular feature. Hibernate consults hibernate.cfg.xml file for its operating properties such as database dialect, connection string and mapping files. These files are searched on class path.

9) Explain about mapping description file? Mapping description file is the second file which Hibernate uses to configure its functions. This mapping file has an extension *.hbm which instructs mapping between Java class and database tables. The usage of mapping description file rests entirely upon the business entity. 10) Explain about transaction file? Transactions denote a work file which can save changes made or revert back the changes. A transaction can be started by session.beginTransaction() and it uses JDBC connection, CORBA or JTA. When this session starts several transactions may occur. 11) Explain about mapping files in Hibernate? Mapping files forms the core of any database mapping tools. These files contain field to field mapping, usually this mapping occurs between classes and attributes. After mapping files they can be persist to the database. Tags can be used to indicate the presence of a primary key. 12) What is the effect when a transient mapped object is passed onto a Sessions save? When a Sessions save () is passed to a transient mapped object it makes the method to become more persistent. Garbage collection and termination of the Java virtual machine stays as long as it is deleted explicitly. It may head back to its transient state. 13) Explain about version field? Application level data integrity constants are important if you are making changes to offline information which is again backed by database. Higher level locking or versioning protocol is required to support them. Version field usage comes at this stage but the design and implementation process is left to the developer. 14) State some advantages of hibernate? Some of the advantages which a developer can get from Hibernate are as follows: Mapping of one POJO table to one table is not required in hibernate. It supports inheritance relationships and is generally a fast tool. Portability is necessary the greater benefit from hibernate. POJOs can be used in other applications where they are applicable. 15) Explain about addClass function? This function translates a Java class name into file name. This translated file name is then loaded as an input stream from the Java class loader. This addclass function is important if you want efficient usage of classes in your code. 16) Explain about addjar() and addDirectory() methods? These methods are the most convenient to use in hibernate. These methods allow you to load all your Hibernate documents at a time. These methods simplify code configuration, refactoring, layout, etc. These functions help you to add your hibernate mapping to Hibernate initialization files.

17) Explain about the id field? This id field corresponds to the surrogate key which is generated by the database. These fields are handled by the id field. Name attribute is used to specify the names of the field and it should correspond to the method name of getid. This also should correspond to long type and the values should be stored I the database in the long column. Read more: http://discuss.itacumens.com/index.php?topic=49191.0#ixzz15edJDikP . What are the Collection types in Hibernate? i. ii. iii. iv. v. Bag Set List Array Map

2. What is the differnece between load() and get() methods? When we use both of them? load() i. ii. iii. get() i. ii. iii. Use get() method when you are not sure that the object exists. It returns null if the unique id is not found in the database. get() will hit the database immediately. Use load() method when you are sure the object exists. It throw an exception if the unique id is not found in the database. It just returns a proxy by default and database won't be hit until the proxy is first invoked.

Relate Links: First Hibernate Progarm Many To One Association One To One Association One To Many Association Many To Many Association Collection Types In Hibernate

1. )What is Hibernate? 2)What is ORM? 3)What does an ORM solution comprises of? 4)What are the different levels of ORM quality?

5)What is a pure relational ORM? 6)What is a meant by light object mapping? 7)What is a meant by medium object mapping? 8)What is meant by full object mapping? 9)What are the benefits of ORM and Hibernate? 10)How does hibernate code looks like? 11)What is a hibernate xml mapping document and how does it look like? 12)Show Hibernate overview? 13)What the Core interfaces are of hibernate framework? 14)What are Callback interfaces? 15)What are Extension interfaces? 16)What are the Extension interfaces that are there in hibernate? 17)What are different environments to configure hibernate? 18)What is the file extension you use for hibernate mapping file? 19)What do you create a SessionFactory? 20)What is meant by Method chaining? 21)What does hibernate.properties file consist of? 22)What should SessionFactory be placed so that it can be easily accessed? 23)What are POJOs? 24)What is object/relational mapping metadata? 25)What is HQL? 26)What are the different types of property and class mappings? 27)What is Attribute Oriented Programming? 28)What are the different methods of identifying an object? 29)What are the different approaches to represent an inheritance hierarchy? 30)What are managed associations and hibernate associations? What is the configuration normally resorted to configure Hibernate? 2. What do you understand by Data Transfer Objects (DTOs)? 3. Can you tell us 3 basic advantages and disadvantages of (DTOs)? 4. Tell us how is the distinction made by a Hibernate between the detached objects and transient? 5. What are considered to be the best ways to define the Hibernate persistent classes? 6. Briefly tell the difference between the session.load() method and session.get() method ? 7. What procedure will you resort to for reattaching the detached objects to a particular session when you find the same object being loaded into the session? 8. What do you mean by version checking in the Hibernate? 9. Can you tell what advantage Hibernate has over the jdbc? 10. Tell us with example about filter in Hibernate. 11. Please tell us the difference between iterate () and list () in Hibernate? 12. How much experience do you have working in this job profile?

You might also like