How to implement @ForeignKey correctly using Spring Boot

Sergio

I am developing my first application using Spring Boot to build my web service. The application is supposed to be a simple gym management system. Actually I managed to create model, controller and repository for Admin (gym owner entity) and Palestra (gym entity).

Admin.java

@Entity(name="admin")
public class Admin {
    private @Id @GeneratedValue Long id_admin;
    private String nome;
    private String cognome;
    private String email;
    private String password;
    
    // Retionships
    @OneToMany(mappedBy="admin", fetch = FetchType.LAZY)
    private List <Palestra> palestre;
    
    Admin() {} // Empty constructor

    public Admin(Long id_admin, String nome, String cognome, String email, String password) {
        super();
        this.nome = nome;
        this.cognome = cognome;
        this.email = email;
        this.password = password;
    }

    public Long getId_admin() {
        return id_admin;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getCognome() {
        return cognome;
    }

    public void setCognome(String cognome) {
        this.cognome = cognome;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public List<Palestra> getPalestre() {
        return palestre;
    }

    public void setPalestre(List<Palestra> palestre) {
        this.palestre = palestre;
    }
    
}

AdminController.java

@RestController
public class AdminController {
    private final AdminRepository adminRepository;
    
    public AdminController(AdminRepository repository) {
        adminRepository = repository;
    }
    
    @GetMapping("/admins")
    Iterable<Admin> getAdmins() {
        return adminRepository.findAll();
    }
    
    @PostMapping("/admins")
    Admin createAdmin(@RequestBody Admin newAdmin) {
        return adminRepository.save(newAdmin);
    }
}

Palestra.java

@Entity(name="palestra")
public class Palestra {
    private @Id @GeneratedValue Long id_palestra;
    private String nome;
    private String via;
    private int civico;
    private String citta;
    
    // Relationships
    @OneToMany(mappedBy="palestra")
    private List<Trainer> trainers;
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="id_admin")
    private Admin admin;
    
    public Palestra() {} // Empty constructor

    public Palestra(Long id_palestra, String nome, String via, int civico, String citta) {
        super();
        this.id_palestra = id_palestra;
        this.nome = nome;
        this.via = via;
        this.civico = civico;
        this.citta = citta;
    }

    public Long getId_palestra() {
        return id_palestra;
    }

    public void setId_palestra(Long id_palestra) {
        this.id_palestra = id_palestra;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getVia() {
        return via;
    }

    public void setVia(String via) {
        this.via = via;
    }

    public int getCivico() {
        return civico;
    }

    public void setCivico(int civico) {
        this.civico = civico;
    }

    public String getCitta() {
        return citta;
    }

    public void setCitta(String citta) {
        this.citta = citta;
    }

    public Admin getAdmin() {
        return admin;
    }

    public void setAdmin(Admin admin) {
        this.admin = admin;
    }

}

PalestraController.java

@RestController
public class PalestraController {
    private final PalestraRepository palestraRepository;
    
    public PalestraController(PalestraRepository repository) {
        palestraRepository = repository;
    }
    
    @GetMapping("/palestre")
    Iterable<Palestra> getPalestre() {
        return palestraRepository.findAll();
    }
    
    @PostMapping("/palestre")
    Palestra createPalestra(@RequestBody Palestra newPalestra) {
        return palestraRepository.save(newPalestra);
    }
}

I used Postman to send and test requests: enter image description hereenter image description here and this is the result stored in the database: enter image description here

As you can see id_admin is supposed to be foreign key but has a NULL value. How I can solve this problem?

Sergio

I answer my own question because I found the problem. In the way I created models, controllers and repositories the entities were already linked by the relationship (@ManyToOne and @OneToMany). The real "problem" was how the request was submitted through Postman. I had to insert in my request the admin_id value manually like this: enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to implement put request correctly in spring boot?

From Dev

How to implement @Async in spring boot correctly in term of thread safety?

From Dev

How to implement Spring Security Ldap authentication using the configurer class correctly?

From Dev

How to correctly instantiate a class using @Autowired in a mapper interface in Spring Boot?

From Dev

How to implement One to Many relationship in Spring Boot JPA using Flyway

From Dev

How to implement an authentication method using Spring Boot and JPA?

From Dev

How to correctly implement kmalloc using C?

From Java

How to implement Spring Boot Microservice with two levels of token authentication using Spring Security?

From Java

How to work correctly with ThreadPoolExecutor in Spring Boot Application

From Java

spring boot - how to correctly define template locations?

From Dev

Spring Boot - How to test an image is correctly served

From Dev

Spring boot how to use @PostConstruct correctly

From Java

How to format the date time correctly in Spring Boot?

From Java

How to correctly join tables in Spring Boot

From Dev

How to correctly integrate HSQLDB with Spring Boot?

From Java

How to implement Logout feature using jwt tokens in spring boot backend(using rest end points) Implementation

From Dev

How to implement Spring Boot service classes without using impl and using a interface as dependency as DIP principle says?

From Java

How to implement Decorator pattern in Spring Boot

From Dev

How to implement spring boot Redis multi tenancy?

From Java

How to implement an Error page Spring boot

From Java

How to implement security in a spring boot application?

From Java

How to implement multitenancy for redis in spring boot

From Java

How to implement pagination in spring boot with hibernate

From Dev

How to implement jms queue in spring boot

From Dev

How to implement the IS-A relationship in Hibernate Spring Boot?

From Dev

how to implement auction logic in spring boot

From Java

Using Spring Boot's ErrorController and Spring's ResponseEntityExceptionHandler correctly

From Java

how to implement pagination front-end in spring boot web application using jsp

From Java

How to correctly implement a spring-websocket java client

Related Related

  1. 1

    How to implement put request correctly in spring boot?

  2. 2

    How to implement @Async in spring boot correctly in term of thread safety?

  3. 3

    How to implement Spring Security Ldap authentication using the configurer class correctly?

  4. 4

    How to correctly instantiate a class using @Autowired in a mapper interface in Spring Boot?

  5. 5

    How to implement One to Many relationship in Spring Boot JPA using Flyway

  6. 6

    How to implement an authentication method using Spring Boot and JPA?

  7. 7

    How to correctly implement kmalloc using C?

  8. 8

    How to implement Spring Boot Microservice with two levels of token authentication using Spring Security?

  9. 9

    How to work correctly with ThreadPoolExecutor in Spring Boot Application

  10. 10

    spring boot - how to correctly define template locations?

  11. 11

    Spring Boot - How to test an image is correctly served

  12. 12

    Spring boot how to use @PostConstruct correctly

  13. 13

    How to format the date time correctly in Spring Boot?

  14. 14

    How to correctly join tables in Spring Boot

  15. 15

    How to correctly integrate HSQLDB with Spring Boot?

  16. 16

    How to implement Logout feature using jwt tokens in spring boot backend(using rest end points) Implementation

  17. 17

    How to implement Spring Boot service classes without using impl and using a interface as dependency as DIP principle says?

  18. 18

    How to implement Decorator pattern in Spring Boot

  19. 19

    How to implement spring boot Redis multi tenancy?

  20. 20

    How to implement an Error page Spring boot

  21. 21

    How to implement security in a spring boot application?

  22. 22

    How to implement multitenancy for redis in spring boot

  23. 23

    How to implement pagination in spring boot with hibernate

  24. 24

    How to implement jms queue in spring boot

  25. 25

    How to implement the IS-A relationship in Hibernate Spring Boot?

  26. 26

    how to implement auction logic in spring boot

  27. 27

    Using Spring Boot's ErrorController and Spring's ResponseEntityExceptionHandler correctly

  28. 28

    how to implement pagination front-end in spring boot web application using jsp

  29. 29

    How to correctly implement a spring-websocket java client

HotTag

Archive