Showing posts with label ORM. Show all posts
Showing posts with label ORM. Show all posts

May 07, 2017

#Part 8: Hibernate Interview Questions

Should all the mapping files of hibernate have .hbm.xml extension to work properly?
No, having .hbm.xml extension is a convention and not a requirement for hibernate mapping file names. We can have any extension for these mapping files.

#Part 7: Hibernate Interview Questions ( N+1 problem)

What is N+1 SELECT problem in Hibernate? How to identify and resolve it?
The N+1 query problem happens when the data access framework executed N additional SQL statements to fetch the same data that could have been retrieved when executing the primary SQL query.

This N+1 query problem is not specific to JPA or Hibernate, it can be triggered using any data access technology, even with plain SQL.

May 06, 2017

#Part 6: Hibernate Interview Questions

What is the purpose of Session.beginTransaction()?
Hibernate keeps a log of every data exchange with the help of a transaction. Thereon, in case a new exchange of date is about to get initiated, the function Session.beginTransaction is executed in order to begin the transaction.

#Part 5: Hibernate Interview Questions (Composite and Derived Identifiers)

Composite Identifiers
Hibernate also allows us to define composite identifiers. FYI, a composite id is represented by a primary key class with one or more persistent attributes.
Here are the conditions which a primary class need to fulfil:
  • It should be defined using @EmbeddedId or @IdClass annotations
  • It should be public, serializable and have a public no-arg constructor
  • It should implement equals() and hashCode() methods
  • The class's attributes can be basic, composite or ManyToOne while avoiding collections and OneToOne attributes.

#Part 4: Hibernate Interview Questions (Identifiers in Hibernate-JPA)

Generated Identifiers and Hibernate GeneratedValue Strategies
Hibernate provides a couple of generation strategies to generate a primary key in the database table. We can access the strategy with the help of @GeneratedValue annotation.

May 05, 2017

#Part 3: Hibernate Interview Questions (Mapping Relations)

How to achieve mapping in Hibernate?
Association mappings are one of the key features of Hibernate. It supports the same associations as the relational database model. We can map each of the below associations as a uni or bidirectional association.
  • One-to-One associations
  • Many-to-One associations
  • Many-to-Many associations

#Part 2: Hibernate Interview Questions


What is the difference between save() , persist() ansaveOrUpdate methods of session object?
  • save() can only INSERT records but saveOrUpdate() can either INSERT or UPDATE records. 
  • session.save() saves the object and returns the id of the instance, whereas persist do not return anything after saving the instance. The return type of save() is a Serializable object, while return type of persist() method is void.
  • persist() method guarantees that it will not execute an INSERT statement if it is called outside of transaction boundaries. save() method does not guarantee the same, it returns an identifier, and if an INSERT has to be executed to get the identifier (like "identity" generator), this INSERT happens immediately, no matter if you are inside or outside of a transaction.
What is the difference between get() and load() methods of session object?
  • get() returns null if no data is present, whereas load throws ObjectNotFoundException exception in such case.
  • get() always hits the database, whereas load() method doesn't hit the database.
  • get() returns actual object, whereas load() return proxy without hitting the database unless required.

#Part 1: Hibernate Interview Questions

What is an ORM tool?
Object-relational mapping (ORM) is a technique that convert the data between relational databases and object oriented programming languages such as Java, C# etc. An ORM tool helps in simplifying data creation, manipulation, and access. It internally uses the Java API to interact with the databases.