-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Hibernate query not returning results
PostPosted: Thu Mar 29, 2007 10:52 am 
Newbie

Joined: Thu Mar 29, 2007 10:41 am
Posts: 6
I have a simple Hibernate code. The query does not work in the sense that even though the table has entries,nothing is returned. Any help is appreciated. I am using Hibernate 3.2 and Cloudscape 10.1

Hibernate.cfg.xml
------------------------


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.apache.derby.jdbc.EmbeddedDriver</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.url">jdbc:derby:JPracticeDB;create=true</property>
<property name="hibernate.connection.username"></property>
<property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</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">create</property>

<mapping resource="beans/Category.hbm.xml"/>
<mapping resource="beans/Questions.hbm.xml"/>
</session-factory>
</hibernate-configuration>


Questions.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="app.proj.practice.beans.Questions" table="QUESTIONS">
<id name="questionId" column="QUESTION_ID">
<generator class="native"/>
</id>
<property name="questionDesc" column="QUESTION_DESC"/>
<property name="questionType" column="QUESTION_TYPE"/>
<!--
<set name="categoryId" table="CATEGORY_QUESTIONS">
<key column="QUESTION_ID"/>
<many-to-many column="CATEGORY_ID" class="app.proj.practice.beans.Category"/>
</set>
-->
</class>
</hibernate-mapping>


Category.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="app.proj.practice.beans.Category" table="CATEGORY">
<id name="categoryId" column="CATEGORY_ID">
<generator class="native"/>
</id>
<property name="categoryName" column="CATEGORY_NAME"/>
<!--
<set name="questions" table="CATEGORY_QUESTIONS">
<key column="CATEGORY_ID"/>
<many-to-many column="QUESTION_ID" unique="true" class="app.proj.practice.beans.Questions"/>
</set>
-->
</class>
</hibernate-mapping>


Query code
--------------

ackage app.proj.practice.beans;

import java.util.List;

import org.hibernate.Session;

public class PracticeManager {


public List listQuestions()
{

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List result = session.createQuery("from Category").list();
session.getTransaction().commit();
return result;

}
/**
* @param args
*/
public static void main(String[] args)
{
PracticeManager pracMgr = new PracticeManager();
List questions = pracMgr.listQuestions();
int size = questions.size();
System.out.println("Size:" + size);
for ( int i = 0; i < size; i++ )
{
Questions question = (Questions)questions.get(i);
System.out.println("Question Id:" + question.getQuestionId());
System.out.println("Question Desc:" + question.getQuestionDesc());
System.out.println("-------End of Record-------------------");
}

}

}


output:
-----------
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select category0_.CATEGORY_ID as CATEGORY1_0_, category0_.CATEGORY_NAME as CATEGORY2_0_ from CATEGORY category0_
Size:0


Thanks,


Top
 Profile  
 
 Post subject: Additional information.
PostPosted: Thu Mar 29, 2007 12:23 pm 
Newbie

Joined: Thu Mar 29, 2007 10:41 am
Posts: 6
Here's some more information:

I load the database using a loader that has a sql statements to load the category and questions table using the ij tool.
If I go to the cloudscape prompt and do a select * from questions table.
ij> select * from questions;

1016 |What is question3?
|MULTIPLE

1017 |What is question4?
|SINGLE

1018 |What is question5?
|SINGLE
I get the output above, which is what I loaded into the database.

But after issuing a hibernate query, it has the effect of removing/deleting all the above entries and returning a zero item list.

PracticeManager pracMgr = new PracticeManager();
List questions = pracMgr.listQuestions();
int size = questions.size()

But now if I programmatically add.

PracticeManager pracMgr = new PracticeManager();
createAndStoreCategory(2007,"NewCategory");
createAndStoreQuestions("desc1","type1");
createAndStoreQuestions("desc2","type2");
createAndStoreQuestions("desc3","type3");
List questions = pracMgr.listQuestions();
int size = questions.size();

the query works, but only once. If I start the program again, the hibernate query process has removed all the entries from the table.
Anybody know why this happens?


Top
 Profile  
 
 Post subject: Additional information.
PostPosted: Thu Mar 29, 2007 12:25 pm 
Newbie

Joined: Thu Mar 29, 2007 10:41 am
Posts: 6
Here's some more information:

I load the database using a loader that has a sql statements to load the category and questions table using the ij tool.
If I go to the cloudscape prompt and do a select * from questions table.
ij> select * from questions;

1016 |What is question3?
|MULTIPLE

1017 |What is question4?
|SINGLE

1018 |What is question5?
|SINGLE
I get the output above, which is what I loaded into the database.

But after issuing a hibernate query, it has the effect of removing/deleting all the above entries and returning a zero item list.

PracticeManager pracMgr = new PracticeManager();
List questions = pracMgr.listQuestions();
int size = questions.size()

But now if I programmatically add.

PracticeManager pracMgr = new PracticeManager();
createAndStoreCategory(2007,"NewCategory");
createAndStoreQuestions("desc1","type1");
createAndStoreQuestions("desc2","type2");
createAndStoreQuestions("desc3","type3");
List questions = pracMgr.listQuestions();
int size = questions.size();

the query works, but only once. If I start the program again, the hibernate query process has removed all the entries from the table.
Anybody know why this happens?


Top
 Profile  
 
 Post subject: Hibernate Query deleting entries from the table
PostPosted: Thu Mar 29, 2007 8:54 pm 
Newbie

Joined: Thu Mar 29, 2007 10:41 am
Posts: 6
This is related to my original posting. I am seeing that the entries from the table are deleted/removed when a Hibernate Query is made on a Cloudscape database. If I do a select * on the table on the sql prompt BEFORE running hibernate program, I get the results correctly. It appears that the Hibernate query is deleting/removing the entries. I am using Hibernate 3.2


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 30, 2007 3:04 am 
Newbie

Joined: Mon Mar 26, 2007 3:33 am
Posts: 18
you comment this entry. it should work after commenting this
in hibernate.cfg.xml.


<property name="hbm2ddl.auto">create</property>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 30, 2007 4:24 am 
Newbie

Joined: Sat Mar 24, 2007 8:14 am
Posts: 15
jst make ur this entry:

<property name="hbm2ddl.auto">create</property>

to:


<property name="hbm2ddl.auto">update</property>

Regards,

Dont forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 30, 2007 8:50 am 
Newbie

Joined: Thu Mar 29, 2007 10:41 am
Posts: 6
Thanks for the answers. It always appears ovious once you get the answer.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.