бесконечный цикл во время jpa retrive - PullRequest
0 голосов
/ 09 октября 2018

Я пытаюсь получить все Квесты из БД.Но вместо этого я получаю бесконечный цикл в теле ответа.Я пробовал @JsonIgnore и @JsonManagedReference, как предлагают другие похожие темы, и это не работает для меня ... Как я могу это исправить?

Классы:

@Entity
@Table(name = "Quest_table")
public class Quest {
@Id
@SequenceGenerator(name = "quest_seq", sequenceName = "quest_seq", initialValue = 1, allocationSize = 1)
@GeneratedValue(strategy = GenerationType.AUTO, generator = "quest_seq")
@Column(name = "quest_id")
private Integer id;

@Column(name = "Name", nullable = false)
private String name;

@ManyToOne(fetch = FetchType.LAZY,cascade = CascadeType.ALL)
@JoinColumn(name = "Owner_id", nullable = false)
@JsonManagedReference
private User owner;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "Executor_id", nullable = false)
private User executor;

.

@Entity
@Table(name = "user_table")
public class User implements UserDetails {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_seq")
@SequenceGenerator(name = "user_seq", sequenceName = "user_seq", initialValue = 1, allocationSize = 1)
@Column(name = "user_id")
private Integer id;

@Column(name = "email",
        unique = true)
@NotNull
@Email
private String email;

@ManyToOne(optional = false, fetch = FetchType.EAGER)
@JoinColumn(name = "role_id")
@JsonIgnore
private Role role;

@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "password_id")
@JsonIgnore
private Password passwordHash;

@OneToMany(fetch = FetchType.LAZY, mappedBy = "owner", targetEntity = 
  Quest.class)
@JsonBackReference
private Set<Quest> quests;

.

@Entity
@Table(name = "password_table")
public class Password implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "pass_seq")
@SequenceGenerator(name = "pass_seq", sequenceName = "pass_seq", initialValue = 1, allocationSize = 1)
@Column(name = "password_id")
@NotNull
private Integer id;

@Column(name = "hash",
        nullable = false,
        length = 100)
@NotNull
@Size(max = 100)
private String hash;

@OneToOne(cascade = CascadeType.ALL,
          fetch = FetchType.LAZY,
          mappedBy = "passwordHash")
private User user;

и вот RestController:

@RestController

открытый класс QuestController {

@Autowired
private QuestRepository questRepository;



@PostMapping("/quests")
public ResponseEntity<Object> createQuest(@RequestBody Quest quest){
    Quest savedQuest = questRepository.save(quest);
    URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}")
            .buildAndExpand(savedQuest.getId()).toUri();

    return ResponseEntity.created(location).build();
}

Пример ответа:

{"id":1,"name":"Quest1","description":null,"photo":null,"points":10,"accepted":true,"validTill":null,"owner":{"id":3,"email":"weronika.sawicka@xyz.pl","firstName":"weronika","lastName":"sawicka","instagram":null,"facebook":null,"role":{"id":2,"roleName":"child"},"passwordHash":{"id":3,"hash":"$2a$10$/7Mqd9W5Vsm5dEXrV.g/TeR8YzJyK5fZ17XvHVB/tHzj450LtIWVi","user":{"id":3,"email":"weronika.sawicka@xyz.pl","firstName":"weronika","lastName":"sawicka","instagram":null,"facebook":null,"role":{"id":2,"roleName":"child"},"passwordHash":{"id":3,"hash":"$2a$10$/7Mqd9W5Vsm5dEXrV.g/TeR8YzJyK5fZ17XvHVB/tHzj450LtIWVi","user":{"id":3
...