aspose file tools*
The moose likes Object Relational Mapping and the fly likes Query returs  empty list but getting results in TOAD Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Query returs  empty list but getting results in TOAD" Watch "Query returs  empty list but getting results in TOAD" New topic
Author

Query returs empty list but getting results in TOAD

sudhakar Tadepalli
Ranch Hand

Joined: Dec 27, 2001
Posts: 130
I am trying to retrive all record from a table. The generated sql I copied and excuted in TOAD and got two record. But my Qury object have empty list.

Can some one help me what could be the problem ?

Here is the code :

try {
Query q =
getSession().createQuery("from Templatedetail td");
list = q.list();

} catch (HibernateException e) {
e.printStackTrace();
}
return list;

Generated Qury :
select templatede0_.TEMPLATEID as TEMPLATEID, templatede0_.TEMPLATETYPE as TEMPLATE2_, templatede0_.DETAILDESC as DETAILDESC, templatede0_.OWNERCODE as OWNERCODE from TEMPLATEDETAILS templatede0_

This qury retrieving two record when I executed in TOAD. But my list from Query object is empty.

Mapping File:

<class name="com.test.model.Templatedetail" table="TEMPLATEDETAILS">
<meta attribute="class-description" inherit="false">@hibernate.class table="TEMPLATEDETAILS"</meta>

<id name="templateid" type="java.lang.Long" column="TEMPLATEID">
<meta attribute="field-description">
@hibernate.id generator-class="assigned" type="java.lang.Long" column="TEMPLATEID"

</meta>
<generator class="assigned" />
</id>

<property name="templatetype" type="java.lang.Long" column="TEMPLATETYPE" length="11">
<meta attribute="field-description">@hibernate.property column="TEMPLATETYPE" length="11"</meta>
</property>
<property name="detaildesc" type="java.lang.String" column="DETAILDESC" length="255">
<meta attribute="field-description">@hibernate.property column="DETAILDESC" length="255"</meta>
</property>
<property name="ownercode" type="java.lang.Long" column="OWNERCODE" length="11">
<meta attribute="field-description">@hibernate.property column="OWNERCODE" length="11"</meta>
</property>

<!-- Associations -->


</class>


Used Middlegen to generate Mapping files and beans. Table have one primary hey called templateid.

Thanks
Sudhakar
sudhakar Tadepalli
Ranch Hand

Joined: Dec 27, 2001
Posts: 130
FYI, I tried inserting and it workd fine.
sudhakar Tadepalli
Ranch Hand

Joined: Dec 27, 2001
Posts: 130
I solved the puzzle. I have manually insterted records in the database and when tried retrieving got empty list, I tried insterting one record through save() method and tried searching , now I got one record back.

After I inserted records manually I ran commit; command also but not sure why hibernate not able to query the records which are manually inserted in db and only able to retrieve which are programatically inserted.

Thanks
Sudhakar
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Maybe you have caching on and it runs the query on the cached set which had no records even though the database now did..

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
sudhakar Tadepalli
Ranch Hand

Joined: Dec 27, 2001
Posts: 130
How to find out whether caching is on/off. I used middlegen to create mapping files and pojo's.

Sudhakar
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Originally posted by sudhakar Tadepalli:
How to find out whether caching is on/off. I used middlegen to create mapping files and pojo's.

Sudhakar


Look at your mapping files. If you've added a <cache /> element to your class then you are asking Hibernate to use the second level cache. The first level cache is (of course) always used. It shouldn't be that though. Is the only difference from your first failed attempt and your second successful one that you forced a commit after running your insert statement in Toad? Because, if I remember right, Toad is configured by default not to commit after every statement.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Query returs empty list but getting results in TOAD
 
Similar Threads
Unable to find Data source
Performance issues with huge data using hiberante
Mapping For CompositeID
Integrity constraint violation: for Hibernate Insert
Use of set tag in Hibernate mapping file for foreign key