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,
|