Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. @Entity
  2. public class Meal {
  3.  
  4. @Id
  5. @GeneratedValue
  6. private Long id;
  7.  
  8. @JoinColumn(name = "student_id")
  9. @ManyToOne(fetch = FetchType.LAZY)
  10. private Student mealCook;
  11.  
  12. private String mealName;
  13.  
  14. private int mealPrice;
  15.  
  16. @Entity
  17. public class Student {
  18.  
  19. @Id
  20. @GeneratedValue
  21. private Long id;
  22.  
  23. private String studentName;
  24.  
  25. @OneToMany(
  26. mappedBy = "mealCook",
  27. cascade = CascadeType.ALL,
  28. orphanRemoval = true
  29. )
  30. private List<Meal> meals = new ArrayList<>();
  31.  
  32. @Controller
  33. @RequestMapping("/m")
  34. public class MealController {
  35.  
  36. @Autowired
  37. private MealService mealService;
  38.  
  39. private final MealRepository mealRepository;
  40. private final StudentRepository studentRepository;
  41.  
  42. public MealController(MealRepository mealRepository, StudentRepository studentRepository){
  43. this.mealRepository = mealRepository;
  44. this.studentRepository = studentRepository;
  45. }
  46. @GetMapping(params = "form")
  47. public String createForm(@ModelAttribute Meal meal , Model model) {
  48. Iterable<Student> students = this.studentRepository.findAll();
  49. model.addAttribute("students" , students);
  50. return "meals/form";
  51. }
  52.  
  53. @PostMapping
  54. public ModelAndView create(@Valid Meal meal, BindingResult result,
  55. RedirectAttributes redirect) {
  56. if (result.hasErrors()) {
  57. return new ModelAndView("meals/form", "formErrors", result.getAllErrors());
  58. }
  59. meal = this.mealRepository.save(meal);
  60.  
  61. redirect.addFlashAttribute("globalMessage", "meals.view.success");
  62. return new ModelAndView("redirect:/m/{meal.id}", "meal.id", meal.getId());
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement