Динамический запрос @Query с двумя таблицами, Spring boot
мне нужно написать запрос, который будет выводить данные из MYSQL в соответствии с данными, которые пользователь ввел при регистрации. У меня есть две таблицы и две @Entity к ним.
Таблица из которой нужно выбирать данные - "table"
@Entity
@Table(name = "table")
public class Table {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column
private int subject;
@Column(length = 60)
private int groupofstudent;
@Column(length = 60)
private int courseofstudent;
@Column(length = 60)
private String monday;
@Column(length = 60)
private String tuesday;
@Column(length = 60)
private String wednesday;
@Column(length = 60)
private String thursday;
@Column(length = 60)
private String friday;
public int getSubject() {
return subject;
}
public void setSubject(int subject) {
this.subject = subject;
}
public int getGroupofstudent() {
return groupofstudent;
}
public void setGroupofstudent(int groupofstudent) {
this.groupofstudent = groupofstudent;
}
public int getCourseofstudent() {
return courseofstudent;
}
public void setCourseofstudent(int courseofstudent) {
this.courseofstudent = courseofstudent;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getMonday() {
return monday;
}
public void setMonday(String monday) {
this.monday = monday;
}
public String getTuesday() {
return tuesday;
}
public void setTuesday(String tuesday) {
this.tuesday = tuesday;
}
public String getWednesday() {
return wednesday;
}
public void setWednesday(String wednesday) {
this.wednesday = wednesday;
}
public String getThursday() {
return thursday;
}
public void setThursday(String thursday) {
this.thursday = thursday;
}
public String getFriday() {
return friday;
}
public void setFriday(String friday) {
this.friday = friday;
}
}
Таблица с данными пользователя - "User"
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(length = 20, nullable = false)
private String firstname;
@Column(unique = true, length = 45, nullable = false)
private String login;
@Column(length = 45, nullable = false)
private String passwordof;
@Column(nullable = false)
private int course;
@Column(nullable = false)
private int groupofstudent;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getPassword() {
return passwordof;
}
public void setPassword(String passwordof) {
this.passwordof = passwordof;
}
public int getCourse() {
return course;
}
public void setCourse(int course) {
this.course = course;
}
public int getGroupofstudent() {
return groupofstudent;
}
public void setGroupofstudent(int groupofstudent) {
this.groupofstudent = groupofstudent;
}
}
Репозиторий для таблицы "table"
@Repository
public interface TableRepository extends JpaRepository<Table, Long> {
@Query("SELECT t FROM Table t WHERE t.courseofstudent= 1 and t.groupofstudent= 1")
List<table> findByGroupofstudentAndCourseofstudent();
}
Этот запрос работает.
@Query("SELECT t FROM table t WHERE t.courseofstudent= 1 and t.groupofstudent= 1")
Но мне нужно брать данные каждого пользователя и динамически изменять две единицы на поля groupofstudent и courseofstudent из таблицы "User". Также другие части кода:
Контроллер
@Controller
public class Controller {
@Autowired
private CustomUserDetailsService customUserDetailsService;
@Autowired
private TableService tableService;
public void setCustomUserDetailsService(CustomUserDetailsService customUserDetailsService){
this.customUserDetailsService = customUserDetailsService;
}
public void setTableService(F_fitService tableService){
this.tableService = tableService;
}
@RequestMapping("/schedule")
public String schedulePage(Model model) {
model.addAttribute("groupandcourse", customUserDetailsService.listAllUsers());
model.addAttribute("subjects", tableService.getSubjectByGroupAndCourse());
return "SchedulePage";
}
}
Сервис
@Service
public class TableService {
@Autowired
private TableRepository tableRepository;
public List<table> getSubjectByGroupAndCourse(){
return tableRepository.findByGroupofstudentAndCourseofstudent();
}
}
Как мне осуществить динамический запрос используя данные из двух таблиц?
Ответы (1 шт):
Автор решения: Максим
→ Ссылка
Если я правильно понял, то вкратце:
- Передаешь поля groupofstudent и courseofstudent в параметры контроллера, например, в PathVariable
- Прокидываешь их выше в свой сервис, далее в репо
- И твой метод будет выглядеть так:
@Query("SELECT t FROM Table t WHERE t.courseofstudent= :courseofstudent and t.groupofstudent= :groupofstudent") List<table> findByGroupofstudentAndCourseofstudent(@Param("courseofstudent") int courseofstudent, @Param("groupofstudent") int groupofstudent);