<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Hibernate &#8211; Pauls Blog</title>
	<atom:link href="https://sterl.org/tag/hibernate/feed/" rel="self" type="application/rss+xml" />
	<link>https://sterl.org</link>
	<description></description>
	<lastBuildDate>Wed, 30 Apr 2025 17:40:32 +0000</lastBuildDate>
	<language>de</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.3</generator>
	<item>
		<title>Spring Persistent Tasks</title>
		<link>https://sterl.org/2025/04/spring-persistent-tasks/</link>
					<comments>https://sterl.org/2025/04/spring-persistent-tasks/#respond</comments>
		
		<dc:creator><![CDATA[Paul Sterl]]></dc:creator>
		<pubDate>Sun, 20 Apr 2025 07:16:43 +0000</pubDate>
				<category><![CDATA[Spring Boot]]></category>
		<category><![CDATA[dashboard]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Inbound Pattern]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JPA]]></category>
		<category><![CDATA[Outbound Pattern]]></category>
		<category><![CDATA[Quartz]]></category>
		<category><![CDATA[quartz daschboard]]></category>
		<category><![CDATA[scheduled]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[task ui]]></category>
		<category><![CDATA[task-scheduling]]></category>
		<category><![CDATA[Transaction]]></category>
		<category><![CDATA[trigger]]></category>
		<guid isPermaLink="false">https://sterl.org/?p=1116</guid>

					<description><![CDATA[A simple task management framework designed to queue and execute asynchronous tasks with support for database persistence and a user-friendly interface. It can be used to implement scheduling patterns or outbound patterns. Focus is the usage with spring boot and JPA. https://github.com/sterlp/spring-persistent-tasks Key Features]]></description>
										<content:encoded><![CDATA[
<p>A simple task management framework designed to queue and execute asynchronous tasks with support for database persistence and a user-friendly interface. It can be used to implement scheduling patterns or outbound patterns.</p>



<p>Focus is the usage with spring boot and JPA.</p>



<p><a href="https://github.com/sterlp/spring-persistent-tasks">https://github.com/sterlp/spring-persistent-tasks</a></p>



<h2 class="wp-block-heading">Key Features</h2>



<ol class="wp-block-list">
<li><strong>JPA-Powered Persistence</strong>&nbsp;&#8211; Automatically persists tasks to your database</li>



<li><strong>Spring Boot Auto-Configuration</strong>&nbsp;&#8211; Simple setup with @EnableSpringPersistentTasks</li>



<li><strong>Clustering Support</strong>&nbsp;&#8211; Built-in lock management for distributed environments</li>



<li><strong>Type-Safe Tasks</strong>&nbsp;&#8211; Generic task definitions with state objects</li>



<li><strong>Retry Mechanisms</strong>&nbsp;&#8211; Automatic retry policies for failed executions</li>



<li><strong>Transactional Integration</strong>&nbsp;&#8211; Works with Spring&#8217;s transaction management</li>



<li><strong>Queue Management</strong> &#8211; Intelligent task queuing and prioritization</li>



<li><strong>Different DB Supports</strong> 
<ul class="wp-block-list">
<li>MsSQL</li>



<li>MariaDB</li>



<li>Postgres</li>
</ul>
</li>
</ol>
]]></content:encoded>
					
					<wfw:commentRss>https://sterl.org/2025/04/spring-persistent-tasks/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Propagation REQUIRES_NEW &#038; NEVER &#124; Transaktionsmanagement mit Spring Boot, JPA &#038; Hibernate</title>
		<link>https://sterl.org/2025/01/propagation-requires_new-never-transaktionsmanagement-mit-spring-boot-jpa-hibernate/</link>
					<comments>https://sterl.org/2025/01/propagation-requires_new-never-transaktionsmanagement-mit-spring-boot-jpa-hibernate/#respond</comments>
		
		<dc:creator><![CDATA[Paul Sterl]]></dc:creator>
		<pubDate>Sun, 19 Jan 2025 20:17:00 +0000</pubDate>
				<category><![CDATA[YouTube]]></category>
		<category><![CDATA[@Transactional]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[JPA]]></category>
		<category><![CDATA[NEVER]]></category>
		<category><![CDATA[propagation]]></category>
		<category><![CDATA[REQUIRES_NEW]]></category>
		<category><![CDATA[Spring Boot]]></category>
		<guid isPermaLink="false">https://sterl.org/?p=1119</guid>

					<description><![CDATA[In diesem Video tauchen wir tiefer in die Welt des Transaktionsmanagements ein und erklären die Unterschiede zwischen den Propagation-Einstellungen REQUIRES_NEW und NEVER in Spring Boot. Erfahre, wie du mit Spring Boot, Spring Data, JPA und Hibernate effektive Transaktionen in modernen Datenbanken steuerst. Lerne, wann und warum du REQUIRES_NEW nutzen solltest, um eine neue Transaktion zu&#8230;]]></description>
										<content:encoded><![CDATA[
<p>In diesem Video tauchen wir tiefer in die Welt des Transaktionsmanagements ein und erklären die Unterschiede zwischen den Propagation-Einstellungen <code>REQUIRES_NEW</code> und <code>NEVER</code> in Spring Boot. Erfahre, wie du mit Spring Boot, Spring Data, JPA und Hibernate effektive Transaktionen in modernen Datenbanken steuerst.</p>



<p>Lerne, wann und warum du <code>REQUIRES_NEW</code> nutzen solltest, um eine neue Transaktion zu starten, und warum <code>NEVER</code> nützlich ist, um sicherzustellen, dass kein bestehender Transaktionskontext verwendet wird. Dieses Tutorial ist ideal für Entwickler, die fortgeschrittene Techniken des Transaktionsmanagements verstehen und in ihren Anwendungen umsetzen möchten.</p>



<p><a rel="noreferrer noopener" href="https://github.com/sterlp/youtube/blob/main/db-grundlagen/src/test/java/org/sterl/db_grundlagen/transaction/AuditServiceTest.java" target="_blank">Github</a></p>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="Propagation REQUIRES_NEW &amp; NEVER | Transaktionsmanagement mit Spring Boot, JPA &amp; Hibernate" width="640" height="360" src="https://www.youtube.com/embed/_5JAer04v9M?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>
]]></content:encoded>
					
					<wfw:commentRss>https://sterl.org/2025/01/propagation-requires_new-never-transaktionsmanagement-mit-spring-boot-jpa-hibernate/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Transaktionsmanagement mit Spring, JPA, Hibernate</title>
		<link>https://sterl.org/2024/08/transaktionsmanagement-mit-spring-jpa-hibernate/</link>
					<comments>https://sterl.org/2024/08/transaktionsmanagement-mit-spring-jpa-hibernate/#respond</comments>
		
		<dc:creator><![CDATA[Paul Sterl]]></dc:creator>
		<pubDate>Sat, 31 Aug 2024 15:02:17 +0000</pubDate>
				<category><![CDATA[Spring Boot]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[YouTube]]></category>
		<category><![CDATA[@Transactional]]></category>
		<category><![CDATA[Datenbank]]></category>
		<category><![CDATA[DB]]></category>
		<category><![CDATA[Grundlagen]]></category>
		<category><![CDATA[Grundlagen Datenbanken]]></category>
		<category><![CDATA[Grundlagen Transaktionen]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JPA]]></category>
		<category><![CDATA[Mandatory]]></category>
		<category><![CDATA[Read only transaction]]></category>
		<category><![CDATA[Required]]></category>
		<category><![CDATA[Spring Data]]></category>
		<category><![CDATA[Spring TransactionTemplate]]></category>
		<category><![CDATA[Transaction Timeout]]></category>
		<category><![CDATA[Transactions]]></category>
		<category><![CDATA[TransactionTemplate]]></category>
		<category><![CDATA[Transkationen]]></category>
		<guid isPermaLink="false">https://sterl.org/?p=1032</guid>

					<description><![CDATA[Lerne die Grundlagen des Transaktionsmanagements in modernen Datenbanken kennen, und erfahre, wie du mit Spring Boot, Spring Data, JPA und Hibernate effektiv arbeitest. In diesem Tutorial starten wir mit den Grundlagen der Datenbanktransaktionen in deiner Anwendung: &#8211; Transaktionen starten: Dieses Video ist perfekt für Entwickler, die ihre Fähigkeiten in Spring Framework erweitern und transaktionssichere Anwendungen&#8230;]]></description>
										<content:encoded><![CDATA[
<p>Lerne die Grundlagen des Transaktionsmanagements in modernen Datenbanken kennen, und erfahre, wie du mit Spring Boot, Spring Data, JPA und Hibernate effektiv arbeitest.</p>



<p>In diesem Tutorial starten wir mit den Grundlagen der Datenbanktransaktionen in deiner Anwendung: &#8211; Transaktionen starten: </p>



<ul class="wp-block-list">
<li><strong>Transaktion starten</strong>: Verstehe, wie Du Transaktionen in Spring Boot richtig initialisierst.</li>



<li><strong>Rollback-Strategien:</strong> Erlerne die wichtigsten Methoden, um Transaktionen bei Fehlern sicher zurückzusetzen. </li>



<li><strong>Annotations</strong>: Setze Annotations gezielt ein, um Transaktionen zu steuern. </li>



<li><strong>Programmatische Transaktionen</strong>: Implementiere Transaktionen programmatisch und gewinne maximale Kontrolle. </li>



<li><strong>Read Only</strong>: Transaktionen für GET Operationen </li>



<li><strong>Timeouts</strong>: Wie stelle ich bei einer Transaktion einen Timeout ein, was muss ich beachten?</li>
</ul>



<p>Dieses Video ist perfekt für Entwickler, die ihre Fähigkeiten in Spring Framework erweitern und transaktionssichere Anwendungen erstellen möchten.</p>



<ul class="wp-block-list">
<li><a href="https://github.com/sterlp/youtube/commit/24f1d4e1576be803ec32142ffb84f438dcd30623">Git Commit</a></li>



<li><a href="https://github.com/sterlp/youtube/tree/main/db-grundlagen">Git Projekt</a></li>
</ul>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="Transaktionsmanagement | Datenbanken | Spring Boot &amp; Data, JPA &amp; Hibernate | @Transactional" width="640" height="360" src="https://www.youtube.com/embed/9cY73uCj5wA?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>
]]></content:encoded>
					
					<wfw:commentRss>https://sterl.org/2024/08/transaktionsmanagement-mit-spring-jpa-hibernate/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Grundlagen Datenbanken &#124; als Entwickler &#124; Datenbank Locks vs Write Ahead &#124; Spring &#124; JPA &#124; Hibernate</title>
		<link>https://sterl.org/2024/01/grundlagen-datenbanken-als-entwickler-datenbank-locks-vs-write-ahead-spring-jpa-hibernate/</link>
					<comments>https://sterl.org/2024/01/grundlagen-datenbanken-als-entwickler-datenbank-locks-vs-write-ahead-spring-jpa-hibernate/#respond</comments>
		
		<dc:creator><![CDATA[Paul Sterl]]></dc:creator>
		<pubDate>Sat, 13 Jan 2024 10:03:48 +0000</pubDate>
				<category><![CDATA[YouTube]]></category>
		<category><![CDATA[bulk head]]></category>
		<category><![CDATA[concurrent update]]></category>
		<category><![CDATA[Datenbanken]]></category>
		<category><![CDATA[Entwickler]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JPA]]></category>
		<category><![CDATA[Locks]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[State Persistence]]></category>
		<category><![CDATA[Write Ahead]]></category>
		<guid isPermaLink="false">https://sterl.org/?p=990</guid>

					<description><![CDATA[Last uns zusammen ein State Persistence Anwendung auf eine Write Ahead Anwendung abändern und die Verbesserung in der Skalierbarkeit dieser betrachten.]]></description>
										<content:encoded><![CDATA[
<p>Last uns zusammen ein State Persistence Anwendung auf eine Write Ahead Anwendung abändern und die Verbesserung in der Skalierbarkeit dieser betrachten. </p>



<ol class="wp-block-list">
<li>Wie synchronisieren wir parallelle Objektzugriffe in Java und Spring Boot, um die Skalierbarkeit unseres Systems zu optimieren? </li>



<li>Praktische Lösungen: Wie verarbeitet ein Java Spring Boot System viele Updates auf einmal nach einer Downtime? </li>



<li>Strategien für Lastspitzen in der Praxis: Einsatz von DB Locks in Java und Spring Boot. </li>



<li>Praktische Tipps und Tricks: Vor- und Nachteile von Locks in der Java Spring Boot Anwendung. </li>



<li>Weitere Optimierungsmöglichkeiten in der Java Spring Boot Architektur: 
<ul class="wp-block-list">
<li>Umsetzung von Write Ahead Log in Java und Spring Boo, welche Modifikationen sind notwendig? </li>



<li>Was bring das an mehr Skalierbarkeit?</li>
</ul>
</li>
</ol>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="Grundlagen Datenbanken | als Entwickler | Datenbank Locks vs Write Ahead | Spring | JPA | Hibernate" width="640" height="360" src="https://www.youtube.com/embed/xgUrX2vk8UQ?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>
]]></content:encoded>
					
					<wfw:commentRss>https://sterl.org/2024/01/grundlagen-datenbanken-als-entwickler-datenbank-locks-vs-write-ahead-spring-jpa-hibernate/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Datenbank&#124; Transaktionen &#124; Isolationlevel &#124; Locks &#124; Hibernate &#124; JPA &#124; Spring Boot</title>
		<link>https://sterl.org/2023/10/datenbank-transaktionen-isolationlevel-locks-hibernate-jpa-spring-boot/</link>
					<comments>https://sterl.org/2023/10/datenbank-transaktionen-isolationlevel-locks-hibernate-jpa-spring-boot/#respond</comments>
		
		<dc:creator><![CDATA[Paul Sterl]]></dc:creator>
		<pubDate>Mon, 02 Oct 2023 11:40:50 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Spring Boot]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[YouTube]]></category>
		<category><![CDATA[DB Locks]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Isolationlevel]]></category>
		<category><![CDATA[JPA]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Transaktionen]]></category>
		<guid isPermaLink="false">https://sterl.org/?p=958</guid>

					<description><![CDATA[Um weiterführende Konzepte besser nachvollziehen zu können, wollen wir uns in den Grundlagen Datenbanken mit Spring Boot, Isolation Level (Isolationsgrad) und Locks beschäftigen.]]></description>
										<content:encoded><![CDATA[
<p>Um weiterführende Konzepte besser nachvollziehen zu können, wollen wir uns in den Grundlagen Datenbanken mit Spring Boot, Isolation Level (Isolationsgrad) und Locks beschäftigen.</p>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="Datenbanken Grundlagen | Transaktionen | Isolation Level | Locks | Hibernate | JPA | Spring Boot" width="640" height="360" src="https://www.youtube.com/embed/6ytlI4Sshjc?start=604&#038;feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>
]]></content:encoded>
					
					<wfw:commentRss>https://sterl.org/2023/10/datenbank-transaktionen-isolationlevel-locks-hibernate-jpa-spring-boot/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Datenbank Timeouts &#124; Locks &#124; Isonaltionlevel &#124; Transaktionen</title>
		<link>https://sterl.org/2023/10/datenbank-timeouts-locks-isonaltionlevel-transaktionen/</link>
					<comments>https://sterl.org/2023/10/datenbank-timeouts-locks-isonaltionlevel-transaktionen/#respond</comments>
		
		<dc:creator><![CDATA[Paul Sterl]]></dc:creator>
		<pubDate>Mon, 02 Oct 2023 11:15:27 +0000</pubDate>
				<category><![CDATA[Cloud]]></category>
		<category><![CDATA[Pattern & Best Practice]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[YouTube]]></category>
		<category><![CDATA[Datenbanken]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Isonaltionlevel]]></category>
		<category><![CDATA[Lock]]></category>
		<category><![CDATA[Locks]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Spring Boot]]></category>
		<category><![CDATA[Timeouts]]></category>
		<category><![CDATA[Transaktionen]]></category>
		<guid isPermaLink="false">https://sterl.org/?p=955</guid>

					<description><![CDATA[Häufig vergessen, oder nur teilweise gemacht. Timeouts tragen einen wesentlichen Bestandteil zur Stabilität und dem Ressourcenmanagement von Software bei. Insbesondere bei Microservices sollten diese immer richtig eingestellt sein. Timeouts spielen auch eine wesentliche Rolle in Bezug auf Transaktionen, Locks und Isolationslevel. Sollte man zudem den Zugriff auf die Datenbank auch Dritten gewähren, also nicht nur&#8230;]]></description>
										<content:encoded><![CDATA[
<p>Häufig vergessen, oder nur teilweise gemacht. Timeouts tragen einen wesentlichen Bestandteil zur Stabilität und dem Ressourcenmanagement von Software bei. Insbesondere bei Microservices sollten diese immer richtig eingestellt sein. Timeouts spielen auch eine wesentliche Rolle in Bezug auf Transaktionen, Locks und Isolationslevel. Sollte man zudem den Zugriff auf die Datenbank auch Dritten gewähren, also nicht nur den Services selbst, dann sind diese umso wichtiger, für alle Clients, die auf die Datenbank zugreifen.</p>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="Datenbanken Grundlagen | Timeouts | Locks | Isonaltionlevel | DB | als Architekt | Microservices" width="640" height="360" src="https://www.youtube.com/embed/cW_k_VtOVmQ?start=217&#038;feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>
]]></content:encoded>
					
					<wfw:commentRss>https://sterl.org/2023/10/datenbank-timeouts-locks-isonaltionlevel-transaktionen/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>JPA Model testing with Hibernate Statistics</title>
		<link>https://sterl.org/2021/08/jpa-model-testing-with-hibernate-statistics/</link>
					<comments>https://sterl.org/2021/08/jpa-model-testing-with-hibernate-statistics/#respond</comments>
		
		<dc:creator><![CDATA[Paul Sterl]]></dc:creator>
		<pubDate>Sun, 01 Aug 2021 11:25:00 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[JPA]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Spring Boot]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Statistics]]></category>
		<category><![CDATA[Unit Test]]></category>
		<guid isPermaLink="false">https://sterl.org/?p=925</guid>

					<description><![CDATA[Problem Often we write JPA Entity classes an have to ensure in a larger team that refactoring doesn&#8217;t create performance problems or logical errors in the entity model usual problems are: In your test]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">Problem</h2>



<p>Often we write JPA Entity classes an have to ensure in a larger team that refactoring doesn&#8217;t create performance problems or logical errors in the entity model usual problems are:</p>



<ul class="wp-block-list">
<li>Missing fetch joins in Queries
<ul class="wp-block-list">
<li>Which leads to slow fetch joins</li>
</ul>
</li>



<li>Wrong direction of Entity relations 
<ul class="wp-block-list">
<li>which leads to updates after inserts</li>



<li>or even to deletes and reinserts of data</li>
</ul>
</li>



<li>Wrong transaction boundaries
<ul class="wp-block-list">
<li>Which leads to multiple transactions for one logical business operation</li>
</ul>
</li>
</ul>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Java&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;java&quot;}">@Component
public class HibernateAsserts {
  private final Statistics statistics;

  public HibernateAsserts(EntityManager entityManager) {
    try (Session session = entityManager.unwrap(org.hibernate.Session.class)) {
      @SuppressWarnings(&quot;resource&quot;)
      SessionFactory factory = session.getSessionFactory();
      factory.getStatistics().setStatisticsEnabled(true);
      statistics = factory.getStatistics();
    }
  }
  
  public HibernateAsserts assertTrxCount(int expected) {
    long value = statistics.getTransactionCount();
    if (value != expected) {
      logSummary();
      fail(&quot;Expected &quot; + expected + &quot; TransactionCount, but found &quot; + value);
    }
    return this;
  }
  
  public HibernateAsserts assertInsertCount(int expected) {
    long value = statistics.getEntityInsertCount();
    if (value != expected) {
      logSummary();
      fail(&quot;Expected &quot; + expected + &quot; EntityInsertCount, but found &quot; + value);
    }
    return this;
  }

  public void reset() {
    statistics.clear();
  }

  public void logSummary() {
    statistics.logSummary();
  }
}</pre></div>



<h2 class="wp-block-heading">In your test</h2>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Java&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;java&quot;}">@SpringBootTest
class MyTestClassTest {

