Successfully added native queries

This commit is contained in:
Marc Beyer 2021-11-25 22:28:26 +01:00
parent 281d2947cd
commit 2cec62c453
3 changed files with 12 additions and 17 deletions

View File

@ -1,5 +1,6 @@
package com.vpr.server; package com.vpr.server;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
import java.util.List; import java.util.List;
@ -8,5 +9,12 @@ import java.util.List;
// CRUD refers Create, Read, Update, Delete // CRUD refers Create, Read, Update, Delete
public interface EventRepository extends CrudRepository<Event, Integer> { public interface EventRepository extends CrudRepository<Event, Integer> {
@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<Object> test();
} }

View File

@ -65,8 +65,8 @@ public class MainController {
return eventRepository.findAll(); return eventRepository.findAll();
} }
@GetMapping(path="/event-by-userid") @GetMapping(path="/test")
public @ResponseBody Iterable<java.sql.Date> getEventByUserId() { public @ResponseBody Iterable<Object> getTest() {
return userEventRepository.findAllDates(); return eventRepository.test();
} }
} }

View File

@ -10,18 +10,5 @@ import java.util.List;
// CRUD refers Create, Read, Update, Delete // CRUD refers Create, Read, Update, Delete
public interface UserEventRepository extends CrudRepository<UserEvent, Integer> { public interface UserEventRepository extends CrudRepository<UserEvent, Integer> {
@Query("SELECT " +
" ue.date " +
"FROM " +
" user_event ue")
List<java.sql.Date> findAllDates();
/*
@Query("SELECT " +
" new com.vpr.server.DateEvent(ue.date) " +
"FROM " +
" user_event ue")
List<DateEvent> findAllAsDateEvent();
*/
} }