I'm using Spring Data and I'm getting this exception and I'm not understanding why.
我正在使用Spring Data而且我遇到了这个异常,我不明白为什么。
The dataCadastro field in the domain is the only field with a different name in the database. In the base is as datacad
域中的dataCadastro字段是数据库中唯一具有不同名称的字段。在基地是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
使用getDataCadastro中的注释@Column(name = datacad)
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_
1 个解决方案
#1
2
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.
这是因为你在getter方法上使用@Id注释。所以jpa只查看getter来获取列名并忽略字段上的@column注释,但是当你将它放在getter上时就开始工作了。
As an exercise, you can move Id annotation to field level and should see it working again.
作为练习,您可以将Id注释移动到字段级别,并且应该再次查看它。
It is recommended to place jpa annotations either at field level or getters. but not to mix
建议在场级或getter中放置jpa注释。但不要混
#1
2
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.
这是因为你在getter方法上使用@Id注释。所以jpa只查看getter来获取列名并忽略字段上的@column注释,但是当你将它放在getter上时就开始工作了。
As an exercise, you can move Id annotation to field level and should see it working again.
作为练习,您可以将Id注释移动到字段级别,并且应该再次查看它。
It is recommended to place jpa annotations either at field level or getters. but not to mix
建议在场级或getter中放置jpa注释。但不要混