Java 弹簧靴&x2B;事件应用程序中的休眠控制器
我想创建一个事件应用程序,用户可以使用它添加事件,然后添加注释。我的注释控制器有问题。我希望将注释添加到comments and events表中,并且注释与事件相关 我应该如何修改控制器Java 弹簧靴&x2B;事件应用程序中的休眠控制器,java,spring,spring-boot,hibernate,thymeleaf,Java,Spring,Spring Boot,Hibernate,Thymeleaf,我想创建一个事件应用程序,用户可以使用它添加事件,然后添加注释。我的注释控制器有问题。我希望将注释添加到comments and events表中,并且注释与事件相关 我应该如何修改控制器 @Controller @RequestMapping("/comments") public class CommentController { @Autowired private CommentService commentService; @Autowire
@Controller
@RequestMapping("/comments")
public class CommentController {
@Autowired
private CommentService commentService;
@Autowired
private EventService eventService;
@GetMapping("/newComment")
public String newComment(Model model) {
Comment comment = new Comment();
model.addAttribute("comment", comment);
return "redirect:/list";
}
@PostMapping("/saveComment/{id}")
public String createComment( @PathVariable long id, @ModelAttribute Comment comment) {
// save comment to database
model.addAttribute("event", event);
commentService.saveComment(comment);
List comments = new ArrayList();
comments.add(commentService.createComment(comment));
event.setComments(comments);
return "redirect:/list";
}
事件控制器:
@Controller
public class EventController {
@Autowired
private EventService eventService;
// display list of events
@GetMapping("/list")
public String viewHomePage(Model model) {
model.addAttribute("listEvents", eventService.getAllEvents());
return "list";
}
@GetMapping("/showNewEventForm")
public String showNewEventForm(Model model) {
// create model attribute to bind form data
Event event = new Event();
model.addAttribute("event", event);
return "new_event";
}
@PostMapping("/saveEvent")
public String saveEvent(@ModelAttribute("event") Event event) {
// save event to database
eventService.saveEvent(event);
return "redirect:/list";
}
@GetMapping("/showFormForUpdate/{id}")
public String showFormForUpdate(@PathVariable( value = "id") long id, Model model) {
// get event from the service
Event event = eventService.getEventById(id);
// set event as a model attribute to pre-populate the form
model.addAttribute("event", event);
return "update_event";
}
@GetMapping("/deleteEvent/{id}")
public String deleteEvent(@PathVariable (value = "id") long id) {
// call delete event method
this.eventService.deleteEventById(id);
return "redirect:/list";
}
@GetMapping("/showDescription/{id}")
public String showDescription(@PathVariable( value = "id") long id, Model model, Model model2) {
// get event from the service
Event event = eventService.getEventById(id);
Comment comment = new Comment();
// set event as a model attribute to pre-populate the form
model.addAttribute("event", event);
model2.addAttribute("comment", comment);
return "description";
}
}
注释实体:
@Entity
@Table(name = "comments")
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "text")
private String text;
@Column(name = "rating")
private int rating;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "eve_com")
private Event event;
public Comment() {
}
public long getId() {return id;}
public void setId(long id) {this.id = id;}
public String getText() {return text; }
public void setText(String text) {this.text = text;}
public int getRating() {return rating; }
public void setRating(int rating) {this.rating = rating;}
public Event getEvent() {
return event;
}
public void setEvent(Event event) {
this.event = event;
}
}
评论服务:
@Service
public class CommentServiceImpl implements CommentService {
@Autowired
private CommentRepository commentRepository;
@Override
public List<Comment> getAllComment() {
return commentRepository.findAll();
}
@Override
public Object createComment(Comment comment) {
this.commentRepository.save(comment);
return null;
}
}
@Service
public class EventServiceImpl implements EventService {
@Autowired
private EventRepository eventRepository;
@Override
public List <Event> getAllEvents() {
return eventRepository.findAll();
}
@Override
public void saveEvent(Event event) {
this.eventRepository.save(event);
}
@Override
@PutMapping
public Event getEventById(long id) {
Optional < Event > optional = eventRepository.findById(id);
Event event = null;
if (optional.isPresent()) {
event = optional.get();
} else {
throw new RuntimeException("Event not found for id ::" + id);
}
return event;
}
@Override
public void deleteEventById(long id) {
this.eventRepository.deleteById(id);
}
}
@服务
公共类CommentServiceImpl实现CommentService{
@自动连线
私有评论库;
@凌驾
公共列表getAllComment(){
返回commentRepository.findAll();
}
@凌驾
公共对象createComment(Comment){
this.commentRepository.save(comment);
返回null;
}
}
事件实体:
@Entity
@Table(name = "events")
public class Event {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "name")
private String name;
@Column(name = "city")
private String city;
@Column(name = "date")
private String date;
@Column(name = "description")
private String description;
// one to many relationship with messages
@OneToMany(cascade = CascadeType.ALL)
List<Comment> comments = new ArrayList<>();
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() { return name;}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public List<Comment> getComments() { return comments;}
public void setComments(List<Comment> comments) {this.comments = comments;}
}
@实体
@表(name=“events”)
公开课活动{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
私人长id;
@列(name=“name”)
私有字符串名称;
@列(name=“city”)
私人城市;
@列(name=“date”)
私有字符串日期;
@列(name=“description”)
私有字符串描述;
//与消息的一对多关系
@OneToMany(级联=级联类型.ALL)
列表注释=新建ArrayList();
公共长getId(){
返回id;
}
公共无效集合id(长id){
this.id=id;
}
公共字符串getName(){return name;}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getCity(){
回归城市;
}
公共城市(字符串城市){
this.city=城市;
}
公共字符串getDate(){
返回日期;
}
公共无效设置日期(字符串日期){
this.date=日期;
}
公共字符串getDescription(){
返回说明;
}
公共void集合描述(字符串描述){
this.description=描述;
}
public List getComments(){return comments;}
public void setComments(列出注释){this.comments=comments;}
}
活动及服务:
@Service
public class CommentServiceImpl implements CommentService {
@Autowired
private CommentRepository commentRepository;
@Override
public List<Comment> getAllComment() {
return commentRepository.findAll();
}
@Override
public Object createComment(Comment comment) {
this.commentRepository.save(comment);
return null;
}
}
@Service
public class EventServiceImpl implements EventService {
@Autowired
private EventRepository eventRepository;
@Override
public List <Event> getAllEvents() {
return eventRepository.findAll();
}
@Override
public void saveEvent(Event event) {
this.eventRepository.save(event);
}
@Override
@PutMapping
public Event getEventById(long id) {
Optional < Event > optional = eventRepository.findById(id);
Event event = null;
if (optional.isPresent()) {
event = optional.get();
} else {
throw new RuntimeException("Event not found for id ::" + id);
}
return event;
}
@Override
public void deleteEventById(long id) {
this.eventRepository.deleteById(id);
}
}
@服务
公共类EventServiceImpl实现EventService{
@自动连线
私有事件存储库事件存储库;
@凌驾
公共列表getAllEvents(){
返回eventRepository.findAll();
}
@凌驾
公共作废保存事件(事件){
this.eventRepository.save(事件);
}
@凌驾
@PutMapping
公共事件getEventById(长id){
可选Optional=eventRepository.findById(id);
事件=null;
if(可选的.isPresent()){
event=optional.get();
}否则{
抛出新的RuntimeException(“未找到id::“+id的事件”);
}
返回事件;
}
@凌驾
public void deleteEventById(长id){
this.eventRepository.deleteById(id);
}
}
尝试在链接事件实体和注释实体时考虑@ MyTyOne注释。试着用谷歌搜索一下。然后,您可以保存特定事件的注释。