Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

Bug ao salvar no banco

Boa tarde pessoal, tudo bem? Estou tentando criar um objeto no banco, e quando coloca a relação dele está dando isso:

Field 'user_id' doesn't have a default value

package com.igaopk.med.MedicineApi.users.entitys;

import jakarta.persistence.*;
import lombok.*;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.UUID;

@Table(name = "TB_USERS")
@Entity(name = "User")
@Getter
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(of = "id")
public class User implements UserDetails {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "user_id", unique = true)
    private UUID id;

    @Column(name = "full_name")
    private String fullName;

    @Column(name = "user_name", unique = true)
    private String userName;

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

    @OneToMany(fetch = FetchType.LAZY)
    @JoinColumn(referencedColumnName = "user_id")
    private List<CellPhone> cellPhones;

    public User(String fullName, String userName, String password, List<CellPhone> cellPhones) {
        this.id = UUID.randomUUID();
        this.fullName = fullName;
        this.userName = userName;
        this.password = password;
        this.cellPhones = cellPhones;
    }

    public void update(String fullName, String userName, String password, List<CellPhone> cellPhones){
        if(!fullName.isBlank()){
            this.fullName = fullName;
        }

        if(!userName.isBlank() || userName.length() < 4){
            this.userName = userName;
        }

        if(!password.isBlank() || password.length() < 4){
            this.password = password;

        }

        this.cellPhones = cellPhones;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return List.of(new SimpleGrantedAuthority("ROLE_USER"));
    }

    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return this.userName;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
package com.igaopk.med.MedicineApi.users.entitys;

import jakarta.persistence.*;
import lombok.Data;

import java.util.UUID;

@Entity
@Table(name = "TB_CELLPHONES")
@Data
public class CellPhone {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "phone_id", unique = true)
    private UUID id;

    @Column(name = "cell_phone")
    private String cellphone;

    @Column(name = "user_id")
    private UUID userId;

    public CellPhone(String cellphone){
        this.id = UUID.randomUUID();
        this.cellphone = cellphone;
    }
}
1 resposta
solução!

Oi!

Como você está gerando o id pela na própria aplicação, então deve remover a anotação @GeneratedValue do atributo id.