Migration Deep Dive für 23 · Hibernate und JPA öffnen →
⌂ ÜbersichtAlle Vergleiche
Modul 23 · 100 % Codeabdeckung

Hibernate und JPA

Vollständiger Legacy-Modern-Vergleich aller Java-Klassen, Tests, Maven-POMs und relevanten Ressourcen dieses Moduls. Jede Datei ist genau einmal einer fachlich passenden Vergleichsgruppe zugeordnet.

4Legacy-Dateien
5Modern-Dateien
111 → 104Codezeilen
10/10vollständig erfasst

Fachliche Einordnung

Legacy

Native Hibernate Session

Session, Transaction und Query werden direkt über Hibernate-APIs gesteuert.

Modern

Jakarta Persistence plus Spring Data JPA

Ein JPA-Entity und Spring-Data-Repository bilden die Persistenzgrenze.

Vergleichsfokus: Alle Dateien werden nach technischer Rolle und fachlicher Verantwortung gruppiert.
Wichtige Abgrenzung: One-to-many-Änderungen werden als Gruppe dargestellt; eine künstliche 1:1-Zuordnung wird vermieden.

Vollständige Abdeckungsmatrix

Die Ähnlichkeit ist eine technische Orientierung auf normalisiertem Quelltext. Sie ersetzt keine fachliche Bewertung.

VergleichsgruppeLegacy-DateienModern-DateienLegacy-ZeilenModern-ZeilenÄhnlichkeit
Build-Konfiguration11394238%
Entity-Modell11262647%
Persistenzzugriff und Test2246258%
Bootstrap010110%

Gemeinsame Modulstruktur

Das Pair-POM bindet die Legacy- und Modern-Submodule zusammen und gehört deshalb ebenfalls zur vollständigen Codeabdeckung.

Gemeinsam · 19 Zeilenpom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>at.aydin.academy</groupId>
        <artifactId>java-modernization-deep-dive</artifactId>
        <version>1.0.0</version>
        <relativePath>../../pom.xml</relativePath>
    </parent>
    <artifactId>23hibernatejpa-pair</artifactId>
    <packaging>pom</packaging>
    <name>Hibernate und JPA: Legacy und Modern</name>
    <modules>
        <module>legacy</module>
        <module>modern</module>
    </modules>
</project>

Alle semantischen Codevergleiche

01Build-KonfigurationDirektvergleich · Legacy 39 / Modern 42 Zeilen · Ähnlichkeit 38%

Hibernate- und JPA-/Spring-Data-Abhängigkeiten werden verglichen.

Legacy-DateienModern-DateienEinordnung
pom.xmlpom.xmlDirektvergleich
Semantische Ähnlichkeit: 38%

Legacy

Legacy · 39 Zeilenlegacy/pom.xml
<?xml version="1.0" encoding="UTF-8"?>
        <project xmlns="http://maven.apache.org/POM/4.0.0"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
            <modelVersion>4.0.0</modelVersion>
            <parent>
                <groupId>at.aydin.academy</groupId>
                <artifactId>23hibernatejpa-pair</artifactId>
                <version>1.0.0</version>
                <relativePath>../pom.xml</relativePath>
            </parent>
            <artifactId>hibernate-native-legacy</artifactId>
            <packaging>jar</packaging>
            <name>Hibernate Native Legacy</name>

            <dependencies>

<dependency><groupId>org.hibernate</groupId><artifactId>hibernate-core</artifactId><version>5.6.15.Final</version></dependency>
<dependency><groupId>javax.persistence</groupId><artifactId>javax.persistence-api</artifactId><version>2.2</version></dependency>
<dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId></dependency>
<dependency><groupId>org.slf4j</groupId><artifactId>slf4j-simple</artifactId><version>2.0.17</version></dependency>

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <scope>test</scope>
</dependency>

            </dependencies>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                    </plugin>

                </plugins>
            </build>
        </project>

Modern

Modern · 42 Zeilenmodern/pom.xml
<?xml version="1.0" encoding="UTF-8"?>
        <project xmlns="http://maven.apache.org/POM/4.0.0"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
            <modelVersion>4.0.0</modelVersion>
            <parent>
                <groupId>at.aydin.academy</groupId>
                <artifactId>23hibernatejpa-pair</artifactId>
                <version>1.0.0</version>
                <relativePath>../pom.xml</relativePath>
            </parent>
            <artifactId>jpa-repository-modern</artifactId>
            <packaging>jar</packaging>
            <name>JPA Repository Modern</name>

            <dependencies>

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency>
<dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><scope>runtime</scope></dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

            </dependencies>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                    </plugin>

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

                </plugins>
            </build>
        </project>
