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 Newest

   next 30
 Q41. Can we declare Entity class as final ?Hibernate
Ans. Yes but as Hibernate creates the Proxy Classes inherited from the Entity Classes to communicate with Database for lazy initialization. Declaring entity classes as final will prohibit communication with database lazily and hence will be a performance hit.

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

   Like         Discuss         Correct / Improve     hibernate   proxy objects   proxy classes   entity hibernate


 Q42. What is the difference between JPA and Hibernate ?Hibernate
Ans. JPA or Java Persistence API is a standard specification for ORM implementations whereas Hibernate is the actual ORM implementation or framework.

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

   Like         Discuss         Correct / Improve     jpa   hibernate   orm   architecture   technologies     Asked in 2 Companies


 Q43. What is the advantage of JPA ?Database
Ans. Its a specification that guides the implementation of ORM frameworks. Implementations abiding by the specification would mean that one can be replaced with other in an application without much hassle. Only the Features that are added over the specification needs to be taken care of if any such change is made.

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

   Like         Discuss         Correct / Improve     jpa   hibernate   orm   technologies


 Q44. What is Criteria in Hibernate ?Hibernate
Ans. Criteria is a simplified API for retrieving entities by composing Criterion objects.

For example - session.createCriteria(Employee.class).add( Restrictions.like("name", "A%") ).list();

will return all employee objects having name starting with A.

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

   Like         Discuss         Correct / Improve     hibernate   hibernate criteria     Asked in 7 Companies      Basic        Frequent

Try 1 Question(s) Test


 Q45. How do we specify the criteria if it involves mapping between two entities or join between tables ?Hibernate
Ans. The following code returns the list of Employee objects having employee name starting with A and Dept Name ( Department , Employee Mapped ).

session.createCriteria(Employee.class,"emp")
.createAlias("emp.department", "dept",Criteria.INNER_JOIN)
.add( Restrictions.like("name", "A%") )
.add(Restrictions.eq("dept.name","Finance")
.list();

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

   Like         Discuss         Correct / Improve     hibernate   hibernate criteria   hibernate table mapping


 Q46. Name few Restriction Methods ?Hibernate
Ans. eq, ge, gt , between, in , isNull, isEmpty, isNotnull, ne , like, lt , or , not

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

   Like         Discuss         Correct / Improve     hibernate   hibernate criteria   hibernate restriction


 Q47. In Hibernate, While defining Criteria, Have you ever faced any problem while adding restrictions with user defined types or Enums ?Hibernate
Ans. Yes, with Enum as was getting an exception while doing equality check for enum field.

got it fixed by adding @Enumerated(EnumType.STRING) to the field in entity.

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

   Like         Discuss         Correct / Improve     hibernate   hibernate criteria   enumerations


 Q48. What is your choice while writing queries within Hibernate - criteria or HQL ?Hibernate
Ans. I prefer using Criteria for Dynamic queries and HQL for static queries.

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

   Like         Discuss         Correct / Improve     hibernate   hibernate criteria   hql


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


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


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


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


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


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


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


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


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


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


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


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


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


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


 Q63. What was the worst problem you faced while working with Hibernate ?Hibernate
Ans. Lazy Initialization exceptions

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

   Like         Discuss         Correct / Improve     


 Q64. Is it advisable to keep session or transaction open for long time just to avoid LazyInitializationException ?Hibernate
Ans. No. It's a resource and performance overhead.

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

   Like         Discuss         Correct / Improve     LazyInitializationException  Lazy Loading


 Q65. Does Hibernate synchronize entity with the database automatically i.e any change to entity gets updated in DB ?Hibernate
Ans. Yes, the feature is there but can be override with changing the Flush Mode.

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

   Like         Discuss         Correct / Improve     


 Q66. How can we make sure that Hibernate doesn't update the entity changes to DB or how to keep the entity detached ?Hibernate
Ans. If it's only a read only transaction, we can wrap the get / load call in read only transaction.

If we want it to be application wide, we an set the flush mode accordingly or else we can evict the entity object to remove them from session.

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

   Like         Discuss         Correct / Improve     

Try 2 Question(s) Test


 Q67. What is the difference between session.clear and session.evict ?Hibernate
Ans. session.clear clears all the objects from session whereas session.evict is used for evicting a particular entity.

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

   Like         Discuss         Correct / Improve     hibernate session clear  hibernate evict

Try 2 Question(s) Test


 Q68. What is the use of Hibernate session.clear() ?Hibernate
Ans. It is used to clear the session and is used to dissociate disconnect all the objects from the session.

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

   Like         Discuss         Correct / Improve     hibernate session clear


 Q69. What is the use of Hibernate session.evict()?Hibernate
Ans. It is used to dissociate / disconnect a particular object from the session.

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

   Like         Discuss         Correct / Improve     hibernate session evict  hibernate evict


 Q70. What are the core interfaces in Hibernate?Hibernate
Ans. Session,SessionFactory,Configuration,Transaction,query and Criteria

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

   Like         Discuss         Correct / Improve     core interfaces of Hibernate     Asked in 1 Companies        frequent

Try 1 Question(s) Test


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: