Hibernate简单查询但没有返回任何行

时间:2011-05-24 05:11:24

标签: hibernate hql

以下查询不返回任何行 列表补救措施= session.createQuery(“来自Remedy”)。list(); 没有错误,但补救措施列表中没有任何内容,表格中有行。

我所拥有的是2个表:补救和补救翻译 我有以下地图文件: remedy.hbm.xml如下:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="com.audiClave.Service.Remedy" table="REMEDY">
    <id column="REMEDY_ID" name="remedy_id" type="int">
      <generator class="native"/>
    </id>
    <property generated="never" lazy="false" name="name">
      <column name="NAME"/>
    </property>
    <set 
        name="remedyTranslations" 
        lazy="true"
        inverse="true"
        cascade="save-update">
  <key column="REMEDY_ID"/>
  <one-to-many class="com.audiClave.Service.RemedyTranslation"/>
    </set>
  </class>
</hibernate-mapping>

remedyTranslation.hbm.xml如下:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.audiClave.Service.RemedyTranslation" table="REMEDYTRANSLATION">
    <id column="REMEDYTRANSLATION_ID" name="remedyTranslation_id" type="int">
        <generator class="native"/>
    </id>
    <many-to-one 
        name="remedy_id" 
        column="REMEDY_ID" 
        class="com.audiClave.Service.Remedy" 
        not-null="true"
        lazy="false" />
    <property generated="never" lazy="false" name="name">
        <column name="NAME"/>
    </property>
    <property generated="never" lazy="false" name="language">
        <column name="LANGUAGE"/>
    </property>
</class>
</hibernate-mapping>

hibernate.cfg.xml中:

<?xml version='1.0' encoding='utf-8'?>
<!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>
  <!-- Database connection settings -->
  <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="connection.url">jdbc:mysql://localhost/userdata</property>
  <property name="connection.username">mysql</property>
  <property name="connection.password">???????</property>

  <!-- JDBC connection pool (use the built-in) -->
  <property name="connection.pool_size">1</property>

  <!-- SQL dialect -->
  <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

  <!-- Enable Hibernate's automatic session context management -->
  <property name="current_session_context_class">thread</property>

  <!-- Disable the second-level cache -->
  <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

  <!-- Echo all executed SQL to stdout -->
  <property name="show_sql">true</property>

  <!-- Drop and re-create the database schema on startup -->
  <property name="hbm2ddl.auto">update</property>

  <!-- Mapping files -->
  <mapping resource="remedy.hbm.xml"/>
  <mapping resource="remedyTranslation.hbm.xml"/>

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

3 个答案:

答案 0 :(得分:2)

如果安德鲁发布的内容不起作用,请将您的休眠日志记录设置为显示sql语句,并查看您是否实际发出了任何sql请求。如果没有请求,问题在于您的配置,如果有错误的请求,那么您将知道它在您的班级。

答案 1 :(得分:1)

在我的情况下,Hibernate没有抛出任何异常,没有显示已执行的SQL并且结果为空。这是因为<property name="packagesToScan">中的包不正确:

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="packagesToScan">
        <list>
            <value>com.example.entity</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.enable_lazy_load_no_trans">true</prop>
        </props>
    </property>
</bean>

答案 2 :(得分:0)

就我而言,POJO类中使用的变量导致了问题。我有一个int变量,默认值在数据库中设置为null。将默认值从null更改为0解决了问题。