Showing posts with label Hibernate. Show all posts
Showing posts with label Hibernate. Show all posts

January 31, 2020

Part 2: Spring Data JPA Tutorial and Interview Questions

What is JPA?
JPA (Java Persistence API) is the specification of Java that is used to persist data between Java object and RDBMS(relational database).

JPA acts as a bridge between object-oriented domain models and relational database systems. As JPA is just a specification, it doesn't perform any operation by itself. It requires an implementation. Therefore, ORM tools like Hibernate implements JPA specifications for data persistence.

Part 1: Spring Data JPA Tutorial and Interview Questions

The DAO layer usually consists of a lot of boilerplate code that can be simplified. Spring Data makes it possible to remove the DAO implementations entirely.

JPA (Java Persistent API) is the sun specification for persisting objects in the enterprise application. It is currently used as the replacement for complex entity beans.

June 29, 2017

A simple Hibernate Example with Maven, Eclipse and SQLServer..

Go to the command prompt execute the following command to generate Maven compatible Java project named as 'HibernateSLC'
mvn archetype:generate -DgroupId=com.khs.scrutiny -DartifactId=HibernateSLC -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Above command will tell Maven to create a Java project from “maven-archetype-quickstart” template. If you ignore the archetypeArtifactId argument, a list of the templates will be listed for you to choose.

To convert Maven project to support Eclipse IDE, on the command prompt navigate to 'HibernateSLC' and execute below command:
mvn eclipse:eclipse

June 05, 2017

Implementing Inheritance in Hibernate

We will create a Mobile class, which is extended by two other classes. Both SamsungMobile and BlackberryMobile have one more variable samsungModelType and bbModel respectively.

1).
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

May 07, 2017

Hibernate Tutorial & Interview Questions And Answers

What is Hibernate?
Hibernate is an ORM tool and is used in the data layer of an application. It implements JPA (Java Persistence API), which is a set of standards that have been prescribed for any persistence implementation.

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