Found 11 Articles for Hibernate

What are various Inheritance mapping strategies available in Hibernate?

Updated on 26-Aug-2022 11:47:53


There are three types of inheritance mapping strategies − Table per class hierarchy Table per concrete class Table per subclassIn this article, we will discuss table per class hierarchy. Table per class hierarchy In this, only a single table is created for inheritance mapping. Disadvantages of this approach is that a lot of null values gets stored in the table. @Inheritance(strategy=InheritanceType.SINGLE_TABLE), @DiscriminatorColumn and @DiscriminatorValue are the annotations used in this strategy. @DiscriminatorColumn is used to create an additional column which is used to identify the hierarchy classes. Consider the following example to understand this − Steps ... Read More

How to Execute Batch Insert Update in Hibernate?

Updated on 26-Aug-2022 11:40:06

2K+ Views

In this article, we will see how we can execute batch insert/update in hibernate. Whenever we execute a sql statement, we do it by making a network call to the database. Now, if we have to insert 10 entries to our database table, then we have to make 10 network calls. Instead we can optimize our network calls by using batching. Batching allows us to execute a group of SQL statements in a single network call. To understand and implement this, let us define our entities − @Entity public class Parent { @Id @GeneratedValue(strategy ... Read More

How to customize the Result of JPA Queries with Aggregation Functions?

Updated on 26-Aug-2022 11:34:40

2K+ Views

Most of the time when we use JPA queries, the result obtained is mapped to an object/particular data type. But When we use aggregate function in queries, handling the result sometimes require us to customize our JPA query. Let’s understand this with help of an example (Department, Employee) − @Entity public class Dept { @Id private Long id; private String name; @OneToMany(mappedBy = "dep") private List emp; //Getters //Setters } A department can have one or more ... Read More

How to connect hibernate with MySQL Database?

Updated on 26-Aug-2022 11:28:09

5K+ Views

In this article, we will see how we can connect to MySQL database using an ORM (object relational mapping) framework like hibernate. First of all, we need to add maven dependency for hibernate in our pom.xml file − org.hibernate hibernate-core 5.6.2.Final Now, let us define an entity class that will be mapped to a database table using hibernate. @Entity @Table( name = " Employee") public class Employee { @Id @GeneratedValue(strategy = GenerationType.AUTO) Long id; @Column(name = ... Read More

How does Hibernate Second Level Cache Works?

Updated on 26-Aug-2022 11:24:31

1K+ Views

Caching helps to reduce database network call, for executing queries. First level cache is linked with a session. It is implemented implicitly. First level cache exist only till the session object is there. Once session object is terminated/closed, there will be no cache objects. Second level cache works across multiple sessions objects. It is linked with a session factory. Second level cache objects are available to all the session in a single session factory. These cache objects are terminated when that particular session factory is closed. Implementing second level caching We need to add the following dependencies in order to ... Read More

Difference between Hibernate and JPA

Pradeep Kumar
Updated on 25-Jul-2022 10:09:14

4K+ Views

Red Hat developed the Hibernate framework, which is also referred to as Hibernate Object Relational Mapper (ORM). The first version of this object-relational mapping tool for the Java programming language was made available for download on May 23, 2007. Hibernate supports a Java Virtual Machine (JVM) that works across several platforms, and it is written in Java."JPA" stands for Java Persistence API. It is a tool to manage relational data. In its most basic form, JPA is a specification. It addresses either the object metadata or the relationship metadata. Java Persistence Query Language (JPQL) is the language that is used by ... Read More

Difference between sequence and identity in Hibernate

Himanshu shriv
Updated on 21-Jan-2020 08:04:06

6K+ Views

Hibernate or JPA support 4 different types of  primary key generator. These generators are used to generate primary key while inserting rows in the database. Below are the primary key generator   −GenerationType.AUTOGenerationType. IDENTITYGenerationType.SEQUENCE GenerationType.TABLEGenerationType. IDENTITY − In identity , database is responsible to auto generate the primary key. Insert a row without specifying a value for the ID and after inserting the row, ask the database for the last generated ID.  Oracle 11g does not support identity key generator. This feature is supported in Oracle 12c. GenerationType. SEQUENCE − In sequence, we first ask database for the next set of the sequence ... Read More

Difference between save() and persist() in Hibernate

Himanshu shriv
Updated on 21-Jan-2020 07:33:48

12K+ Views

Save() and persist() both methods are used for saving object in the database. As per docs −Save()  − Persist the given transient instance, first assigning a generated identifier. (Or using the current value of the identifier property if the assigned generator is used.) This operation cascades to associated instances if the association is mapped with cascade="save-update".As per docs −persist() −  Make a transient instance persistent. This operation cascades to associated instances if the association is mapped with cascade="persist". The semantics of this method are defined by JSR-220. Sr. No.Keysave()persist()1Basic  It stores object in databaseIt also stores object in database2Return Type It return generated id and return type is ... Read More

Difference Between get() and load() in Hibernate

Himanshu shriv
Updated on 21-Jan-2020 07:19:37

16K+ Views

In hibernate, get() and load() are two methods which is used to fetch data for the given identifier. They both belong to Hibernate session class. Get() method return null, If no row is available in the session cache or the database for the given identifier whereas load() method throws object not found exception. Sr. No.KeyGet()Load()1Basic It  is used to fetch data from the database for the given identifier  It  is also used to fetch data from the database for the given identifier 2Null Object It object not found for the given identifier then it will return null object It will throw object not found exception 3Lazy ... Read More

Difference Between First level cache and Second level cache in Hibernate

Himanshu shriv
Updated on 21-Jan-2020 07:12:08

5K+ Views

Hibernate support two type of cache one is first level cache and other is second level cache. First level cache is a session level cache and it is always associated with session level object. This type of cache is used for minimizing Db interaction by caching the state of the object. That is instead of updating after every modification done in the transaction, it updates the transaction only at the end of the transaction.Second level cache is session factory level cache and it is available across all sessions.While running the transactions, in between it loads the objects at the Session Factory level, ... Read More