02Entity-ModellDirektvergleich · Legacy 26 / Modern 26 Zeilen · Ähnlichkeit 47%

Mapping-Annotationen und Entity-Design werden verglichen.

Legacy-DateienModern-DateienEinordnung
LegacyBook.javaBookEntity.javaDirektvergleich
Semantische Ähnlichkeit: 47%

Legacy

package at.aydin.lab.hibernate.legacy;

import javax.persistence.*;

@Entity
@Table(name = "book")
public class LegacyBook {
    @Id
    @GeneratedValue
    private Long id;
    private String title;
    protected LegacyBook() {
    }

    public LegacyBook(String title) {
        this.title = title;
    }

    public Long getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }
}

Modern

package at.aydin.lab.hibernate.modern;

import jakarta.persistence.*;

@Entity
@Table(name = "book")
public class BookEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String title;
    protected BookEntity() {
    }

    public BookEntity(String title) {
        this.title = title;
    }

    public Long getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }
}
03Persistenzzugriff und TestDirektvergleich · Legacy 46 / Modern 25 Zeilen · Ähnlichkeit 8%

Session-Management wird Spring-Data-Repository gegenübergestellt.

Legacy-DateienModern-DateienEinordnung
HibernateBookStore.java
HibernateBookStoreTest.java
BookRepository.java
BookRepositoryTest.java
Direktvergleich
Semantische Ähnlichkeit: 8%

Legacy

package at.aydin.lab.hibernate.legacy;

import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import java.util.*;

// Design Pattern: Unit of Work
// Zweck: Hibernate Session und Transaction bilden eine explizite Arbeitseinheit.
public final class HibernateBookStore implements AutoCloseable {
    private final SessionFactory factory = new Configuration().addAnnotatedClass(LegacyBook.class).setProperty("hibernate.connection.url",
        "jdbc:h2:mem:hibernatelegacy;DB_CLOSE_DELAY=-1").setProperty("hibernate.connection.driver_class", "org.h2.Driver")
            .setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect").setProperty("hibernate.hbm2ddl.auto", "create-drop")
            .buildSessionFactory();
    public Long save(String title) {
        try (Session s = factory.openSession()) {
            Transaction tx = s.beginTransaction();
            Long id = (Long) s.save(new LegacyBook(title));
            tx.commit();
            return id;
        }
    }

    public Optional<LegacyBook> find(Long id) {
        try (Session s = factory.openSession()) {
            return Optional.ofNullable(s.get(LegacyBook.class, id));
        }
    }

    public void close() {
        factory.close();
    }
}
package at.aydin.lab.hibernate.legacy;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

class HibernateBookStoreTest {
    @Test
    void storesWithNativeSession() {
        try (var store = new HibernateBookStore()) {
            Long id = store.save("Java");
            assertEquals("Java", store.find(id).orElseThrow().getTitle());
        }
    }
}

Modern

package at.aydin.lab.hibernate.modern;

import org.springframework.data.jpa.repository.JpaRepository;

// Design Pattern: Repository
// Zweck: Datenzugriff wird als deklarativer fachlicher Vertrag ausgedrückt.
public interface BookRepository extends JpaRepository<BookEntity, Long> {
}
package at.aydin.lab.hibernate.modern;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import static org.junit.jupiter.api.Assertions.assertEquals;

@DataJpaTest
class BookRepositoryTest {
    @Autowired
    BookRepository repository;
    @Test
    void storesEntity() {
        var saved = repository.save(new BookEntity("Java"));
        assertEquals("Java", repository.findById(saved.getId()).orElseThrow().getTitle());
    }
}
04BootstrapNeu in Modern · Legacy 0 / Modern 11 Zeilen · Ähnlichkeit 0%

Die moderne Variante ergänzt eine ausführbare Boot-Anwendung.

Legacy-DateienModern-DateienEinordnung
JpaModernApplication.javaNeu in Modern
Semantische Ähnlichkeit: 0%

Legacy

Kein Legacy-Gegenstück. Diese Verantwortung wurde neu eingeführt.

Modern

package at.aydin.lab.hibernate.modern;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class JpaModernApplication {
    public static void main(String[] args) {
        SpringApplication.run(JpaModernApplication.class, args);
    }
}
⌂ Cockpit