Lista elementos da tabela eventos | Fórum | School of Net

Deseja poder participar de nosso fórum e tirar todas as suas dúvidas?
Clique aqui e assine nosso plano de acesso ilimitado. Saiba mais.

por Laryssa

2 meses, 2 semanas atrás Laryssa

Lista elementos da tabela eventos

Erro do console ``` Resolved [org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: failed to lazily initialize a collection of role: com.example.demo.models.Event.people, could not initialize proxy - no Session; nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: com.example.demo.models.Event.people, could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->com.example.demo.models.Event["people"])] ``` Meu EventController ``` package com.example.demo.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import com.example.demo.models.Event; import com.example.demo.repository.EventRepository; @RestController @RequestMapping("/events") public class EventController { @Autowired private EventRepository eventRepository; public EventController(EventRepository eventRepository) { this.eventRepository = eventRepository; } @GetMapping @ResponseBody public List<Event> findAll() { return (List<Event>) this.eventRepository.findAll(); } @PostMapping @ResponseBody public Event create(@RequestBody Event event) { this.eventRepository.save(event); return event; } } ``` Meu models event e person ``` package com.example.demo.models; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; @Entity @Table(name = "events") public class Event { @Column @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column private String name; @Column private Double value; @Column @OneToMany(cascade = CascadeType.ALL, mappedBy = "eventId") private Set<Person> people; public Event() { } public Event(Long id, String name, Double value) { this.id = id; this.name = name; this.value = value; } public Event(String name, Double value) { this.name = name; this.value = value; } 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 Double getValue() { return value; } public void setValue(Double value) { this.value = value; } public Set<Person> getPeople() { return people; } public void setPeople(Set<Person> people) { this.people = people; } } package com.example.demo.models; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; import com.fasterxml.jackson.annotation.JsonBackReference; @Entity @Table(name="people") public class Person { @Column @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column private String name; @ManyToOne @JoinColumn(name = "event_id") @JsonBackReference private Event eventId; public Person() { } public Person(Long id, String name, Event event) { this.setId(id); this.setName(name); this.setEventId(event); } public Person(String name, Event event) { this.setName(name); this.setEventId(event); } 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 Event getEventId() { return eventId; } public void setEventId(Event eventId) { this.eventId = eventId; } } ```

2 Respostas