[Solved] why did i get this result?

pugile-16 Asks: why did i get this result?
when i try to send a POST Request in a API REST done in spring boot the foreign key value is saved with null

this is my Authorities entity.

Code:
@Entity(name = "authorities")
public class Authorities {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id_authority")
    private long id;
    
    @Column(name="username" , insertable = false , updatable = false)
    private String username;
    private String authority;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "username" , referencedColumnName  ="username")
    @JsonIgnore
    private User user;

    public Authorities() {

    }

    public Authorities(String username, String authority, User user) {
        super();
        this.username = username;
        this.authority = authority;
        this.user = user;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getAuthority() {
        return authority;
    }

    public void setAuthority(String authority) {
        this.authority = authority;
    }

}

This is my User entity.

Code:
@Entity(name = "users")
public class User {

    @Id
    private String username;

    @Column(name = "password")
    private String password;

    private Boolean enabled;

    @OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
    private List<Authorities> authorities;

    public User() {
        super();
    }

    public User(String username, String password) {
        super();
        this.username = username;
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

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

    public List<Authorities> getAuthorities() {
        return authorities;
    }

    public void setAuthorities(List<Authorities> authorities) {
        this.authorities = authorities;
    }

    public Boolean getEnabled() {
        return enabled;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

}

this is my POST JSON REQUEST.

Code:
 {
        "username": "dani",
        "password": "nani",
        "enabled": true,
       
            "authorities":[
            {
                "username": "dani",
                "authority": "ROLE_ADMIN"
            }
        ]
        
    }

here is my tables in postgresql.

Code:
CREATE TABLE users (
    username varchar,
    password varchar,
    enabled boolean,
    CONSTRAINT pk_users PRIMARY KEY (username)
);

CREATE TABLE authorities (
    id_authority serial , 
    username varchar ,
    authority varchar,
    CONSTRAINT pk_authority PRIMARY KEY (id_authority),
    CONSTRAINT fk_authority FOREIGN KEY (username) REFERENCES users(username)
);

This is my result. in pgadmin

enter image description here

This is my UserController.

Code:
@RestController
@RequestMapping("/service")

public class UserController {

    @Autowired
    private UserRepository repo;
    @Autowired
    private AuthoritiesRepository repositori;

    @GetMapping
    public ResponseEntity<?> saludo() {
        return ResponseEntity.ok("hola mundo desde JPA App");
    }

    @GetMapping("/all")
    public List<User> getAll() {
        return this.repo.findAll();
    }

    @PostMapping("/user")
    public ResponseEntity<?> postUser(@RequestBody User user) {
        User u = this.repo.save(user);
        u.getAuthorities().stream().forEach(this.repositori::save);
        return ResponseEntity.status(HttpStatus.CREATED).body(u);
    }

    @GetMapping("/user/{username}")
    public ResponseEntity<?> getUserById(@PathVariable String username) {
        return ResponseEntity.ok(this.repo.findByUsername(username));
    }
}

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.