Bom dia, tudo bem pessoal? Eu estou tendo um problema para gerar o relacionamento entre duas tabelas no migrations, eu já tentei alterar de várias formas, inclusive ler a documentação, porém não consigo chegar na solução, pois sempre chego a outro erro. Podem me ajudar?
Erro: could not execute statement [Field 'user_id' doesn't have a default value] [insert into TB_USERS (full_name,user_password,user_name) values (?,?,?)]
Entitys
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;
@Table(name = "TB_USERS", schema = "public")
@Entity
@Data
@EqualsAndHashCode(of = "id")
public class User implements UserDetails {
@Id
@Column(name = "user_id", unique = true)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long 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(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinColumn(name = "user_id")
private List<CellPhone> cellPhones;
public User (){
}
public User(String fullName, String userName, String password, List<CellPhone> cellPhones) {
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.io.Serializable;
@Entity
@Table(name = "TB_CELLPHONES")
@Data
public class CellPhone implements Serializable {
@Id
@Column(name = "phone_id", unique = true)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "cell_phone")
private String cellphone;
@ManyToOne(targetEntity = User.class)
@JoinColumn(name = "TB_CELLPHONES_fk")
private int user_id;
public CellPhone(){}
public CellPhone(String cellphone) {
this.cellphone = cellphone;
}
}
Querys
CREATE TABLE TB_USERS(
user_id int NOT NULL PRIMARY KEY,
full_name varchar(255) NOT NULL,
user_name varchar(50) NOT NULL,
user_password varchar(100) NOT NULL
);
CREATE TABLE TB_CELLPHONES(
phone_id int NOT NULL PRIMARY KEY,
cell_phone MEDIUMTEXT NOT NULL,
user_id int,
CONSTRAINT TB_CELLPHONES_fk FOREIGN KEY (user_id) REFERENCES TB_USERS(user_id)
);