JPA 2.0,如何修复以下2个错误
我实际上正在学习 JPA2.0 并且我遇到了以下错误,考虑一下我使用 eclipse 作为 IDE。
Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1215)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1148)
at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:307)
at com.banking.core.persistence.utils.EntityManagerTest.main(EntityManagerTest.java:38)
Caused by: org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.loader.Loader.doList(Loader.java:2536)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276)
at org.hibernate.loader.Loader.list(Loader.java:2271)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:452)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1268)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:274)
... 1 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'loanaccoun0_.id' in 'field list'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3597)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3529)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1990)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2151)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2625)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2119)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2281)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1953)
at org.hibernate.loader.Loader.doQuery(Loader.java:802)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274)
at org.hibernate.loader.Loader.doList(Loader.java:2533)
... 9 more
这是我现在正在使用的查询...
LoanAccount.class
@Entity
@Table(schema="BANKING_CORE", name="NUMBER_OF_PAYMENTS")
@DiscriminatorValue("LA")
@NamedQuery(name = "LoanAccount.findByNumberofPayments", query = "SELECT lc from LoanAccount lc WHERE lc.numberOfPayments = :number_of_payments")
这是查询本身
entitymanger.class
LoanAccount lc = (LoanAccount) em.createNamedQuery("LoanAccount.findByNumberofPayments").setParameter("number_of_payments", 25).getSingleResult();
System.out.println(lc.getNumberOfPayments()+" "+lc.getCutOffDate());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,似乎您更新了实体,但表尚未重新创建。确保您的休眠配置中的设置,将
hbm2dll.auto
设置为update
或create-drop
最后将查询更改为
FROM LoanAccount WHERE numberOfPayment = :number_of_ payments
还要在 hibernate 配置中激活 show_sql 并查看生成的 sql 是什么。
First off all, seems like you updated your entity but the tables have no been recreated. make sure the setting in your hibernate config, set
hbm2dll.auto
is eitherupdate
orcreate-drop
Finally change the query to
FROM LoanAccount WHERE numberOfPayment = :number_of_payments
Also activate show_sql in your hibernate config and see what the produced sql is.