Reputation: 649
I'm using Spring Data and I'm getting this exception and I'm not understanding why.
The dataCadastro field in the domain is the only field with a different name in the database. In the base is as datacad
Repository
public interface IRepositorioUsuario extends CrudRepository<Usuario, Long> {
}
Service
@Stateless
public class UsuarioService {
@Inject
IRepositorioUsuario usuarioRepositorio;
public void buscar() {
usuarioRepositorio.findAll().forEach(u -> System.out.println(u.getNome()));
}
}
Domain
@Entity
@Table(name="TUSUARIO")
public class Usuario implements Serializable {
private static final long serialVersionUID = -5427866189669150032L;
private Long codigo;
private String nome;
private String login;
private String senha;
@Column(name="datacad") // query error.....
private Date datacadastro;
private Boolean situacao;
@Id
public Long getCodigo() {
return codigo;
}
public void setCodigo(Long codigo) {
this.codigo = codigo;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getSenha() {
return senha;
}
public void setSenha(String senha) {
this.senha = senha;
}
public Date getDatacadastro() {
return datacadastro;
}
public void setDatacadastro(Date datacadastro) {
this.datacadastro = datacadastro;
}
public Boolean getSituacao() {
return situacao;
}
public void setSituacao(Boolean situacao) {
this.situacao = situacao;
}
// Omitting hasCode
Error Query
SQL Error: 1054, SQLState: 42S22
Unknown column 'usuario0_.datacadastro' in 'field list'
select
usuario0_.codigo as codigo1_0_,
usuario0_.datacadastro as datacada2_0_,
usuario0_.login as login3_0_,
usuario0_.nome as nome4_0_,
usuario0_.senha as senha5_0_,
usuario0_.situacao as situacao6_0_
from TUSUARIO usuario0_
With the annotation @Column(name = datacad) in getDataCadastro
Seccess
select
usuario0_.codigo as codigo1_0_,
usuario0_.datacad as datacad2_0_,
usuario0_.login as login3_0_,
usuario0_.nome as nome4_0_,
usuario0_.senha as senha5_0_,
usuario0_.situacao as situacao6_0_
from TUSUARIO usuario0_
Upvotes: 1
Views: 337
Reputation: 8247
It is because you are using @Id annotation on the getter method. And so the jpa looks only at the getters to derive the column names and ignores the @column annotation on the field but starts working when you place it on the getter.
As an exercise, you can move Id annotation to field level and should see it working again.
It is recommended to place jpa annotations either at field level or getters. but not to mix
Upvotes: 2