Hibernate无效的列名

时间:2013-01-08 12:02:04

标签: java sql-server-2008 hibernate entities

我正在尝试做一个简单的选择,我收到了这个错误:

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')

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

2 个答案:

答案 0 :(得分:3)

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

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

public String getMesAno() {
    return mesAno;
}

答案 1 :(得分:3)

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

  

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

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

所以在这里你可以写:

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

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