Witam,
Posiadam mały problem z kodem
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String surname;
private String email;
@ManyToOne(fetch= FetchType.EAGER)
//@OneToOne
@JoinColumn(name = "position_id")
private Position position;
public class Position {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToMany(fetch= FetchType.EAGER, mappedBy = "position")
private List<Person> persons;
public String position;
Wyswietlanie listy
@RequestMapping(value = {"/persons"}, method = RequestMethod.GET, produces = "application/json")
public ResponseEntity showPersons() {
List<Employee> employees = new ArrayList<>();
for (Person p : personRepository.findAll()) {
Employee employee = new Employee();
employee.setName(p.getName());
employee.setSurname(p.getSurname());
employee.setEmail(p.getEmail());
// employee.setPositionName());
employees.add(employee);
}
return new ResponseEntity(employees, HttpStatus.OK);
//return new ResponseEntity(personRepository.findAll(), HttpStatus.OK);
}
Dodawanie
@RequestMapping(value = "/add", method = RequestMethod.POST, produces = "application/json")
public ResponseEntity addPerson(@RequestBody Employee employee) {
try {
Position newPosition;
Person newPerson = new Person();
List<Position> ps = positionRepository.findByPosition(employee.getPositionName());
Position dbPosition = ps.get(0);
if (dbPosition != null && employee.getPositionName().equals(dbPosition.getPosition())) {
newPerson.setPosition(ps.get(0));
} else {
Position position = new Position();
position.setPosition(employee.getPositionName());
newPosition = positionRepository.save(position);
newPerson.setPosition(newPosition);
}
newPerson.setName(employee.getName());
newPerson.setSurname(employee.getSurname());
newPerson.setEmail(employee.getEmail());
personRepository.save(newPerson);
} catch (Exception e) {
// logger.error("Error message", e);
return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
}
return new ResponseEntity(HttpStatus.OK);
}
Klasa Employee posiada 4 stringi name surname email i positionName
Wszystko dobrze działało przy wyswietlaniu ale dzisiaj jak probowalem wyswietlic za pomoca
return new ResponseEntity(personRepository.findAll(), HttpStatus.OK);
to wywala bledy i zaczalem pisac kod ktory jest obecnie ale nie moge przekazac nazwy stanowiska do setPositionName
Jakie pomysły jak to poprawic albo co jest zle?