Hibernate似乎从HQL查询创建错误的SQL查询

时间:2014-09-17 08:01:02

标签: java sql hibernate hql

我尝试使用函数扩展我的数据库程序以执行任何hql查询。我几乎完成了它,但我在下面的过程中收到错误。

select p.reviews, p.title from Product p

这个hql查询在sql查询中转换如下:

Hibernate: select . as col_0_0_, product0_.title as col_1_0_, reviews1_.account_number as account_1_2_, reviews1_.product_id as product_2_2_, reviews1_.points as points3_2_, reviews1_.review as review4_2_ from dbprak12.view_product_meta product0_ inner join dbprak12.view_customer_evaluates reviews1_ on product0_.product_id=reviews1_.product_id

正如你可以明显看到的那样,sql状态代码42601会抛出一个错误。因为" 。 as col_0_0 _..."不正确的语法。但我不明白为什么hibernate会创建这个SQL查询。

我使用以下两个xml映射:

Product.hbm.xml

<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="media.definitions.Product" table="dbprak12.view_product_meta">
  <id name="asin" column="product_id">
     <generator class="native" />
  </id>
  <property name="title" column="title" />
  <property name="avgRating" column="rating" />
  <property name="salesRank" column="sales_rank" />
  <property name="picUrl" column="picture" />
  <set name="reviews" cascade="all" inverse="true" lazy="true">
      <key column="product_id" />
      <one-to-many class="media.definitions.Review" />
   </set>
  <set name="categories" table="dbprak12.view_product_in_category" inverse="false" lazy="true" fetch="select" cascade="all">
     <key column="product_id" />
     <many-to-many column="category_id" class="media.definitions.Category" />
  </set>
  <joined-subclass name="media.definitions.Book" table="dbprak12.view_book">
     <key column="product_id" />
     <property name="isbn" column="isbn" />
     <property name="publishers" column="publisher" />
     <property name="pubDate" column="publication" />
     <property name="pages" column="pages" />
  </joined-subclass>
  <joined-subclass name="media.definitions.DVD" table="dbprak12.view_dvd">
     <key column="product_id" />
     <property name="format" column="fformat" />
     <property name="regionCode" column="region_code" />
     <property name="runningTime" column="running_time" />
  </joined-subclass>
  <joined-subclass name="media.definitions.Music" table="dbprak12.view_cd">
     <key column="product_id" />
     <property name="labels" column="label" />
     <property name="releaseDate" column="release_date" />
  </joined-subclass>
</class>

</hibernate-mapping>

Review.hbm.xml

<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="media.definitions.Review" table="dbprak12.view_customer_evaluates">
 <composite-id>
    <key-property name="accountNumber" column="account_number" />
    <key-many-to-one name="product" class="media.definitions.Product" lazy="false">
       <column name="product_id"></column>
    </key-many-to-one>
 </composite-id>
 <property name="rating" column="points" />
 <property name="content" column="review" />
</class>

</hibernate-mapping>

编辑:我的Hibernate配置文件: 的hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>

    <session-factory>
        <property name="hibernate.connection.pool_size">10</property>
        <property name="show_sql">true</property>
        <property name="hibernate.current_session_context_class">thread</property>

        <mapping resource="Product.hbm.xml"/>
        <mapping resource="Category.hbm.xml"/>
        <mapping resource="Review.hbm.xml"/>
        <mapping resource="Person.hbm.xml"/>
        <mapping resource="Offer.hbm.xml"/>

    </session-factory>
</hibernate-configuration>

db2Module.properties

#
# Database module to load
#
middleware.module:media.mediadbimpl.DB2Module

#
# Database properties
#

db.jdbc.dialect:org.hibernate.dialect.DB2Dialect
db.jdbc.driver:com.ibm.db2.jcc.DB2Driver
db.jdbc.url:jdbc:db2://anyurl.de:50001/datasource
db.user:dbuser
db.password:.dbpass.

1 个答案:

答案 0 :(得分:0)

注意:这不是经过研究的答案,因此可能不正确。

我认为你的错误可能就在这里:

<column name="product_id"></column>

我认为这应该是

<column name="product_id" />

标签中的正文内容(甚至0个字符)与不具有正文内容的情况不同。 (相当于“”和字符串为null)

文档说您可以在密钥

中使用嵌套列元素
<key-many-to-one name="product" class="media.definitions.Product" lazy="false">
   <column name="product_id" />
</key-many-to-one>

但你似乎没有使用它,所以你可以这样做:

<key-many-to-one name="product" class="media.definitions.Product" lazy="false" column="product_id" />

子标签通常用于非标准列。

无论如何要试一试