1
resposta

JPA HIBERNATE COM MIGRATIONS

Olá pessoal, boa noite!! Estou tentando criar uma migration e usar com o JPA, mas sempre dá um erro de coluna. Alguém pode me ajudar?

JDBC exception executing SQL [select u1_0.user_id,u1_0.full_name,u1_0.user_password,u1_0.user_name,c1_0.User_user_id,c1_1.phone_id,c1_1.cell_phone from TB_USERS u1_0 left join (TB_USERS_cellPhones c1_0 join TB_CELLPHONES c1_1 on c1_1.phone_id=c1_0.cellPhones_phone_id) on u1_0.user_id=c1_0.User_user_id where u1_0.user_id=?] [Table 'easysocial.TB_USERS_cellPhones' doesn't exist] [n/a]; SQL [n/a]

Minhas querys

CREATE TABLE TB_USERS(
    user_id binary(16) 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 binary(16) PRIMARY KEY,
    cell_phone MEDIUMTEXT,
    user_id binary(16),

    FOREIGN KEY (user_id) REFERENCES TB_USERS(user_id)
);

Objetos

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", schema = "public")
@Entity(name = "User")
@Data
@EqualsAndHashCode(of = "id")
public class User implements UserDetails {

    @Id
    @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(cascade = CascadeType.ALL, orphanRemoval = true)
    private List<CellPhone> cellPhones;

    public User (){

    }

    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.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.Data;

import java.util.UUID;

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

    @Id
    @Column(name = "phone_id", unique = true)
    private UUID id;

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

    public CellPhone(){}

    public CellPhone(String cellphone) {
        this.id = UUID.randomUUID();
        this.cellphone = cellphone;
    }
}
1 resposta

Oi!

É um problema de mapeamento incorreto no relacionamento entre user e cellphone

Na classe User o mapeamento deveria estar assim:

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "user")
private List<CellPhone> cellPhones;

E na classe CellPhone você esqueceu de mapear o relacionamento com User:

@ManyToOne
private User user;