Home » Tag Archives: JPA

Tag Archives: JPA

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 »

Dealing with Java’s LocalDateTime in JPA

java-interview-questions-answers

A few days ago I ran into a problem while dealing with a LocalDateTime attribute in JPA. In this blog post I will try to create a sample problem to explain the issue, along with the solution that I used. Consider the following entity, which models an Employee of a certain company – @Entity @Getter @Setter public class Employee { @Id ...

Read More »

Spring Data with JPA and @NamedQueries

spring-interview-questions-answers

If you use Spring Data and @NamedQuery annotations at your JPA entity you can easily use them in a more convenient way using the spring data repository. On a previous blog we created a spring data project using spring boot and docker. We will use the pretty same project and enhance our repository’s functionality. We will implement a named query ...

Read More »

Reference by Identity in JPA

java-interview-questions-answers

In a previous post, I mentioned that I opted to reference other aggregates by their primary key, and not by type. I usually use this approach (a.k.a. disconnected domain model) when working with large or complex domain models. In this post, let me try to explain further how it can be done in JPA. Note that the resulting DDL scripts ...

Read More »

OpenMap Tutorial 5 – 3-tier GIS application

1. Introduction Welcome to the 5th tutorial of the OpenMap series of Tutorials.  OpenMap is a free open source Java GIS library. Here is a list of previous tutorials: In the first tutorial we created a basic OpenMap GIS application that displays a map with one shape layer, loaded from the filesystem, inside a JFrame. That tutorial was based on ...

Read More »

Using Stored Procedures With JPA, JDBC. Meh, Just Use jOOQ

The current edition of the Java magazine has an article about Big Data Best Practices for JDBC and JPA by Josh Juneau: http://www.javamagazine.mozaicreader.com/MayJune2016 The article shows how to use a stored procedure with JDBC (notice how resources aren’t closed, unfortunately. This is commonly forgotten, even in Java Magazine articles) // Using JDBC to call upon a database stored // procedure CallableStatement ...

Read More »