From 2cec62c453e919a00a5be712eb4c1e31f77c65a2 Mon Sep 17 00:00:00 2001 From: Marc Beyer Date: Thu, 25 Nov 2021 22:28:26 +0100 Subject: [PATCH] Successfully added native queries --- .../main/java/com/vpr/server/EventRepository.java | 10 +++++++++- .../main/java/com/vpr/server/MainController.java | 6 +++--- .../java/com/vpr/server/UserEventRepository.java | 13 ------------- 3 files changed, 12 insertions(+), 17 deletions(-) diff --git a/server/src/main/java/com/vpr/server/EventRepository.java b/server/src/main/java/com/vpr/server/EventRepository.java index 5d45661..697b224 100644 --- a/server/src/main/java/com/vpr/server/EventRepository.java +++ b/server/src/main/java/com/vpr/server/EventRepository.java @@ -1,5 +1,6 @@ package com.vpr.server; +import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; import java.util.List; @@ -8,5 +9,12 @@ import java.util.List; // CRUD refers Create, Read, Update, Delete public interface EventRepository extends CrudRepository { - + @Query(value = "SELECT e.id AS eid, e.name AS ename, u.id AS uid, u.forename, u.name AS uname " + + "FROM event e " + + "INNER JOIN user_event ue " + + "ON e.id = ue.event_id " + + "INNER JOIN user u " + + "ON ue.user_id = u.id", + nativeQuery = true) + List test(); } \ No newline at end of file diff --git a/server/src/main/java/com/vpr/server/MainController.java b/server/src/main/java/com/vpr/server/MainController.java index 499dbba..06f85cc 100644 --- a/server/src/main/java/com/vpr/server/MainController.java +++ b/server/src/main/java/com/vpr/server/MainController.java @@ -65,8 +65,8 @@ public class MainController { return eventRepository.findAll(); } - @GetMapping(path="/event-by-userid") - public @ResponseBody Iterable getEventByUserId() { - return userEventRepository.findAllDates(); + @GetMapping(path="/test") + public @ResponseBody Iterable getTest() { + return eventRepository.test(); } } \ No newline at end of file diff --git a/server/src/main/java/com/vpr/server/UserEventRepository.java b/server/src/main/java/com/vpr/server/UserEventRepository.java index 129ed43..d5721aa 100644 --- a/server/src/main/java/com/vpr/server/UserEventRepository.java +++ b/server/src/main/java/com/vpr/server/UserEventRepository.java @@ -10,18 +10,5 @@ import java.util.List; // CRUD refers Create, Read, Update, Delete public interface UserEventRepository extends CrudRepository { - @Query("SELECT " + - " ue.date " + - "FROM " + - " user_event ue") - List findAllDates(); - /* - @Query("SELECT " + - " new com.vpr.server.DateEvent(ue.date) " + - "FROM " + - " user_event ue") - List findAllAsDateEvent(); - - */ }