  @Autowired private SubjectUnderTest subject;
  @Autowired private HibernateAsserts hibernateAsserts;
  
  @Test
  void textMyBusinessMethod() {
    // GIVEN
    // custom data setup for the test

    // WHEN
    hibernateAsserts.reset(); // ensure the setup is not in the stats
    subject.runBusinessMethod();
    
    // THEN
    // any business asserts
    hibernateAsserts
      .assertTrxCount(1)
      .assertInsertCount(1);
  }
}</pre></div>
]]></content:encoded>
					
					<wfw:commentRss>https://sterl.org/2021/08/jpa-model-testing-with-hibernate-statistics/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>JPA default entity listener</title>
		<link>https://sterl.org/2017/08/jpa-default-entity-listener/</link>
					<comments>https://sterl.org/2017/08/jpa-default-entity-listener/#respond</comments>
		
		<dc:creator><![CDATA[Paul Sterl]]></dc:creator>
		<pubDate>Tue, 15 Aug 2017 10:52:10 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[JPA]]></category>
		<category><![CDATA[entity-listeners]]></category>
		<category><![CDATA[Hibernate]]></category>
		<guid isPermaLink="false">http://sterl.org/?p=232</guid>

					<description><![CDATA[The problem Sometimes we want to register a default Entity Listeners which are called as soon an entity is persisted, without adding the&#160;@EntityListeners annotation to our classes, as in most cases we anyway provide interfaces so we can access data on the entity itself. The problem is, that we cannot annotate our entity listener with&#8230;]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading"> The problem </h2>



<p> Sometimes we want to register a default Entity Listeners which are called as soon an entity is persisted, without adding the&nbsp;<code>@EntityListeners</code> annotation to our classes, as in most cases we anyway provide interfaces so we can access data on the entity itself. The problem is, that we cannot annotate our entity listener with any annotation, only the entity itself. </p>



<h2 class="wp-block-heading"> Solution </h2>



<p> In JPA we can provide entity mappings in the <code>orm.xml</code> which allows us to define default listeners which are executed on any entity. </p>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror cm-s-eclipse" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;xml&quot;,&quot;mime&quot;:&quot;application/xml&quot;,&quot;theme&quot;:&quot;eclipse&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;XML&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;xml&quot;}">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;entity-mappings xmlns=&quot;http://java.sun.com/xml/ns/persistence/orm&quot;
                 xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
                 xsi:schemaLocation=&quot;http://xmlns.jcp.org/xml/ns/persistence/orm http://xmlns.jcp.org/xml/ns/persistence/orm_2_1.xsd&quot;
                 version=&quot;2.1&quot;&gt;
    &lt;persistence-unit-metadata&gt;
        &lt;persistence-unit-defaults&gt;
            &lt;entity-listeners&gt;
                &lt;entity-listener class=&quot;org.sterl.example.EntityListener&quot;&gt;
                    &lt;!-- optional, annotations will work too --&gt;
                    &lt;pre-persist method-name=&quot;fooMethod&quot;/&gt; 
                &lt;/entity-listener&gt;
            &lt;/entity-listeners&gt;
        &lt;/persistence-unit-defaults&gt;
    &lt;/persistence-unit-metadata&gt;
&lt;/entity-mappings&gt;</pre></div>



<p>Furthermore, the body of the entity-listener may contain elements pre-persist to define method. </p>



<p><a rel="noreferrer noopener" href="https://github.com/sterlp/training/blob/master/hibernate/src/test/java/org/sterl/education/entitylistener/TimestampEntityListenerTest.java" target="_blank">Example On GitHub</a></p>



<h2 class="wp-block-heading"> Example</h2>



<p>Let&#8217;s assume we want to add to each entity an update time and a create time. We could, of course, implement a base entity, but as we should favor composition over inheritance &#8212; which would allow us to add even more common fields to the entity &#8212; we want to go with an embedded class. Let&#8217;s say <code>TimestampBE</code>. </p>



<h3 class="wp-block-heading">Embeddable time stamps</h3>



<p>This class adds to any entity the create date and update date. We just store UTC longs measured from 1970-01-01T00:00Z.</p>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror cm-s-eclipse" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text/x-java&quot;,&quot;theme&quot;:&quot;eclipse&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Java&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;java&quot;}">@Embeddable @Getter @Setter
public class TimestampBE implements Serializable {
    @Column(name = &quot;create_date&quot;, nullable = false, updatable = false)
    private Long createDate;
    @Column(name = &quot;update_date&quot;, nullable = false, updatable = true)
    private Long updateDate;
}</pre></div>



<h3 class="wp-block-heading">Tag interface</h3>



<p>Next, we add an interface that allows us to tag any entity that has a timestamp.</p>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror cm-s-eclipse" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text/x-java&quot;,&quot;theme&quot;:&quot;eclipse&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Java&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;java&quot;}">/**
 * Interface which marks an entity that it has the timestamp entity.
 */
public interface HasTimestamp {
    TimestampBE getTimestamp();
    void setTimestamp(TimestampBE timestamp);
}</pre></div>



<h3 class="wp-block-heading">Timestamp Entity Listener</h3>



<p>Now we have to define the behavior which sets us the create and update date in the live cycle of the entity. </p>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror cm-s-eclipse" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text/x-java&quot;,&quot;theme&quot;:&quot;eclipse&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Java&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;java&quot;}">public class TimestampEntityListener {
    @PrePersist
    void prePersist(Object entity) {
        if (entity instanceof HasTimestamp) {
            TimestampBE timestamp = getTimestamp((HasTimestamp)entity);
            final long now = Clock.systemUTC().millis();
            timestamp.setCreateDate(now);
            timestamp.setUpdateDate(now);
        }
    }

    @PreUpdate
    void preUpdate(Object entity) {
        if (entity instanceof HasTimestamp) {
            TimestampBE timestamp = getTimestamp((HasTimestamp)entity);
            final long now = Clock.systemUTC().millis();
            if (timestamp.getCreateDate() == null) timestamp.setCreateDate(now);
            timestamp.setUpdateDate(now);
        }
    }
    
    private TimestampBE getTimestamp(HasTimestamp ht) {
        TimestampBE timestamp = ht.getTimestamp();
        if (timestamp == null) {
            timestamp = new TimestampBE();
            ht.setTimestamp(timestamp);
        }
        return timestamp;
    }
}</pre></div>



<h3 class="wp-block-heading">Reister the default listener</h3>



<p>Add the <code>src/main/resources/META-INF/orm.xml</code> and register the Listener above. Adjust the package as needed: </p>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror cm-s-eclipse" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;htmlmixed&quot;,&quot;mime&quot;:&quot;text/html&quot;,&quot;theme&quot;:&quot;eclipse&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;HTML&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;html&quot;}">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;entity-mappings xmlns=&quot;http://java.sun.com/xml/ns/persistence/orm&quot;
                 xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
                 xsi:schemaLocation=&quot;http://xmlns.jcp.org/xml/ns/persistence/orm http://xmlns.jcp.org/xml/ns/persistence/orm_2_1.xsd&quot;
                 version=&quot;2.1&quot;&gt;
    &lt;persistence-unit-metadata&gt;
        &lt;persistence-unit-defaults&gt;
            &lt;entity-listeners&gt;
                &lt;entity-listener class=&quot;org.sterl.jpa.timestamp.TimestampEntityListener&quot; /&gt;
            &lt;/entity-listeners&gt;
        &lt;/persistence-unit-defaults&gt;
    &lt;/persistence-unit-metadata&gt;
&lt;/entity-mappings&gt;</pre></div>



<h3 class="wp-block-heading"> Use the new classes</h3>



<p>In the end, you may add the new class at any entity: </p>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror cm-s-eclipse" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text/x-java&quot;,&quot;theme&quot;:&quot;eclipse&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Java&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;java&quot;}">@Entity
@Data
@NoArgsConstructor @AllArgsConstructor @Builder
public class PersonBE implements HasTimestamp, Serializable {
    @Id 
    @GeneratedValue
    @Column(name = &quot;person_id&quot;)
    private Long id;
    private String name;
    @Embedded
    private TimestampBE timestamp;
}</pre></div>
]]></content:encoded>
					
					<wfw:commentRss>https://sterl.org/2017/08/jpa-default-entity-listener/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
