Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Query returs empty list but getting results in TOAD

 
sudhakar Tadepalli
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FYI, I tried inserting and it workd fine.
 
sudhakar Tadepalli
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
sudhakar Tadepalli
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to find out whether caching is on/off. I used middlegen to create mapping files and pojo's.

Sudhakar
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic