Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Entity
- public class Meal {
- @Id
- @GeneratedValue
- private Long id;
- @JoinColumn(name = "student_id")
- @ManyToOne(fetch = FetchType.LAZY)
- private Student mealCook;
- private String mealName;
- private int mealPrice;
- @Entity
- public class Student {
- @Id
- @GeneratedValue
- private Long id;
- private String studentName;
- @OneToMany(
- mappedBy = "mealCook",
- cascade = CascadeType.ALL,
- orphanRemoval = true
- )
- private List<Meal> meals = new ArrayList<>();
- @Controller
- @RequestMapping("/m")
- public class MealController {
- @Autowired
- private MealService mealService;
- private final MealRepository mealRepository;
- private final StudentRepository studentRepository;
- public MealController(MealRepository mealRepository, StudentRepository studentRepository){
- this.mealRepository = mealRepository;
- this.studentRepository = studentRepository;
- }
- @GetMapping(params = "form")
- public String createForm(@ModelAttribute Meal meal , Model model) {
- Iterable<Student> students = this.studentRepository.findAll();
- model.addAttribute("students" , students);
- return "meals/form";
- }
- @PostMapping
- public ModelAndView create(@Valid Meal meal, BindingResult result,
- RedirectAttributes redirect) {
- if (result.hasErrors()) {
- return new ModelAndView("meals/form", "formErrors", result.getAllErrors());
- }
- meal = this.mealRepository.save(meal);
- redirect.addFlashAttribute("globalMessage", "meals.view.success");
- return new ModelAndView("redirect:/m/{meal.id}", "meal.id", meal.getId());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement