Hibernate - Interview Questions and Answers for 'Hibernate' | Search Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com

Search Interview Questions


 More than 3000 questions in repository.
 There are more than 900 unanswered questions.
Click here and help us by providing the answer.
 Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text

   



Interview Questions and Answers - Order By Rating

   next 30
 Q31. What are the disadvantages of Hibernate over native APIs ?Hibernate
 This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     


 Q32. Do you like Native SQL Apis like JDBC or JPA / Hibernate ?Hibernate
 This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     JDBC vs JPA  JDBC vs Hibernate


 Q33. If you are given choice to avoid LazyInitializationException using any of the following measures, which are the ones you will choose and why ?

1. Set lazy=false in the hibernate config file.
2. Set @Basic(fetch=FetchType.EAGER) at the mapping.
3. Make sure that we are accessing the dependent objects before closing the session.
4. Force initialization using Hibernate.initialize
Hibernate
Ans. First resolution is a big No as it conveys no lazy loading in complete app. even second is advocating the same but for a particular mapping.

third one is most appropriate if loading and dependent entity property access is closer to each other in code and can be accomplished.

I don't mind using 4th too.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     lazy loading  lazy initialization  LazyInitializationException


 Q34. What does the following exception means

org.hibernate.LazyInitializationException: could not initialize proxy - no Session
Hibernate
Ans. The error states that Hibernate is not able to initialize proxy / dependent entity objects as there is no session or transaction present. Very likely we are trying to load the dependent entities lazily but the call to dependent object property is not wrapped within the session or transaction.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     Lazy Loading  Lazy Initialization  org.hibernate.LazyInitializationException


 Q35. If we would like to load just the mapping id for the mapped entity in some cases and whole entity in some cases, because we would like to lazily initialize the dependent entity in some case and not load the entity at all in some case, how can we accomplish that.

For example -

Employee entity is mapped to department entity through department id. We want that when we load employee.getDepartment().getDepartmentId(), it shouldn't load the complete department object but just the Id.
Hibernate
Ans. a

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     


 Q36. If we would like to load just the mapping id for the mapped entity, how can we accomplish that.

For example -

Employee entity is mapped to department entity through department id. We want that when we load employee.getDepartment().getDepartmentId(), it shouldn't load the complete department object but just the Id.
Hibernate
Ans. We can use

@AccessType("property") for getDepartmentId() method within Department class in that case.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     


 Q37. Can we have 2 entity fields mapped to the same column in Hibernate ?Hibernate
Ans. No, it's not legal in hibernate. 1 column should be mapped to only one field in entity.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     


 Q38. What are the methods to connect to database in Java ?Database
Ans. JDBC ( Java Database Connectivity ),ODBC (Open Database Connectivity), Hibernate, JPA ( Java Persistence API ), JOOQ,MyBatis

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     database connection  jdbc  ORM  Hibernate     Asked in 1 Companies


 Q39. What is a fetch mode in Hibernate ?Hibernate
Ans. It specifies an association fetching strategy. It could be DEFAULT, JOIN or SELECT as following

@Fetch(FetchMode.SELECT)

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     


 Q40. Difference between save and flush in Hibernate ?Hibernate
Ans. Save is used to save the entity to the session whereas flush is to save the session content to DB.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     


 Q41. How does Lazy Initialization helps improving performance of an application ?Hibernate
Ans. Lazy Initialization means , Load Dependencies when required. Which means less load on application resources as only required data is loaded upfront. It's not only good for better performance but for better resource utilization too.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     lazy loading  lazy initialization        frequent


 Q42. What are the points to be considered if we move from Eager initialization to Lazy Initialization in Hibernate ?Hibernate
Ans. Make sure that the properties of dependent Hibernate entities are not accessed and if yes, better wrap the whole code within single transaction.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     lazy loading  lazy initialization


 Q43. How are DB transactions handled in Hibernate ? Hibernate
Ans. // Non-managed environment idiom
Session sess = factory.openSession();
Transaction tx = null;
try {
tx = sess.beginTransaction();

// do some work
...

tx.commit();
}
catch (RuntimeException e) {
if (tx != null) tx.rollback();
throw e; // or display error message
}
finally {
sess.close();
}

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve          Asked in 1 Companies


 Q44. Can we have an entity in hibernate without primary Key ?Hibernate
Ans. No, Every entity in hibernate needs to have a key, either primary or composite.

If we dont have a primary key on table, there are various ways this problem can be countered.

1. By using composite key on entity ( make sure that the appropriate unique constraint in defined on columns in Database )

2. By mapping Id in entity to ROWID of table.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     hibernate entity  hibernate


 Q45. Difference between JDBC and Hibernate ?Database
