Home » Tag Archives: JPA

Tag Archives: JPA

Spring Data JPA Tutorial

java-interview-questions-answers

Managing data between java classes or objects and the relational database is a very cumbersome and tricky task. The DAO layer usually contains a lot of boilerplate code that should be simplified in order to reduce the number of lines of code and make the code reusable. In this tutorial, we will discuss spring data’s implementation of JPA. 1. Introduction ...

Read More »

Automatic save of managed JPA entities outside of transaction

java-interview-questions-answers

Repositories and transactions in Spring go hand in hand. All database access in Spring should be run inside a transaction, and you typically have @Transactional somewhere to enforce this. However, this is not always necessary. For example, when using Spring Data your repositories use SimpleJPARepository for CRUD functionality. The SimpleJPARepository uses @Transactional so when you perform CRUD operations, transactions are ...

Read More »

JPA Tips: Avoiding the N + 1 select problem

java-interview-questions-answers

Introduction ORM frameworks like JPA simplifies our development process by helping us to avoid lots of boilerplate code during the object <-> relational data mapping. However, they also bring some additional problems to the table, and N + 1 is one of them. In this article we will take a short look at the problem along with some ways to ...

Read More »

Spring Boot & JPA & Hibernate & Oracle

java-interview-questions-answers

In this tutorial we show how to create a Spring Boot application that communicates with an Oracle data source through Hibernate. Prerequisites: Eclipse IDE (neon release) Maven 4 Java 1.8 1- Create maven project Open eclipse, then create a new maven project and name it as SpringBootHibernate. At the end of this tutorial, we’ll get the following project structure: 2- ...

Read More »

Spring Data JPA Example with Spring Boot

java-interview-questions-answers

1. Introduction In this post, we shall demonstrate how to leverage the powerful Spring Data JPA APIs to interact with the database, in-memory H2 database for this lesson. Spring Data JPA offers a set of very powerful and highly-abstracted interfaces which are used to interact with any underlying database. Databases can be MySQL, MongoDB, Elasticsearch or any other supported database. ...

Read More »

How to map JSON collections using JPA and Hibernate

Introduction The open-source hibernate-types project allows you to map Java objects or Jackson JsonNode as JPA entity properties. Recently, thanks to our awesome contributors, we added support for type-safe collections to be persisted as JSON as well. In this article, you are going to see how to achieve this goal. Maven dependency First of all, you need to set up ...

Read More »

JPA persistence.xml SQL script definitions

java-interview-questions-answers

You can define and link to SQL scripts in a JPA persistence context definition that will be executed at runtime. There are standardized properties to define scripts how to create the schema, bulk-load data and drop the schema, respectively: <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> <persistence-unit name="prod" transaction-type="JTA"> <properties> <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/> <property name="javax.persistence.schema-generation.create-script-source" value="create-schema.sql" /> <property name="javax.persistence.schema-generation.sql-load-script-source" value="load-data.sql" /> ...

Read More »

Access the EntityManager from Spring Data JPA

spring-interview-questions-answers

Spring Data JPA allows you to rapidly develop your data access layer through the use of Repository interfaces. Occasionally you will need to access the EntityManager from Spring Data JPA. This post shows you how to access the EntityManager. EntityManager The purpose of the EntityManager is to interact with the persistence context. The persistence context will then manage entity instances ...

Read More »

Understanding the first level JPA cache

java-interview-questions-answers

I can bet that every Java developer at least heard about L1 (aka EntityManager or Session) cache. But is your level of understanding it good enough? If you’re not sure, consider going through this post. At first, we need to know what the persistence context is. Following EntityManager JavaDoc we know, that: “A persistence context is a set of entity instances in which for ...

Read More »