Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Reading DB table using Hibernate

843838Jul 12 2007 — edited Jul 14 2007
I've finally enabled hibernate persistence in my application, and it looks like everything is moving smoothly on the writing side. The database is being written to as it should. My final step in incorporating hibernate will be to have a javabean read the database to return an array of objects for a datatable to display.
I've been following this tutorial: http://www.codeguru.com/cpp/misc/misc/interfacingtootherlanguages/article.php/c10079__1/

The save method in that tutorial needed a small fix, so I'm wondering how reliable the method for loading objects is.
This is what I'm using right now:
	public Project[] listProjects() throws Exception {
		Project[] projectAll;
		ArrayList projAlist = new ArrayList();
		
		//Log4J initialisation
		org.apache.log4j.BasicConfigurator.configure();
		

		//***RELEVENT HIBERNATE CODE

       		 // Create SessionFactory and Session objects
	        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        	Session session = sessionFactory.openSession();

	        //Query using Hibernate Query Language
        	String sqlString = "FROM project as p";
       		Query query = session.createQuery(sqlString);
        
	        //Iterate through stored objects
        	for (Iterator it = query.iterate(); it.hasNext();) {
           		Project tempProjObj = (Project) it.next();
           		projAlist.add(tempProjObj);
        	}
		session.close();

		//***END RELEVENT HIBERNATE CODE
		
		// Create an array of type Projects from projAlist arraylist
		projectAll = new Project[projAlist.size()];
		// Populate projectAll array with Project objects from projAlist arraylist in reverse order
		for (int i = 0; i < projAlist.size(); i++) {
			projectAll[i] = (Project) projAlist.get( (projAlist.size() - i -1) );
		}
		return projectAll;
	}
I recognise that any number of things may be going wrong. I've tried playing around with the HQL statement a bit to see what kind of results I can get, but have settled on using the syntax provided by the tutorial. Using a test class, I receive this error in calling this method:

Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: project is not mapped. [FROM project as p]

I have a mapping configuration file for the object. Do I need to add anything or even have another file for mapping an object from a relational database or should I be using the same mapping as that for object -> schema? Does anyone have a good resource for valid SQL/HSQL statements? I only need to learn basic SQL statements to make appropriate queries to the database, and plan to learn more SQL from there. I'm confused about the compatibility between SQL and HSQL, and what constitutes a valid SQL statement. For example, MySQL query browser is using this to query the table:
SELECT * FROM hibernate10.project p
The same statment does not seem to be valid in the java class, as it causes even more problems. Can someone explain to me what is going on here?

Also, the Log4J initialization line is there because I was getting errors concerning Log4J. Is there a more appropriate thing to do? That line doesn't seem so elegant, and I'm not currently using Log4J for anything.

Thank you all for your help
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 11 2007
Added on Jul 12 2007
3 comments
1,169 views