Ans. JDBC is a standard Java Api for Database communication whereas Hibernate is an ORM framework that uses JDBC to connect with Database.

Hibernate is another layer of object table mapping over JDBC that provide a database independent, object oriented and an efficient way of database communication.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     JDBC  Hibernate  JDBC vs Hibernate     Asked in 3 Companies


 Q46. How to specify the or/and combination restrictions within Criteria in Hibernate ? Hibernate
Ans. session.createCriteria(Employee.class).add( Restrictions.or(Restrictions.like("name", "A%"),Restrictions.like("name", "B%"))).list();

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     hibernate  Criteria  Restrictions


Frequently asked question in companies using hibernate.
  Q47. Difference between first level and second level cache in hibernate ?Hibernate
Ans. 1. First level cache is enabled by default whereas Second level cache needs to be enabled explicitly.

2. First level Cache came with Hibernate 1.0 whereas Second level cache came with Hibernate 3.0.

3. First level Cache is Session specific whereas Second level cache is shared by sessions that is why First level cache is considered local and second level cache is considered global.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     hibernate   orm   hibernate cache   architecture   technical lead   first level cache vs second level cache     Asked in 18 Companies      Intermediate        frequent


 Q48. What are the the methods to clear cache in Hibernate ?Hibernate
Ans. Evict() and clear(). Evist is used to clear a particular object from the cache whereas clear clears the complete local cache.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     hibernate   orm   hibernate cache


 Q49. What are different types of second level cache ?Hibernate
Ans. 1. EHCache ( Easy Hibernate )
2. OSCache ( Open Symphony )
3. Swarm Cache ( JBoss )
4. Tree Cache ( JBoss )

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     hibernate   orm   hibernate cache   technical lead     Asked in 7 Companies


 Q50. Can we disable first level cache ? What should one do if we don't want an object to be cached ?Hibernate
Ans. No.We can either call evict after the object retrieval or can use separate sessions.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     hibernate   orm   hibernate cache   architecture


 Q51. How to configure second level cache in Hibernate ?Hibernate
Ans. 1. Configure Provider class in Hibernate configuration file.

2. Add Cache usage tag ( read-only or read-write ) in mapping files ( hbm ).

3. Create an XML file called ehcache.xml and place in classpath which contains time settings and update settings, behavior of cache , lifetime and idletime of Pojos, how many objects are allowed.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     hibernate   orm   hibernate cache   architecture


 Q52. What is Hibernate ?Hibernate
Ans. Hibernate is a Java ORM Framework.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     hibernate   orm   frameworks   architecture


 Q53. What are the advantages of Hibernate ?Hibernate
Ans. 1. No need to know SQL, RDBMS, and DB Schema.
2. Underlying Database can be changed without much effort by changing SQL dialect and DB connection.
3.Improved Performance by means of Caching.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     hibernate   orm   architecture


 Q54. What are the different types of inheritance in Hibernate ?Hibernate
Ans. Table Per Class , Table per Sub Class , Table per Concrete Class

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     hibernate   orm   inheritance hibernate     Asked in 1 Companies      Basic        frequent


 Q55. What is the purpose of dialect configured in Hibernate configuration file ?Hibernate
Ans. It tells the framework which SQL varient to generate.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     hibernate   orm   hibernate configuration


 Q56. Please specify in what sequence the objects of following classes will be created ?

Session , SessionFactory, Query , Configuration
Hibernate
Ans. Configuration -> SessionFactory -> Session -> Query

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     hibernate


Very frequently asked Hibernate interview question. Frequently asked in TCS ( based on 2 feedback )
  Q57. What are different types of associations in Hibernate ?Hibernate
Ans. There are 4 types of associations in Hibernate

One to One
One to Many
Many to One
Many to Many

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     hibernate   associations     Asked in 11 Companies        frequent


 Q58. What are the configuration files in Hibernate ?Hibernate
Ans. hibernate.cfg.xml ( Main Configuration File )

and *.hbm.xml files ( Mapping Files )

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     hibernate   configuration   mapping files


 Q59. What are the contents of Hibernate configuration file ( hibernate.cfg.xml ) ?Hibernate
Ans. HBM Files ( Mapping )
DB Connection ( DB Connection String , User Name , Password , Pool Size )
SQL Dialect ( SQL variant to be generated )
Show SQL ( Show / No show SQL on Console )
Auto Commit ( True / False )

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     hibernate   configuration   barclays     Asked in 11 Companies


 Q60. What are the Core Interfaces of Hibernate Framework ? Hibernate
Ans. Configuration
SessionFactory
Session
Transaction
Query and Citeria

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     hibernate


previous 30   next 30

Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked: