I am doing a course that I downloaded from the net (Spring with rest and angular) to develop a personal project, however I am facing some problems that I can not solve and I did not find solution in the "father of the donkeys" (Google). >
The scenario is as follows:
I'm developing the backend part initially and running the tests via Postman (get, post and put) ...
I have the Person class and the Employee class that inherits from Person. Each class has a Repository that inherits from JPARepository and has a controller class called Resource . >
When attempting to perform the person query (localhost ... / person), an error is shown that the fields that are in the employee list do not exist in the person table (unknown-column-in-field-list), however this field is of the employee's specialization.
I need each class to have a table, since I will create yet another class that inherits from Person.
I know I can do without inheritance but wanted to learn how to use inheritance in such a case. What am I doing wrong? Here are the codes below:
Class Person:
@Entity
@Table(name="pessoa")
@Inheritance(strategy = InheritanceType.JOINED)
public class Pessoa {
@Id
@Column(name="cd_Pessoa")
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected long cd_Pessoa;
@NotNull
@Size (min = 3, max = 50)
protected String nm_Pessoa;
...
Class Officer:
@Entity
@PrimaryKeyJoinColumn(name = "cd_Pessoa")
@Table(name="funcionario")
public class Funcionario extends Pessoa{
protected double salario;
protected double jornadaTrabalho;
PersonResource:
@RestController //meta dados. converte json em objeto
@RequestMapping ("/pessoas")
public class PessoaResource {
@Autowired
private PessoaRepository pessoaRepository;
@Autowired
private PessoaService pessoaService;
@Autowired
private ApplicationEventPublisher publisher;
@GetMapping
public List<Pessoa> listar(){
return pessoaRepository.findAll();
@PostMapping
public ResponseEntity<Pessoa> criar(@Valid @RequestBody Pessoa pessoa, HttpServletResponse response) {
Pessoa pessoaSalva = pessoaRepository.save(pessoa);
publisher.publishEvent(new RecursoCriadoEvent(this, response, pessoaSalva.getCd_Pessoa()));
return ResponseEntity.status(HttpStatus.CREATED).body(pessoaSalva);
}
@GetMapping("/{cd_Pessoa}")
public ResponseEntity<Pessoa> buscarPeloCodigo (@PathVariable Long cd_Pessoa) {
Pessoa pessoa = pessoaRepository.findOne(cd_Pessoa);
return pessoa != null ? ResponseEntity.ok(pessoa) : ResponseEntity.notFound().build();
}
}
PersonRepository
public interface PessoaRepository extends JpaRepository<Pessoa, Long>{
}
To not get too long the post, so the official also has the repository and resource classes just changing the object from person to official. If you do not understand something, just post it and try to explain it better.