我在使用 Hibernate 时遇到问题:
我有一个到数据库的连接,并生成了用于执行 HQL 查询的类和 hmb.xml 文件。但是,当我打字时:
from Department
并且Show SQL
参数设置为true
,我可以看到涉及的 SQL 查询:
select from
但我不明白查询的另一部分在哪里。
我不知道,但也许这是运行查询后出错的原因:
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:2545)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276)
at org.hibernate.loader.Loader.list(Loader.java:2271)
at org.hibernate.hql.classic.QueryTranslatorImpl.list(QueryTranslatorImpl.java:940)
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)
Caused by: java.sql.SQLSyntaxErrorException: ORA-00936: missing expression
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:447)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:396)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:951)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:513)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:227)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:531)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:208)
at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:886)
at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1175)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1296)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3613)
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3657)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1495)
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)
... 8 more
配置文件是这样的:
<?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.OracleDialect</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
<property name="hibernate.connection.username">admin</property>
<property name="hibernate.connection.password">admin</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
<mapping resource="catalog/Coworker.hbm.xml"/>
<mapping resource="catalog/Unit.hbm.xml"/>
<mapping resource="catalog/Department.hbm.xml"/>
<mapping resource="catalog/Post.hbm.xml"/>
</session-factory>
</hibernate-configuration>
文件内容hbm.xml
:
<?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">
<!-- Generated Nov 26, 2013 3:51:26 PM by Hibernate Tools 3.6.0 -->
<hibernate-mapping>
<class name="catalog.Department" table="DEPARTMENT" schema="ADMIN">
<id name="idDepartment" type="big_decimal">
<column name="ID_DEPARTMENT" precision="22" scale="0" />
<generator class="assigned" />
</id>
<many-to-one name="department" class="catalog.Department" fetch="select">
<column name="PARENT_ID" precision="22" scale="0" not-null="true" />
</many-to-one>
<property name="nameDepartment" type="string">
<column name="NAME_DEPARTMENT" length="40" not-null="true" />
</property>
<set name="posts" table="POST" inverse="true" lazy="true" fetch="select">
<key>
<column name="ID_DEPARTMENT" precision="22" scale="0" not-null="true" />
</key>
<one-to-many class="catalog.Post" />
</set>
<set name="departments" table="DEPARTMENT" inverse="true" lazy="true" fetch="select">
<key>
<column name="PARENT_ID" precision="22" scale="0" not-null="true" />
</key>
<one-to-many class="catalog.Department" />
</set>
</class>
</hibernate-mapping>
请帮助我进入 Hibernate 学习的下一部分!