2

我正在尝试进行简单的选择,但出现此错误:

org.hibernate.exception.SQLGrammarException: Column name 'mesAno' invalid.
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:122)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129)
at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
at $Proxy64.executeQuery(Unknown Source)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1897)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1698)
at org.hibernate.loader.Loader.doQuery(Loader.java:832)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:293)
at org.hibernate.loader.Loader.doList(Loader.java:2382)
at org.hibernate.loader.Loader.doList(Loader.java:2368)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2198)
at org.hibernate.loader.Loader.list(Loader.java:2193)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:470)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:355)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:195)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1244)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
at br.com.gep.persistencia.DaoBasico.recuperarTodos(DaoBasico.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy62.recuperarTodos(Unknown Source)
at br.com.gep.sped.extrator.csv.arvore.CriadorArquivoCSVArvoreCompetencia.recuperarRegistros(CriadorArquivoCSVArvoreCompetencia.java:37)
at br.com.gep.sped.extrator.csv.CriadorArquivoCSV.escreverArquivoCsv(CriadorArquivoCSV.java:89)
at br.com.gep.sped.extrator.csv.CriadorArquivoCSV.criar(CriadorArquivoCSV.java:55)
at br.com.gep.sped.extrator.csv.CriadorConjuntoArquivosCSVArvore.criarArquivosCSV(CriadorConjuntoArquivosCSVArvore.java:113)
at br.com.gep.sped.extrator.csv.CriadorConjuntoArquivosCSVArvore.criarConjuntoDeArquivos(CriadorConjuntoArquivosCSVArvore.java:94)
at br.com.gep.sped.extrator.TarefaExtracao.extrairDados(TarefaExtracao.java:70)
at br.com.gep.sped.extrator.TarefaExtracao.extrairDadosEEnviar(TarefaExtracao.java:48)
at br.com.gep.sped.extrator.TarefaExtracao.extrair(TarefaExtracao.java:36)
at br.com.gep.sped.extrator.TarefaExtracao.run(TarefaExtracao.java:30)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:53)
at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:178)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:292)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: column name 'mesAno' invalid.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:197)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1493)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:390)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:340)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4575)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1400)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:179)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:154)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(SQLServerPreparedStatement.java:283)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)
... 46 more

这是我的实体。如您所见,它只有一列(它是一个简单的视图)。

@javax.persistence.Table(name = "WIN_ARVORE_EFD_COMPETENCIA", schema = "dbo", catalog = "SPED_WIN")
@Entity
public class VisaoArvoreEfdCompetencia {
    @Id
    private String mesAno;

    @javax.persistence.Column(name = "MES_ANO", nullable = true, insertable = true, updatable = true, length = 7, precision = 0)
    public String getMesAno() {
        return mesAno;
    }

    public void setMesAno(String mesAno) {
        this.mesAno = mesAno;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        VisaoArvoreEfdCompetencia that = (VisaoArvoreEfdCompetencia) o;
        if (mesAno != null ? !mesAno.equals(that.mesAno) : that.mesAno != null) return false;
        return true;
    }

    @Override
    public int hashCode() {
        return mesAno != null ? mesAno.hashCode() : 0;
    }
}

这是这个实体的观点:

SELECT CAST(MONTH(DATAFISCAL) AS VARCHAR(2)) + '_' + CAST(YEAR(DATAFISCAL) AS VARCHAR(4)) AS MES_ANO
FROM dbo.MESFISCAL
WHERE (Status = N'AB')

我的实体映射或视图中有问题吗?

4

2 回答 2

3

@Column使用注释在字段上指定列名。这将对实体使用字段访问,而不是混合文件和设置器注释。

@Id
@Column(name="MES_ANO", nullable = true, insertable = true, updatable = true, length = 7, precision = 0)
private String mesAno;

public String getMesAno() {
    return mesAno;
}
于 2013-01-08T12:03:44.947 回答
3

来自http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/

应避免在字段和方法中混合注释。Hibernate 会从@Id 或@EmbeddedId 的位置猜测访问类型。

如果将 @Id 注释放在字段上,则需要将其他 JPA 注释(@Column 等)放在字段(而不是 getter)上。您不能在 getter / 字段上混合注释。

所以在这里你可以写:

@Id
@Column(name="MES_ANO", length = 7)
private String mesAno;

请注意,由于该列已经是 @Id,因此您的可空/可插入/可更新声明可能没有意义。

于 2013-01-08T12:25:44.440 回答