gpt4 book ai didi

java - Hibernate 无效的列名

转载 作者:行者123 更新时间:2023-11-29 07:10:38 25 4
gpt4 key购买 nike

我正在尝试做一个简单的选择,但出现了这个错误:

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

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

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

我的实体映射或 View 有问题吗?

最佳答案

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

Mixing annotations in both fields and methods should be avoided. Hibernate will guess the access type from the position of @Id or @EmbeddedId.

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

所以在这里你可以写:

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

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

关于java - Hibernate 无效的列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14214520/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com