Interview Questions and Answers - Order By Newest Q31. How many SessionFactory and Session objects are created ? Java EE
Ans. Single SessionFactory object and multiple session objects for opening different session. Hibernate creates new Session object per thread. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  hibernate   sessionfactory hibernate   session hibernate Q32. What is the use of hbm2ddl Configuration in Hibernate ? Hibernate
Ans. This configuration specifies if hibernate should creates the Schema / Table on its own if the respective table is not found. "update" doesn't create the table if it's not found whereas configuration set as "create" creates the schema automatically. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  hibernate   hibernate configuration   hibernate.cfg.xml   hbm2ddl Q33. What is the difference between these 2 annotations ?
@Entity
@Entity ( name="EMPLOYEES" ) Hibernate
Ans. The first annotation will try to map the Class with the Table as of same name as Class whereas the second annotation will specify the Entity name as "EMPLOYEES" and hence will try to map with Table Name "EMPLOYEES". Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  hibernate   hibernate annotations   entity annotations Q34. What is the difference between these 2 annotations ?
@Entity ( name ="EMPLOYEES")
@Entity @Table ( name=""EMPLOYEES"" )
@Entity ( name="EMP")
@Table ( name="EMPLPYEES" ) Hibernate
Ans. First Annotation will set the Entity name as EMPLOYEES and hence will try to map with the same Table name. The second annotation will make the Entity mapped to table EMPLOYEES irrespective of the Entity Name ( which is class name in this case ). Third Annotations will set the different names for Enitity and Table and will explicitly map them. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  hibernate   hibernate annotations   entity annotation   table annotation Q35. What are the different ID generating strategies using @GeneratedValue annotation ? Hibernate
Ans. Auto , Identity , Sequence and Table. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  hibernate   generatedvalue annotations   architecture Q36. How to do Eager loading in Hibernate ? Hibernate
Ans. Using lazy = false in hibernate config file or @Basic(fetch=FetchType.EAGER) at the mapping Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  hibernate   lazy loading hibernate   basic annotation hibernate   architectureAns. Instead of Saving Parent as well as Child Entities individually , Hibernate provides the option to persist / delete the related entities when the Parent is persisted. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  hibernate   cascade   hibernate relationship   hibernate associations Q38. What are the different Cascade types ? Hibernate
Ans. Detach, Merge , Persist , Remove , Refresh Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  hibernate   cascade   cascade type Q39. Which type of associated Entities are Eagerly loaded by Default ? Hibernate
Ans. OneToOne Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  hibernate   hibernate associations   hibernate relationship Q40. After which Hibernate version , related Entities are initialized lazily ? Hibernate
Ans. After Hibernate 3.0 Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  hibernate   lazy loading hibernate   lazy initialization hibernate 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 mappingAns. 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