【发布时间】:2012-09-15 20:08:16
【问题描述】:
大家好,我正在尝试使用 HQL 编辑器从我的数据库中检索数据进行查询。我按照示例“在 Java Swing 应用程序中使用 Hibernate”中的步骤进行操作。我的配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mobilecarriers?zeroDateTimeBehavior=convertToNull</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">######</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
<property name="hibernate.c3p0.min_size">1</property>
<property name="hibernate.c3p0.max_size">100</property>
<property name="hibernate.c3p0.timeout">0</property>
<property name="hibernate.c3p0.max_statements">0</property>
<property name="hibernate.c3p0.idle_test_period">0</property>
<mapping resource="mobilecarriers/entity/National.hbm.xml"/>
</session-factory>
</hibernate-configuration>
映射:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 23 Sep 2012 11:26:47 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="mobilecarriers.entity.National" table="national" catalog="mobilecarriers">
<id name="id" type="java.lang.Short">
<column name="id" />
<generator class="identity" />
</id>
<property name="dominantSecondLanguage" type="string">
<column name="DominantSecondLanguage" length="25" />
</property>
</class>
</hibernate-mapping>
使用这个:from national 或 from National 我得到以下错误:
org.hibernate.exception.SQLGrammarException:无法执行查询 在 org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67) 在 org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) 在 org.hibernate.loader.Loader.doList(Loader.java:2223) 在 org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104) 在 org.hibernate.loader.Loader.list(Loader.java:2099) 在 org.hibernate.hql.classic.QueryTranslatorImpl.list(QueryTranslatorImpl.java:912) 在 org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172) 在 org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121) 在 org.hibernate.impl.QueryImpl.list(QueryImpl.java:79) 原因: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 你有一个 SQL 语法错误;检查与您对应的手册 MySQL 服务器版本,可在“从限制 100”附近使用正确的语法 在第 1 行 sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法) 在 sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) 在 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 在 java.lang.reflect.Constructor.newInstance(Constructor.java:525) 在 com.mysql.jdbc.Util.handleNewInstance(Util.java:411) 在 com.mysql.jdbc.Util.getInstance(Util.java:386) 在 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052) 在 com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609) 在 com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541) 在 com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002) 在 com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163) 在 com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2624) 在 com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2127) 在 com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2293) 在 org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:186) 在 org.hibernate.loader.Loader.getResultSet(Loader.java:1787) 在 org.hibernate.loader.Loader.doQuery(Loader.java:674) 在 org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236) 在 org.hibernate.loader.Loader.doList(Loader.java:2220) ... 8 更多
【问题讨论】:
-
您的查询语句根据日志有错误,请发布/检查您的查询
-
您忘记了映射。请更新您的问题。
-
我从 HQL 编辑器查询并尝试了“来自国家”和“来自国家”
-
@Satya 我从 HQL 编辑器查询并尝试了“来自国家”和“来自国家”。
-
@bussambule,Hibernat 应该打印发送到 MySQL 的查询。请尝试在您的日志中找到它。我也会暂时删除 c3po 设置。