1

The following query doesn't return any rows List remedies = session.createQuery("from Remedy").list(); No errors, but nothing in the remedies list and there are rows in the table.

What I have is 2 tables: remedy and remedyTranslation I have the following map files: remedy.hbm.xml is below:

<?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 is below:

<?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 Answers 3

2

If what andrew posted doesn't work, turn your hibernate logging on to show sql statements and see if you are actually making any sql requests. If there are no requests, the problem lies in your configuration, if there is a bad request then you'll know it's in your class.

Sign up to request clarification or add additional context in comments.

7 Comments

I did as you suggested and it produced this output: select remedy0_.REMEDY_ID as REMEDY1_0_, remedy0_.NAME as NAME0_ from REMEDY remedy0_
In this app of mine, I'm just doing a security check. A basic table with a username column and an authorities column. It returns 'Hibernate: select this_.username as username2_0_, this_.authority as authority2_0_ from authorities this_' and it is working as intended. It seems the hibernate is doing what it is supposed to.
I also get '2011-05-24 15:47:40 StringType [TRACE] returning 'admin' as column: username2_0_' and '2011-05-24 15:47:41 StringType [TRACE] returning 'ROLE_ADMIN' as column: authority2_0_' notifying me that it has loaded the results.
I took the SQL and pasted it into mysql and ran it and it returned the correct results. So it must be to do with the mapping??
You wouldn't believe it, it was the wrong database name in the configuration file. Unbelievable, I refactored the database and left the old one in the cfg config file. It didn't have the same field names and so nothing got mapped properly.
|
1

In my case, Hibernate didn't throw any exception, didn't show executed SQL and results were empty. It happened because of incorrect package in <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>

Comments

0

In my case, the variable used in the POJO class was causing the issue . I had an int variable and the default value was set as null in database. Changing the default value from null to 0 resolved the issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.