diff --git a/JavaJPA.iml b/JavaJPA.iml new file mode 100644 index 00000000..9959be17 --- /dev/null +++ b/JavaJPA.iml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index 5150e50f..05def9a9 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ +[![Review Assignment Due Date](https://classroom.github.com/assets/deadline-readme-button-22041afd0340ce965d47ae6ef1cefeee28c7c493a6346c4f15d667ab976d596c.svg)](https://classroom.github.com/a/_uV8Mn8f) # 📘 Projektarbete: JPA + Hibernate med GitHub-flöde Projektet genomförs som antingen en Java CLI-applikation eller med hjälp av JavaFX om ni vill ha ett grafiskt gränssnitt. diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..717cb0a1 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,17 @@ +services: + mysql: + image: mysql:9.5.0 + container_name: jpa-mysql-db + environment: + MYSQL_PASSWORD: root + MYSQL_ROOT_PASSWORD: root + MYSQL_DATABASE: invoice-db + + ports: + - "3306:3306" + + volumes: + - mysql-data:/var/lib/mysql + +volumes: + mysql-data: diff --git a/pom.xml b/pom.xml index 909503d0..bb348498 100644 --- a/pom.xml +++ b/pom.xml @@ -51,4 +51,16 @@ 4.8.184 + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.11.0 + + 25 + + + + diff --git a/src/main/java/org/example/App.java b/src/main/java/org/example/App.java index 165e5cd5..dce37aca 100644 --- a/src/main/java/org/example/App.java +++ b/src/main/java/org/example/App.java @@ -1,7 +1,28 @@ package org.example; +import jakarta.persistence.EntityManager; +import org.example.entity.User; +import org.example.repository.UserRepository; +import org.example.service.UserService; +import org.example.util.JpaUtil; + public class App { public static void main(String[] args) { - System.out.println("Hello There!"); + try (EntityManager em = JpaUtil.getEntityManager()) { + + UserRepository userRepository = new UserRepository(em); + UserService userService = new UserService(userRepository); + + User user = new User("testUser"); + + //Operation 1 + userService.createUser(user); + + User saved = userService.getUserByUsername("testUser"); + System.out.println(saved); + + //Operation 2 + userService.deleteUserById(saved.getId()); + } } } diff --git a/src/main/java/org/example/entity/User.java b/src/main/java/org/example/entity/User.java new file mode 100644 index 00000000..dbb81008 --- /dev/null +++ b/src/main/java/org/example/entity/User.java @@ -0,0 +1,30 @@ +package org.example.entity; + +import jakarta.persistence.*; + +import java.util.UUID; + +@Entity +@Table(name = "users") +public class User { + + @Id + @GeneratedValue(strategy = GenerationType.UUID) + private UUID id; + + @Column + private String username; + + + public User(String username){ + this.username = username; + } + + public User() { + + } + + public UUID getId() { + return id; + } +} diff --git a/src/main/java/org/example/repository/BaseRepository.java b/src/main/java/org/example/repository/BaseRepository.java new file mode 100644 index 00000000..0a81dd22 --- /dev/null +++ b/src/main/java/org/example/repository/BaseRepository.java @@ -0,0 +1,77 @@ +package org.example.repository; + +import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityManagerFactory; + +import java.util.List; +import java.util.Optional; +import java.util.function.Function; + +public abstract class BaseRepository { + + private final EntityManagerFactory emf; + protected final Class entityClass; + + protected BaseRepository(EntityManagerFactory emf, Class entityClass) { + this.emf = emf; + this.entityClass = entityClass; + } + + protected R runInTransaction(Function action) { + EntityManager em = emf.createEntityManager(); + try { + em.getTransaction().begin(); + R result = action.apply(em); + em.getTransaction().commit(); + return result; + } catch (Exception e) { + if (em.getTransaction().isActive()) em.getTransaction().rollback(); + throw new RuntimeException("Transaction failed for " + entityClass.getSimpleName(), e); + } finally { + em.close(); + } + } + + protected R executeRead(Function action) { + EntityManager em = emf.createEntityManager(); + try { + return action.apply(em); + } finally { + em.close(); + } + } + + public void save(T entity) { + runInTransaction(em -> { + if (em.contains(entity)) { + em.merge(entity); + } else { + em.persist(entity); + } + return null; + }); + } + + public void delete(T entity) { + runInTransaction(em -> { + em.remove(em.contains(entity) ? entity : em.merge(entity)); + return null; + }); + } + + public Optional findById(ID id) { + return executeRead(em -> Optional.ofNullable(em.find(entityClass, id))); + } + + public List findAll() { + return executeRead(em -> + em.createQuery("SELECT e FROM " + entityClass.getSimpleName() + " e", entityClass) + .getResultList() + ); + } + + + + + +} diff --git a/src/main/java/org/example/repository/UserRepository.java b/src/main/java/org/example/repository/UserRepository.java new file mode 100644 index 00000000..3e347362 --- /dev/null +++ b/src/main/java/org/example/repository/UserRepository.java @@ -0,0 +1,35 @@ +package org.example.repository; + +import jakarta.persistence.EntityManager; +import org.example.entity.User; + +import java.util.UUID; + +public class UserRepository { + private final EntityManager em; + + public UserRepository(EntityManager em) { + this.em = em; + } + + public User getUserById(UUID id) { + return em.find(User.class, id); + } + + public User getUserByUsername(String username) { + return em.find(User.class, username); + } + + + public void save(User user) { + em.getTransaction().begin(); + em.persist(user); + em.getTransaction().commit(); + } + + public void delete(User user) { + em.getTransaction().begin(); + em.remove(user); + em.getTransaction().commit(); + } +} diff --git a/src/main/java/org/example/service/UserService.java b/src/main/java/org/example/service/UserService.java new file mode 100644 index 00000000..e11c58db --- /dev/null +++ b/src/main/java/org/example/service/UserService.java @@ -0,0 +1,33 @@ +package org.example.service; + +import org.example.entity.User; +import org.example.repository.UserRepository; + +import java.util.UUID; + +public class UserService { + + private final UserRepository userRepository; + + public UserService(UserRepository userRepository) { + this.userRepository = userRepository; + } + + public void deleteUserById(UUID id) { + User user = userRepository.getUserById(id); + userRepository.delete(user); + } + + public User getUserById(UUID id) { + return userRepository.getUserById(id); + } + + public User getUserByUsername(String username) { + return userRepository.getUserByUsername(username); + } + + public void createUser(User user) { + userRepository.save(user); + } + +} diff --git a/src/main/java/org/example/util/JpaUtil.java b/src/main/java/org/example/util/JpaUtil.java new file mode 100644 index 00000000..44a3a01c --- /dev/null +++ b/src/main/java/org/example/util/JpaUtil.java @@ -0,0 +1,23 @@ +package org.example.util; + +import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityManagerFactory; +import jakarta.persistence.Persistence; + +public class JpaUtil { + + private static final EntityManagerFactory emf; + + static { emf = Persistence.createEntityManagerFactory("jpa-hibernate-mysql"); + Runtime.getRuntime().addShutdownHook(new Thread(() -> + { + if (emf.isOpen()) { emf.close(); } })); + } + public static EntityManager getEntityManager() { + return emf.createEntityManager(); + } + + public static EntityManagerFactory getEntityManagerFactory() { + return emf; + } +} diff --git a/src/main/resources/META-INF/persistence.xml b/src/main/resources/META-INF/persistence.xml new file mode 100644 index 00000000..ae075dd9 --- /dev/null +++ b/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,21 @@ + + + + org.example.entity.User + + + + + + + + + + + + + +