<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Backendhance</title><link>https://backendhance.com/blog/category/jpa/</link><description>Recent content Backendhance</description><generator>Hugo -- gohugo.io</generator><language>de</language><lastBuildDate>Wed, 02 Aug 2023 07:30:19 +0200</lastBuildDate><atom:link href="https://backendhance.com/blog/category/jpa/index.xml" rel="self" type="application/rss+xml"/><item><title>Vermeide diese 5 häufigen Performance Pitfalls in Spring Data JPA: Praktische Lösungen und Tipps</title><link>https://backendhance.com/blog/2023/5-common-jpa-pitfalls/</link><pubDate>Wed, 02 Aug 2023 07:30:19 +0200</pubDate><author/><guid>https://backendhance.com/blog/2023/5-common-jpa-pitfalls/</guid><description>&lt;p>In der Welt der Backendentwicklung auf der JVM ist Spring Data JPA ein unverzichtbares Werkzeug. Es bietet eine bequeme und leistungsstarke Möglichkeit, Daten zu verwalten und zu manipulieren. Aber wie bei jedem mächtigen Werkzeug gibt es auch Fallstricke, die die Performance beeinträchtigen können. In diesem Artikel werden wir fünf häufige Performance Pitfalls in Spring Data JPA untersuchen und Lösungen aufzeigen, um diese zu vermeiden.&lt;/p>
&lt;h2 id="1-indexdefinition-mit-spring-data-jpa">
&lt;a href="#1-indexdefinition-mit-spring-data-jpa" class="anchor">1. Indexdefinition mit Spring Data JPA&lt;/a>
&lt;/h2>
&lt;p>Ein häufig übersehener Aspekt bei der Arbeit mit Spring Data JPA ist die korrekte Definition von Indizes. Ein gut definierter Index kann den Datenzugriff erheblich beschleunigen, während ein fehlender oder falsch definierter Index zu Performance-Problemen führen kann. In &lt;a href="https://backendhance.com/blog/2023/how-to-setup-index-in-spring/">diesem Artikel&lt;/a>
erfährst du, wie du einen Index mit Spring Data JPA definierst und welche Überlegungen du dabei anstellen solltest.&lt;/p></description></item><item><title>Effiziente Datenabfragen mit Spring Data JPA Projektionen</title><link>https://backendhance.com/blog/2023/jpa-projections/</link><pubDate>Wed, 26 Jul 2023 07:18:03 +0200</pubDate><author/><guid>https://backendhance.com/blog/2023/jpa-projections/</guid><description>&lt;p>In der Welt der &lt;strong>Backendentwicklung auf der JVM&lt;/strong> ist die effiziente Datenverarbeitung ein entscheidendes Anliegen. Eine der Hauptstärken von &lt;strong>Spring Data JPA&lt;/strong> liegt in seiner Fähigkeit, die Interaktion mit der Persistenzschicht zu vereinfachen und zu optimieren. Eine besonders leistungsstarke Funktion, die Spring Data JPA zur Verfügung stellt, sind die Projektionen. Sie bieten eine ausgezeichnete Möglichkeit, Datenanfragen effizienter zu gestalten und die Systemperformance zu verbessern.&lt;/p>
&lt;h2 id="was-sind-spring-data-jpa-projektionen">
&lt;a href="#was-sind-spring-data-jpa-projektionen" class="anchor">Was sind Spring Data JPA Projektionen?&lt;/a>
&lt;/h2>
&lt;p>Spring Data JPA-Projektionen ermöglichen es, maßgeschneiderte &lt;strong>Partial Views&lt;/strong> deiner Entity-Klassen zu erstellen. Das bedeutet, dass du statt einer vollständigen Entität nur ausgewählte Felder abrufen kannst, was die Größe der zurückgegebenen Daten erheblich reduziert und damit die Gesamtperformance verbessert.&lt;/p></description></item><item><title>Der Hi/Lo-Algorithmus in Hibernate: Optimierung der Datenbank-Identifikatorgenerierung</title><link>https://backendhance.com/blog/2023/hibernate-hi-lo-algorithm/</link><pubDate>Wed, 19 Jul 2023 06:00:43 +0200</pubDate><author/><guid>https://backendhance.com/blog/2023/hibernate-hi-lo-algorithm/</guid><description>&lt;p>In der Welt der Persistenz auf der JVM hat Hibernate einen festen Platz. Als eines der populärsten Frameworks für die Persistierung von Daten bietet es eine Vielzahl an Möglichkeiten, um Entwicklern das Leben einfacher zu machen. Eine dieser Möglichkeiten ist der Hi/Lo-Algorithmus, eine Datenbank-Identifikatorgenerierungsstrategie, die es ermöglicht, die Anzahl der Datenbankaufrufe zu reduzieren, wenn neue Entitäten persistiert werden.&lt;/p>
&lt;h2 id="die-herausforderung-der-identifikatorgenerierung">
&lt;a href="#die-herausforderung-der-identifikatorgenerierung" class="anchor">Die Herausforderung der Identifikatorgenerierung&lt;/a>
&lt;/h2>
&lt;p>Bevor wir in den Hi/Lo-Algorithmus eintauchen, wollen wir uns kurz die Herausforderung der Identifikatorgenerierung in einer Datenbank anschauen. Jede Entität in einer Datenbank benötigt einen eindeutigen Identifier (oft als ID bezeichnet), um sie von anderen Entitäten zu unterscheiden. Bei jedem Einfügen einer neuen Entität in die Datenbank wird bei numerischen IDs normalerweise die Sequenz auf der Datenbank abgefragt, um die nächste ID zu erhalten. Dies kann jedoch zu einer hohen Anzahl von Datenbankaufrufen führen, insbesondere wenn viele neue Entitäten persistiert werden.&lt;/p></description></item><item><title>Vermeidung mehrfacher Datenabrufe durch den First-Level-Cache in Spring Data JPA</title><link>https://backendhance.com/blog/2023/avoid-repeated-fetched-spring-data/</link><pubDate>Wed, 12 Jul 2023 07:08:24 +0200</pubDate><author/><guid>https://backendhance.com/blog/2023/avoid-repeated-fetched-spring-data/</guid><description>&lt;p>Wenn du dich mit der Backendentwicklung auf der JVM befasst, wirst du sicherlich auf die &lt;em>Java Persistence API&lt;/em> (JPA) stoßen. Ein bekanntes Implementierungsframework dafür ist &lt;em>Hibernate&lt;/em>. In diesem Artikel zeigen wir dir, wie du in &lt;em>Spring Data JPA&lt;/em> mit &lt;em>caching&lt;/em> die Performance deiner Anwendung optimieren kannst, indem du verhinderst, dass gleiche Ressourcen mehrfach abgerufen werden.&lt;/p>
&lt;h2 id="was-ist-der-first-level-cache">
&lt;a href="#was-ist-der-first-level-cache" class="anchor">Was ist der First-Level-Cache?&lt;/a>
&lt;/h2>
&lt;p>Spring Data JPA verwendet Hibernate als Standard-ORM (Object-Relational Mapping), das einen eingebauten First-Level-Cache anbietet. Jede Session hat ihren eigenen Cache, den wir als First-Level-Cache bezeichnen. Jedes Mal, wenn du ein &lt;em>Objekt&lt;/em> (oder genauer gesagt eine &lt;em>Entity-Instanz&lt;/em>) aus der &lt;em>Datenbank&lt;/em> abrufst, wird es zuerst im First-Level-Cache gespeichert. Bei wiederholtem Abrufen desselben Objekts wird es direkt aus dem Cache und nicht aus der Datenbank geholt. Das kann zur Datenwiederholung führen und die &lt;em>Performance&lt;/em> beeinträchtigen.&lt;/p></description></item><item><title>Hibernate ORM: Eager oder Lazy Loading? Wann du welches nutzen solltest</title><link>https://backendhance.com/blog/2023/jpa-fetching-strategies/</link><pubDate>Wed, 05 Jul 2023 06:10:09 +0200</pubDate><author/><guid>https://backendhance.com/blog/2023/jpa-fetching-strategies/</guid><description>&lt;p>Hibernate ist ein etabliertes &lt;strong>Object-Relational Mapping (ORM)&lt;/strong> Framework, das in der Java-Entwicklung weit verbreitet ist. Dieses Framework hilft Entwicklern, den Code für Datenbankvorgänge wie das Abrufen von Daten (auch bekannt als Fetching) zu optimieren. Die Wahl der richtigen Fetching-Strategie - Eager Loading oder Lazy Loading - ist entscheidend für die Leistungsfähigkeit deiner Anwendung. In diesem Artikel betrachten wir diese beiden Strategien und diskutieren, wann und wie du sie in deinem Spring-Data-Projekt einsetzen solltest.&lt;/p></description></item><item><title>Wie definiere ich einen Index mit Spring Data JPA</title><link>https://backendhance.com/blog/2023/how-to-setup-index-in-spring/</link><pubDate>Wed, 28 Jun 2023 07:20:12 +0200</pubDate><author/><guid>https://backendhance.com/blog/2023/how-to-setup-index-in-spring/</guid><description>&lt;p>In diesem Artikel beschäftigen wir uns mit der Definition von Indizes in &lt;strong>Spring Data JPA&lt;/strong> und dem &lt;strong>Hibernate Framework&lt;/strong>. Unser Fokus liegt dabei auf der Definition eines Indexes für eine Nicht-Primarykey und der Nutzung der &lt;code>@Index&lt;/code> Annotation.&lt;/p>
&lt;p>Die richtige Verwendung von Indizes spielt eine entscheidende Rolle für die Leistungsoptimierung deiner Datenbank, da sie schnellere Datenabfragen ermöglicht und insgesamt die Datenbankperformance verbessert.&lt;/p>
&lt;h2 id="definition-eines-indexes-mit-der-index-annotation">
&lt;a href="#definition-eines-indexes-mit-der-index-annotation" class="anchor">Definition eines Indexes mit der @Index Annotation&lt;/a>
&lt;/h2>
&lt;p>Mit JPA und Hibernate kannst du Indizes auf Entitäten und Nicht-Entitätstabellen wie &lt;code>@SecondaryTable&lt;/code>, &lt;code>@CollectionTable&lt;/code> und &lt;code>@JoinTable&lt;/code> über die &lt;code>@Index&lt;/code> Annotation definieren. Diese dient der Datenbankperformance-Optimierung und der Anpassung.&lt;/p></description></item><item><title>Accessing Non-Final Property Name in Constructor With JPA</title><link>https://backendhance.com/blog/2023/non-final-property-constructor-jpa/</link><pubDate>Sat, 21 Jan 2023 00:00:00 +0000</pubDate><author/><guid>https://backendhance.com/blog/2023/non-final-property-constructor-jpa/</guid><description>&lt;p>The implications of JPA always manage to surprise me. Yesterday a colleague of mine made me aware of a warning in IntelliJ. The conversation went like that: &amp;ldquo;Marcus, in your blog you explained that we should &lt;a href="https://backendhance.com/en/blog/2022/bean-validation-antipattern/">check constraints in the constructor instead of bean validation&lt;/a>
. Me: &amp;ldquo;yeah&amp;rdquo;. &amp;ldquo;I wanted to make it right, but when I do it in this entity IntelliJ warns me with &lt;code>Accessing non-final property name in constructor&lt;/code>&amp;rdquo;. So I dug into it. Fearing my conclusion would change the recommendation that I gave in &lt;a href="https://www.youtube.com/watch?v=B4UN5lASzjg"target="_blank" rel="noopener noreferrer">(k)lean JPA&lt;i class="bx bx-link-external">&lt;/i>&lt;/a>
two years ago.&lt;/p></description></item><item><title>Prefer UUID for your Primary Key</title><link>https://backendhance.com/blog/2021/uuid-primary-key/</link><pubDate>Mon, 02 Aug 2021 00:00:00 +0000</pubDate><author/><guid>https://backendhance.com/blog/2021/uuid-primary-key/</guid><description>&lt;p>In my &lt;a href="https://backendhance.com/en/blog/2021/numerical-id-consequence/">last post&lt;/a>
I discussed the downsides of using numerical types
for the primary key of an entity. We should avoid these issues all together by using &lt;code>UUID&lt;/code> instead and in this post I
will discuss the up- and downsides of this approach.&lt;/p>
&lt;p>Using a UUID as the primary ID is simple:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-kotlin" data-lang="kotlin">&lt;span class="line">&lt;span class="cl">&lt;span class="nd">@Entity&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">class&lt;/span> &lt;span class="nc">Flat&lt;/span>&lt;span class="p">(&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nd">@Id&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">val&lt;/span> &lt;span class="py">id&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="n">UUID&lt;/span> &lt;span class="p">=&lt;/span> &lt;span class="nc">UUID&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">randomUUID&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="generate-the-id-on-the-application">
&lt;a href="#generate-the-id-on-the-application" class="anchor">Generate the ID on the application&lt;/a>
&lt;/h2>
&lt;p>Similar to numerical ids we can generate it on the database as well. But for &lt;code>UUID&lt;/code> this is not necessary. The reason we
did this in the first place is that we needed to make sure that an ID is not used twice. A collision of &lt;code>UUID&lt;/code> is very
unlikely to occur.&lt;/p></description></item><item><title>The Inevitable Consequence of a Numerical Id</title><link>https://backendhance.com/blog/2021/numerical-id-consequence/</link><pubDate>Thu, 15 Jul 2021 00:00:00 +0000</pubDate><author/><guid>https://backendhance.com/blog/2021/numerical-id-consequence/</guid><description>&lt;p>In many (JPA) applications numerical ids are chosen for the &lt;a href="https://en.wikipedia.org/wiki/Surrogate_key"target="_blank" rel="noopener noreferrer">surrogate key&lt;i class="bx bx-link-external">&lt;/i>&lt;/a>
of the entities. But how do we make sure that they are not used twice? In a scenario where our application needs to scale horizontally we need a solution for that. Most developers come to the conclusion that the database should take care of that. But this is a fragile solution and in this article I want to discuss it.&lt;/p></description></item><item><title>Should I Pass JPA Entities as Method Parameters?</title><link>https://backendhance.com/blog/2021/passing-jpa-entities/</link><pubDate>Sat, 20 Mar 2021 00:00:00 +0000</pubDate><author/><guid>https://backendhance.com/blog/2021/passing-jpa-entities/</guid><description>&lt;p>JPA entities have a couple of pitfalls and consequences when handling them in your application. Especially understanding how JPA manages and synchronizes the state of an entity is essential to avoid unexpected behavior. This can be unintuitive when passing JPA entities as method parameters in Spring Data in- and outside a transaction. In this post I will explain a best practice when working with JPA entities and when it&amp;rsquo;s okay to pass it through method parameters and when you should avoid it.&lt;/p></description></item><item><title>JPA Doesn't Call Persist But Merge Operation On New Entities</title><link>https://backendhance.com/blog/2021/jpa-version-null/</link><pubDate>Thu, 07 Jan 2021 00:00:00 +0000</pubDate><author/><guid>https://backendhance.com/blog/2021/jpa-version-null/</guid><description>&lt;p>When working with JPA I prefer generating the primary key on the application and not in the database (check out &lt;a href="https://web.archive.org/web/20170814122921/http://www.onjava.com/pub/a/onjava/2006/09/13/dont-let-hibernate-steal-your-identity.html"target="_blank" rel="noopener noreferrer">this post from James Brundege&lt;i class="bx bx-link-external">&lt;/i>&lt;/a>
). Additionally, I also prefer optimistic locking and in order to use it you need to specify a &lt;code>@Version&lt;/code> field in your Entity. But you need to be careful how to initialize these two fields. In this post I&amp;rsquo;ll talk about an error when you assign a default value for both fields in JPA which leads the &lt;code>EntityManager&lt;/code> to never call &lt;code>persist&lt;/code> but the &lt;code>merge&lt;/code> operation instead when creating a new entity.&lt;/p></description></item><item><title>Additional auditing with Spring Data JPA and Hibernate</title><link>https://backendhance.com/blog/2020/additional-auditing/</link><pubDate>Tue, 28 Jan 2020 00:00:00 +0000</pubDate><author>Sascha Boeing</author><guid>https://backendhance.com/blog/2020/additional-auditing/</guid><description>&lt;p>Spring Data provides an easy way of keeping track who creates and modifies a persistent entity as well as when the action happened by annotating properties with &lt;code>@CreatedBy&lt;/code>, &lt;code>@CreatedDate&lt;/code>, &lt;code>@LastModifiedBy&lt;/code> and &lt;code>@LastModifiedDate&lt;/code>. The properties are automatically provided by an implementation of the &lt;code>AuditAware&lt;/code> and &lt;code>DateTimeProvider&lt;/code> interface.&lt;/p>
&lt;p>Beside this common auditing information in my current projects some entities require storing auditing-information about crucial state-changes like soft-deletion.&lt;/p>
&lt;p>Previously we had to fill the properties ourself:&lt;/p></description></item></channel></rss>