1

我想使用休眠进行选择 * 查询。我的数据库只有一个 ID 和名称列。我使用以下标准:

  try {
  SessionFactory sessFact = new Configuration()
  .configure("hibernate.cfg.xml").buildSessionFactory();
  Session sess = sessFact.openSession();
  Transaction tran = sess.beginTransaction();

List<testModel> ans = sess.createCriteria(testModel.class).list();
}

可悲的是,我不断收到无法执行的错误:org.hibernate.exception.GenericJDBCException: could not execute query

我的模型文件是:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!--This DTD should also be in the hbm.xml file-->
<hibernate-mapping package="test.model">
  <class name="testModel"
     table="test">

<id name="name_id" type="string" column="name_id">
  <generator class="native">
  </generator>
</id>
<property name="name"
          type="string"
          column="name" />

顺便说一句,当我将查询添加到我的数据库时,它运行良好。

生成的 Stacktrace 是:

    [] org.hibernate.exception.GenericJDBCException: could not execute query
[]  at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:140)
[]  at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:128)
[]  at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
[]  at org.hibernate.loader.Loader.doList(Loader.java:2545)
[]  at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276)
[]  at org.hibernate.loader.Loader.list(Loader.java:2271)
[]  at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:119)
[]  at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1716)
[]  at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)
[]  at com.amazon.rhip_dbv3.getHostClassHostsActivity.getHostClassHosts(getHostClassHostsActivity.java:47)
[]  at com.amazon.rhip_dbv3.getHostClassHostsActivity$$FastClassByCGLIB$$296fc5b2.invoke(<generated>)
[]  at org.springframework.internal.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
[]  at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:688)
[]  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.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:621)

[]  at org.apache.catalina.valves.SSLValve.invoke(SSLValve.java:113)
[]  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
[]  at org.apache.coyote.http11.Http11NioProcessor.process(Http11NioProcessor.java:894)
[]  at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:728)
[]  at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:2108)
[]  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
[]  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
[]  at java.lang.Thread.run(Thread.java:722)
[] Caused by: java.sql.SQLException: No database selected
[]  at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
[]  at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
[]  at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
[]  at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503)
[]  at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664)
[]  at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2815)
[]  at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
[]  at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2322)
[]  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:2542)
[]  ... 37 more

虽然它显示的查询是:

 select this_.name_id as name1_2_0_, this_.name as name2_2_0_,  from test this_

好的,我解决了。只是有错误的数据库表名。我恨我自己 :(

4

1 回答 1

3

您发布的内容与代码一致。如果您只需要列出表中的项目,则可能不需要事务。

在堆栈跟踪中,您可以看到:

原因:java.sql.SQLException:未选择数据库。

检查连接字符串...

于 2013-06-04T22:25:48.713 回答