Monday, January 16, 2006

Hibernate error when listing data

Ok this is an error I'm currently having with hibernate when loading data from the DB.
I'm starting to learn hibernate .
I have made a test caseI finally made this work, even within a struts project.
Now I thought I had it all, I found a problem in my code.
I have made the database table in postgreSQL and made a java persistent class, I also made a mapping file.
I tested this by storing some data in my DB which worked perfect so I guess this means that all my files for hibernate are ok.
Now when I want to print a list from the db, I get the following

error :org.hibernate.hql.ast.QuerySyntaxException: tbl_tarifftype is not mapped.
[from tbl_tarifftype]at.....Caused by: tbl_tarifftype is not mapped
....
22 more
I used the code from several examples I found on the www which should work but in my case they don't.
This is the code to show the list:
public void listTariffType() {log.info("list TariffType");
Session session = HibernateSessionFactory.currentSession();
Transaction tx = session.beginTransaction();
List TariffTypeList = session.createQuery("from tbl_tarifftype").list();
for (Iterator iter = TariffTypeList.iterator(); iter.hasNext() {
TariffTypeValue TariffType = (TariffTypeValue) iter.next();
System.out.println("Id " + TariffType.getTariffTypeID() + " Name " + TariffType.getName());
}
tx.commit();
HibernateSessionFactory.closeSession();
}

Answer to the problem:
change List TariffTypeList = session.createQuery("from tbl_tarifftype").list();
to
List TariffTypeList = session.createQuery("from TariffTypeValue").list();
Thanks to http://forums.devshed.com/java-help-9/question-about-hibernate318157.html
user: stdunbar

Also a very helpful comment on this same forum thread from
user Outlaw:
guess it'll be wrong but try to change "tbl_TarifType" to lower case everywhere long ago i had a problem with table namesguess in postgreSQL names might be in lower case
INDEED USE SMALL CAPITALS IN POSTGRESQL...
Thanks guys

No comments: