简体   繁体   English

通过Hibernate使用MySQL:org.hibernate.exception.SQLGrammarException

[英]using MySQL by Hibernate : org.hibernate.exception.SQLGrammarException

I am using Hibernate to access MySQL database for my java application. 我正在使用Hibernate访问Java应用程序的MySQL数据库。 I configured Hibernate using this instructions .Now I want to have query to my database; 我使用此说明配置了Hibernate。现在,我想查询数据库。 using this code: 使用此代码:

java.util.List categList = null;
SessionFactory sessionFacoryt ;
sessionFacoryt = NewHibernateUtil.getSessionFactory();
org.hibernate.classic.Session hiberanateSession = sessionFacoryt.getCurrentSession();
String HQL = "select r from RssCategory r";
Transaction transaction = hiberanateSession.beginTransaction();                
org.hibernate.Query q = hiberanateSession.createQuery(HQL);

// here is the Exception cause line  
categList = q.list();

as you see because of the last line; 如您所见,由于最后一行; I see this Exception: 我看到此异常:

Oct 23, 2011 3:20:25 PM org.hibernate.util.JDBCExceptionReporter
logExceptions WARNING: SQL Error: 1064, SQLState: 42000 Oct 23, 2011
3:20:25 PM org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '-IPTV-EPG.rss_category rsscategor0_' at line 1
org.hibernate.exception.SQLGrammarException: could not execute query at
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67) at
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.doList(Loader.java:2223)     at
org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)  at
org.hibernate.loader.Loader.list(Loader.java:2099)  at
org.hibernate.hql.classic.QueryTranslatorImpl.list(QueryTranslatorImpl.java:912) at
org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172) at
org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)  at
org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)    at
RSS.connectHtmlandleRSS.addTODB(connectHtmlandleRSS.java:206)   at
RSS.connectHtmlandleRSS.access$000(connectHtmlandleRSS.java:37)     at
RSS.connectHtmlandleRSS$1.run(connectHtmlandleRSS.java:163) Caused by:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an
error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near
'-IPTV-EPG.rss_category rsscategor0_' at line 1     at
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at
java.lang.reflect.Constructor.newInstance(Constructor.java:513) at
com.mysql.jdbc.Util.handleNewInstance(Util.java:409)    at
com.mysql.jdbc.Util.getInstance(Util.java:384)  at
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)  at
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3566)  at
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3498)  at
com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)   at
com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2113)    at
com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2568)     at
com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2113) at
com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2275) at
org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:186) at 
org.hibernate.loader.Loader.getResultSet(Loader.java:1787)  at
org.hibernate.loader.Loader.doQuery(Loader.java:674)    at
org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236) at
org.hibernate.loader.Loader.doList(Loader.java:2220)

can anyone guide me for the cause of this exception ? 谁能指导我这个异常的原因? thanks. 谢谢。

here is mapping generated for RssCategory; 这是为RssCategory生成的映射; This class was generated to this package sakila.entity. 此类已生成到此包sakila.entity。

<hibernate-mapping>
    <class name="sakila.entity.RssCategory" table="rss_category" catalog="SAMIM-IPTV-EPG">
        <id name="catId" type="int">
            <column name="cat_id" />
            <generator class="assigned" />
        </id>
        <property name="catName" type="string">
            <column name="cat_name" length="45" not-null="true" unique="true" />
        </property>
        <set name="rssNewses" inverse="true" table="rss_to_cat">
            <key>
                <column name="cat_id" not-null="true" />
            </key>
            <many-to-many entity-name="sakila.entity.RssNews">
                <column name="rss_id" not-null="true" />
            </many-to-many>
        </set>
    </class> </hibernate-mapping>

and here is the SQL code generated for rss_category: 这是为rss_category生成的SQL代码:

CREATE TABLE `rss_category` (
  `cat_id` int(11) NOT NULL,
  `cat_name` varchar(45) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`cat_id`),
  UNIQUE KEY `cat_name_UNIQUE` (`cat_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

and the HQL query generated by Hibernate is: Hibernate生成的HQL查询为:

Hibernate: select rsscategor0_.cat_id as cat1_4_, rsscategor0_.cat_name as cat2_4_ from SAMIM-IPTV-EPG.rss_category rsscategor0_

The problem is certainly caused by the dashes in the catalog name. 该问题当然是由目录名称中的破折号引起的。 Try to change the name of the catalog to something else, without dashes. 尝试将目录名称更改为其他名称,不要使用破折号。

I had some similar problems because I was not using the correct dialect (MySQL Diallect). 我有一些类似的问题,因为我没有使用正确的方言(MySQL Diallect)。 Please try to configure the hibernate.dialect to org.hibernate.dialect.MySQL5Dialect. 请尝试将hibernate.dialect配置为org.hibernate.dialect.MySQL5Dialect。

Use the the below HQL, 使用下面的HQL,

String HQL = "from RssCategory"

If you want all the columns as a list of Object[] (or partial list of items) use the below, 如果您希望所有列都作为Object []列表(或部分项目列表),请使用以下内容,

String HQL = "select r.prop1, r.prop2 ... from RssCategory r"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.hibernate.exception.SQLGrammarException:无法插入,使用mysql休眠 - org.hibernate.exception.SQLGrammarException: could not insert, hibernate with mysql 带有MySQL的Hibernate错误org.hibernate.exception.SQLGrammarException - Hibernate error org.hibernate.exception.SQLGrammarException with MySQL Spring Hibernate:org.hibernate.exception.SQLGrammarException - Spring hibernate: org.hibernate.exception.SQLGrammarException JPA 异常:org.hibernate.exception.SQLGrammarException - JPA Exception: org.hibernate.exception.SQLGrammarException 获取异常:org.hibernate.exception.sqlgrammarexception - Getting Exception : org.hibernate.exception.sqlgrammarexception 获取异常org.hibernate.exception.SQLGrammarException: - getting exception org.hibernate.exception.SQLGrammarException: InvalidDataAccessResourceUsageException:org.hibernate.exception.SQLGrammarException: - InvalidDataAccessResourceUsageException: org.hibernate.exception.SQLGrammarException: org.hibernate.exception.SQLGrammarException:休眠迁移后,无法使用Hibernate和SQLServer绑定多部分标识符 - org.hibernate.exception.SQLGrammarException: The multi part identifier can not be bound using Hibernate and SQLServer After a hibernate migration org.hibernate.exception.SQLGrammarException:休眠不会加入列 - org.hibernate.exception.SQLGrammarException: hibernate will not join column 嵌套的异常是org.hibernate.exception.SQLGrammarException:无法执行查询 - nested exception is org.hibernate.exception.SQLGrammarException: could not execute query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM