1
I am conducting a course that I downloaded from the net (Spring with Rest and angular) to develop a personal project, but I am facing some problems that I cannot solve and I did not find a solution in the "father of donkeys" (Google).
The scenario is as follows:
I am developing the back-end part initially and performing the tests via Postman (get, post and put)...
I got class Person and class Functionary inherited from Person. Each class has a Repository who inherits from Jparepository and have a class of controller call for Resource.
When trying to perform the person query (localhost.../person) error is shown that the fields that are in operation do not exist in the person table (Unknown-column-in-field-list), but this field is of the employee 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 I wanted to learn how to use inheritance in a case like this. What am I doing wrong? Follow the codes below:
Classe Pessoa:
@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;
...
Staff Class:
@Entity
@PrimaryKeyJoinColumn(name = "cd_Pessoa")
@Table(name="funcionario")
public class Funcionario extends Pessoa{
protected double salario;
protected double jornadaTrabalho;
Pessoaresource:
@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();
}
}
Pessoarepository
public interface PessoaRepository extends JpaRepository<Pessoa, Long>{
}
In order not to get too long the post, to run it also has the classes of Repository and Resource just changing the person object to run. In case you don’t understand something, just post that I try to explain better.
First I think you should define the purpose of this inheritance. If you want to see some examples, I recommend this link: https://www.thoughts-on-java.org/complete-guide-inheritance-strategies-jpa-hibernate/. Note the Superclass annotations in each example
– DiegoAugusto
The idea is to have a customer class that will also inherit from person and already thinking about some specialties of the client I believe that inheritance would be a good to use. I think my problem is in the return of the list method... I will try with the link annotations you sent, which by the way is very good. Thank you for the reply.
– Michel
And also the idea is to have a single registration page and only differentiate by a combo on the screen, without the need to have a person type field in the database.
– Michel
As I commented earlier the problem was precisely in findAll’s Return that I was passing person... I also fixed an issue related to database and everything is working... But I’m only in the back-end... my question now is: How will I distinguish which Pository call to save, create, query in the front-end?
– Michel
Wow, I’m so glad you could fix it. Put your solution as an answer to help the crowd. And about your new doubt, I think I could open up another question
– DiegoAugusto
To define which Repository to call the best is to have one resource for client and another to work. For example, for client,
/clientes
; for staff/funcionarios
.– romarcio
In addition to creating the endpoints for each refusal it is good to use the protocols http POST, GET, PUT and DELETE according to each operation.
– diego.samuel.alves