Issue
I'm using hibernate to read my database datas but i'm having a issue with the relationship of the tables when i run a query. And i don't find the reason That's the error:
2021-01-23 18:16:55.427 WARN 1192 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 42883 2021-01-23 18:16:55.427 ERROR 1192 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR: operator does not exist: bigint = character varying Dica: No operator matches the given name and argument types. You might need to add explicit type casts. Posição: 384
That's the query:
public interface QueryUsuario extends CrudRepository<EntidadeUsuario, Integer> {
@Query(value = "select * from usuario where email = :email", nativeQuery = true)
public EntidadeUsuario consultaUsuarioPorPorEmail(@Param("email") String email);
}
And that's how I call the query:
public void consultaUsuario(String email) {
var x = queryUsuario.consultaUsuarioPorPorEmail(email);
}
Entities:
@Getter
@Setter
@Table(name = "usuario")
@Entity
public class EntidadeUsuario {
@Id
@Column(name = "id_usuario")
String idUsuario;
@Column(name = "cd_usuario")
String usuario;
@Column(name = "senha")
String senha;
@Column(name = "email")
String email;
@Column(name = "nome")
String nome;
@Column(name = "descricao")
String descricao;
@Column(name = "endereco")
String endereco;
@Column(name = "foto")
String fotoBase64;
@Column(name = "dt_atz")
Date data;
@OneToMany(mappedBy = "idUsuario")
private List<EntidadeUsuarioCursos> entidadeUsuarioCursos;
// @OneToMany(mappedBy = "usuario")
// private List<EntidadeGrupoEstudoUsuario> entidadeGrupoEstudoUsuario;
}
@Getter
@Setter
@Table(name = "usuario_perfil_cursos_interesse")
@Entity
public class EntidadeUsuarioCursos {
@Id
@Column(name = "id_usuario_perfil_cursos_interesse")
Integer idUsuarioPerfilCursosInteresse;
@Getter(AccessLevel.NONE)
@ManyToOne
@JoinColumn(name = "id_usuario")
EntidadeUsuario idUsuario;
@Column(name = "id_curso")
Integer idCurso;
@Column(name = "dt_atz")
Date data;
}
sql script:
CREATE TABLE public.usuario (
id_usuario bigint DEFAULT nextval('public.usuario_id_seq'::regclass) NOT NULL,
cd_usuario character varying NOT NULL,
senha character varying NOT NULL,
dt_atz timestamp(0) without time zone,
email character varying,
nome character varying,
endereco character varying,
descricao character varying,
foto bytea
);
ALTER TABLE ONLY public.usuario
ADD CONSTRAINT usuario_pk PRIMARY KEY (id_usuario);
CREATE TABLE public.usuario_perfil_cursos_interesse (
id_usuario bigint NOT NULL,
id_curso bigint NOT NULL,
dt_atz timestamp(0) without time zone,
id_usuario_perfil_cursos_interesse bigint DEFAULT nextval('public.usuario_perfil_cursos_interesse_id_seq'::regclass) NOT NULL
);
ALTER TABLE ONLY public.usuario_perfil_cursos_interesse
ADD CONSTRAINT usuario_perfil_cursos_interesse_fk FOREIGN KEY (id_usuario) REFERENCES public.usuario(id_usuario);
ALTER TABLE ONLY public.usuario_perfil_cursos_interesse
ADD CONSTRAINT usuario_perfil_cursos_interesse_fk_1 FOREIGN KEY (id_curso) REFERENCES public.curso(id_curso);
Solution
I got it, this error occurred cause EntidadeUsuario id is a Integer, not a String. So I changed that:
@Id
@Column(name = "id_usuario")
String idUsuario;
To that:
@Id
@Column(name = "id_usuario")
Integer idUsuario;
Answered By - Capybara