diff --git a/docs/praktikum/aufgaben/Componentware - Praktikum - Transaktionen und Spring - Aufgaben.pdf b/docs/praktikum/aufgaben/Componentware - Praktikum - Transaktionen und Spring - Aufgaben.pdf new file mode 100644 index 0000000..eae30e3 Binary files /dev/null and b/docs/praktikum/aufgaben/Componentware - Praktikum - Transaktionen und Spring - Aufgaben.pdf differ diff --git a/spring/pom.xml b/spring/pom.xml index f40d60d..0ec20d0 100644 --- a/spring/pom.xml +++ b/spring/pom.xml @@ -1,34 +1,40 @@ - + 4.0.0 - org.springframework.boot spring-boot-starter-parent 3.3.5 - de.componentware spring-demo 1.0-SNAPSHOT spring-demo Spring Boot Demo – Componentware Übung 6 - 21 - org.springframework.boot spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-data-jpa + + + com.h2database + h2 + runtime + + + org.projectlombok + lombok + 1.18.46 + - diff --git a/spring/src/main/java/org/example/demo/praktikum6/Auto.java b/spring/src/main/java/org/example/demo/praktikum6/Auto.java new file mode 100644 index 0000000..fb691fe --- /dev/null +++ b/spring/src/main/java/org/example/demo/praktikum6/Auto.java @@ -0,0 +1,20 @@ +package org.example.demo.praktikum6; + +import jakarta.persistence.*; +import java.io.Serializable; + +@Entity +public class Auto implements Serializable { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(nullable = false) + public String hersteller; + + @Column(nullable = false) + public String typ; + + @Column(nullable = false) + public String kennzeichen; +} diff --git a/spring/src/main/java/org/example/demo/praktikum6/AutoDAO.java b/spring/src/main/java/org/example/demo/praktikum6/AutoDAO.java new file mode 100644 index 0000000..9ab71db --- /dev/null +++ b/spring/src/main/java/org/example/demo/praktikum6/AutoDAO.java @@ -0,0 +1,32 @@ +package org.example.demo.praktikum6; + +import jakarta.persistence.EntityManager; +import jakarta.persistence.PersistenceContext; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; + +@Repository +public class AutoDAO { + + @PersistenceContext + private EntityManager em; + + @Autowired + private LogEntryDAO logEntryDAO; + + @Transactional + public void createAuto(String hersteller, String typ, String kennzeichen) { + try { + Auto auto = new Auto(); + auto.hersteller = hersteller; + auto.typ = typ; + auto.kennzeichen = kennzeichen; + em.persist(auto); + logEntryDAO.createLogEntry("Auto angelegt: " + hersteller + " " + typ + " (" + kennzeichen + ")"); + } catch (Exception e) { + logEntryDAO.createLogEntry("Fehler beim Anlegen des Autos: " + e.getMessage()); + throw e; + } + } +} diff --git a/spring/src/main/java/org/example/demo/praktikum6/LogEntry.java b/spring/src/main/java/org/example/demo/praktikum6/LogEntry.java new file mode 100644 index 0000000..dc6ab25 --- /dev/null +++ b/spring/src/main/java/org/example/demo/praktikum6/LogEntry.java @@ -0,0 +1,16 @@ +package org.example.demo.praktikum6; + +import jakarta.persistence.*; +import java.time.LocalDateTime; +import java.util.Date; + +@Entity +public class LogEntry { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + public Date date; + public LocalDateTime time; + public String message; +} diff --git a/spring/src/main/java/org/example/demo/praktikum6/LogEntryDAO.java b/spring/src/main/java/org/example/demo/praktikum6/LogEntryDAO.java new file mode 100644 index 0000000..0ca4002 --- /dev/null +++ b/spring/src/main/java/org/example/demo/praktikum6/LogEntryDAO.java @@ -0,0 +1,30 @@ +package org.example.demo.praktikum6; + +import jakarta.persistence.EntityManager; +import jakarta.persistence.PersistenceContext; +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; + +import java.time.LocalDateTime; +import java.util.Date; +import java.util.List; + +@Repository +public class LogEntryDAO { + + @PersistenceContext + private EntityManager em; + + @Transactional + public void createLogEntry(String message) { + LogEntry entry = new LogEntry(); + entry.date = new Date(); + entry.time = LocalDateTime.now(); + entry.message = message; + em.persist(entry); + } + + public List getAllLogEntries() { + return em.createQuery("SELECT l FROM LogEntry l", LogEntry.class).getResultList(); + } +} diff --git a/spring/src/main/java/org/example/demo/praktikum6/TestClient.java b/spring/src/main/java/org/example/demo/praktikum6/TestClient.java new file mode 100644 index 0000000..eae7c05 --- /dev/null +++ b/spring/src/main/java/org/example/demo/praktikum6/TestClient.java @@ -0,0 +1,28 @@ +package org.example.demo.praktikum6; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Component +public class TestClient implements CommandLineRunner { + + @Autowired + private AutoDAO autoDAO; + + @Autowired + private LogEntryDAO logEntryDAO; + + @Override + public void run(String... args) { + autoDAO.createAuto("BMW", "E32", "NE-IN 0000"); + + List logs = logEntryDAO.getAllLogEntries(); + System.out.println("=== Log-Einträge (" + logs.size() + ") ==="); + for (LogEntry entry : logs) { + System.out.println("[" + entry.time + "] " + entry.message); + } + } +} diff --git a/spring/src/main/java/org/example/demo/uebung6/SpringDemoApplication.java b/spring/src/main/java/org/example/demo/uebung6/SpringDemoApplication.java index 7bd0b3a..e59c1f3 100644 --- a/spring/src/main/java/org/example/demo/uebung6/SpringDemoApplication.java +++ b/spring/src/main/java/org/example/demo/uebung6/SpringDemoApplication.java @@ -2,8 +2,10 @@ package org.example.demo.uebung6; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.domain.EntityScan; -@SpringBootApplication +@SpringBootApplication(scanBasePackages = "org.example.demo") +@EntityScan(basePackages = "org.example.demo") public class SpringDemoApplication { public static void main(String[] args) { diff --git a/spring/src/main/resources/application.properties b/spring/src/main/resources/application.properties index 85127d7..8e3af21 100644 --- a/spring/src/main/resources/application.properties +++ b/spring/src/main/resources/application.properties @@ -1 +1,7 @@ spring.application.name=spring-demo + +spring.datasource.url=jdbc:h2:mem:testdb +spring.datasource.driver-class-name=org.h2.Driver +spring.jpa.hibernate.ddl-auto=create-drop +spring.jpa.show-sql=true +spring.h2.console.enabled=true