java 休眠无效的列名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14214520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate invalid column name
提问by brevleq
I'm trying to do a simple select, and I'm getting this error:
我正在尝试做一个简单的选择,但出现此错误:
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.access1(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
This is my entity. As you can see it has only a column (its a simple 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;
}
}
This is the view this entity is vinculated:
这是该实体的观点:
SELECT CAST(MONTH(DATAFISCAL) AS VARCHAR(2)) + '_' + CAST(YEAR(DATAFISCAL) AS VARCHAR(4)) AS MES_ANO
FROM dbo.MESFISCAL
WHERE (Status = N'AB')
Is something wrong in my entity mapping or in the view?
我的实体映射或视图有问题吗?
回答by Kevin Bowersox
Specify the column name on the field using the @Column
annotation. This will use field access for entity, instead of mixing file and setter annotations.
使用@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;
}
回答by Christophe L
From http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/:
来自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.
应避免在字段和方法中混合注释。Hibernate 会根据@Id 或@EmbeddedId 的位置猜测访问类型。
If you put the @Id annotation on a field you need to put your other JPA annotations (@Column etc.) on fields (rather than getters). You cannot mix annotations on getters / fields.
如果将 @Id 注释放在字段上,则需要将其他 JPA 注释(@Column 等)放在字段上(而不是 getter)。您不能在 getter/字段上混合注释。
So here you could write:
所以在这里你可以写:
@Id
@Column(name="MES_ANO", length = 7)
private String mesAno;
Note that since the column is already an @Id, your nullable / insertable / updatable declarations may not make sense.
请注意,由于该列已经是 @Id,因此您的可空/可插入/可更新声明可能没有意义。