Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
| de:modul:ffit:3-jahr:java:learningunits:lu03:c [2025/08/29 17:58] – angelegt apeter | de:modul:ffit:3-jahr:java:learningunits:lu03:c [2025/09/01 23:22] (aktuell) – apeter | ||
|---|---|---|---|
| Zeile 5: | Zeile 5: | ||
| In der vorhergehenden Lektion haben Sie die Klasse '' | In der vorhergehenden Lektion haben Sie die Klasse '' | ||
| - | ^SQL '' | + | ^SQL '' |
| | {{: | | {{: | ||
| Zeile 15: | Zeile 15: | ||
| Wir wählen den Java-first-Ansatz mit JPA/ | Wir wählen den Java-first-Ansatz mit JPA/ | ||
| - | <code bash> | + | Die '' |
| - | ... | + | |
| + | Ebenfalls existiert eine Konfiguration '' | ||
| + | |||
| + | ^ Vorher ^ Nachher ^ | ||
| + | | < | ||
| + | <code properties> | ||
| + | DB_URL=jdbc: | ||
| + | DB_USER=localuser | ||
| + | DB_PASSWORD=secret | ||
| </ | </ | ||
| + | </ | ||
| + | <code properties> | ||
| + | jakarta.persistence.jdbc.url=jdbc: | ||
| + | jakarta.persistence.jdbc.user=localuser | ||
| + | jakarta.persistence.jdbc.password=secret | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | |||
| + | Im Java-Code sind ausserdem folgende Anpassungen nötig: | ||
| + | * Die Eigenschaften der Tabelle werden als Annotationen ('' | ||
| + | * Ein leerer Standard-Constructor ist nun nötig, damit Hibernate im Hintergrund eine neue Instanz erstellen kann. | ||
| + | * '' | ||
| + | |||
| + | Dadurch sieht die Klasse '' | ||
| + | |||
| + | <code java> | ||
| + | package ch.bzz.model; | ||
| + | |||
| + | import jakarta.persistence.*; | ||
| + | |||
| + | @Entity | ||
| + | @Table(name = " | ||
| + | public class Book { | ||
| + | |||
| + | @Id | ||
| + | @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| + | private Integer id; | ||
| + | |||
| + | @Column(name = " | ||
| + | private String isbn; | ||
| + | |||
| + | @Column(name = " | ||
| + | private String title; | ||
| + | |||
| + | @Column(name = " | ||
| + | private String author; | ||
| + | |||
| + | @Column(name = " | ||
| + | private Integer publicationYear; | ||
| + | |||
| + | public Book(){} | ||
| + | |||
| + | public Book(Integer id, String isbn, String title, String author, Integer publicationYear) { | ||
| + | this.id = id; | ||
| + | this.isbn = isbn; | ||
| + | this.title = title; | ||
| + | this.author = author; | ||
| + | this.publicationYear = publicationYear; | ||
| + | } | ||
| + | | ||
| + | // toString, equals(), hashCode() if needed | ||
| + | ... | ||
| + | | ||
| + | // Getter & Setter | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | Um Objekte von der Datenbank zu lesen kann mittels '' | ||
| + | Bei den ''< | ||
| + | |||
| + | <code java> | ||
| + | private final EntityManagerFactory emf = Persistence.createEntityManagerFactory(" | ||
| + | |||
| + | public List< | ||
| + | try (EntityManager em = emf.createEntityManager()) { | ||
| + | var query = em.createQuery(" | ||
| + | if (limit > 0) { | ||
| + | query.setMaxResults(limit); | ||
| + | } | ||
| + | return query.getResultList(); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | |||
| + | public void saveAll(List< | ||
| + | try (EntityManager em = emf.createEntityManager()) { | ||
| + | try { | ||
| + | em.getTransaction().begin(); | ||
| + | books.forEach(em:: | ||
| + | em.getTransaction().commit(); | ||
| + | } catch (RuntimeException e) { | ||
| + | if (em.getTransaction().isActive()) { | ||
| + | em.getTransaction().rollback(); | ||
| + | } | ||
| + | log.error(" | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Schreiben Sie Ihren Code so um, dass die '' | ||
| + | |||
| + | Der Commit '' | ||
| + | |||