tags:

views:

51

answers:

1

I am developing a project at java fresher level. I had to implement database interaction using hibernate. At the first stage, I started using HQL query language. But, later I come to know about Crieteria through my previous questions. But, still after learning crieteria, I am not getting what are the steps I should follow to fill and fetch data to and from database. In fact, what are the packages and classes I need to develop manually and the script or query I need to write to fill/fetch data if I am given a database and table in it, using Crieteria ? Please also tell me, where would be difference when I use different database like PostGres or MySQL within steps.

+1  A: 

In fact, what are the packages and classes I need to develop manually and the script or query I need to write to fill/fetch data if I am given a database and table in it, using Crieteria?

Create an object model and map it to the tables using either annotations or xml mapping files. Classes that can be persisted are called Entities. Using the reverse engineering module of Hibernate Tools, it is possible to generate them.

Create instances of entities, set their attributes and use session.persist(Object) to persist them to the database to "fill data". Use the Criteria API to read from the database and fetch data. Data access is typically done in a data access layer using the DAOs pattern. DAOs expose finders and CRUD methods (Create, Read, Update, Delete).

If all this is new to you, I'd suggest to use Spring, it provides useful support classes and will help you to structure your application following the above pattern. Have a look at the Chapter 12. Object Relational Mapping (ORM) data access.

Please also tell me, where would be difference when I use different database like PostGres or MySQL within steps.

If their physical model differs, you may have to change the mapping of entities. Apart from that, switching from one database to another would require using the appropriate JDBC driver, changing the connection string and the Hibernate dialect (i.e. this is more a matter of configuration).

Pascal Thivent