We started a set of tutorials to learn what Hibernate is , and how it works with Java. Today we just take a look how to configure before starting .
Hibernate, what is that?
Hibernate ORM enables developers to more easily write applications whose data outlives the application process. As an Object/Relational Mapping (ORM) framework, Hibernate is concerned with data persistence as it applies to relational databases (via JDBC).
But, ORM?, Object-relational mapping (ORM, O/RM, and O/R mapping tool) in computer science , is a programming technique for converting data between incompatible type systems using object-oriented programming languages. This creates, in effect, a “virtual object database” that can be used from within the programming language. There are both free and commercial packages available that perform object-relational mapping, although some programmers opt to construct their own ORM tools.
Now we are going to learn to configure our IDE with Hibernate step by step, and with the next tutorial (Hibernate II) we will be able to execute different sentences.
Of course we need a database to work with hibernate, in this case MYSQL.:
I create a new database named studentsdata , with a single table “students” and insert three students:
Okey so now its time to configure our IDE , we working with Netbeans this time.
Create New Proyect –>Create new –> Other
The first file we created in the Hibernate file,
hibernate.cfg.xml, just a configuration interface will take as a base
to initialize the SessionFactory (to open connections).
Select Hibernate –> Hibernate Configuration Wizar.Then create a new database connection , and select driver (MYSQL this time).
Drivers defined , so now customize connection , host, database, User name and Password.
You can add new user and grant privileges to database with :
Then try to connect, if all works correct you can follow the next steps by default (schema,name of connection…).
Second step , Reverse Engineering Wizard: add a new file Hibernate –> Hibernate Reverse Engineering Wizard.
Use default name of file , then add the tables:
A new .xml file should be generated in our proyect:
And the last file we need to finish the configuration : Hibernate Mapping Files and POJOS from Database:
You can see how Hibernate create a new class Students , with constructors , getters and setters , we use this class to work with the database in the next tutorial (Hibernate II ) using HQL .
The Hibernate Query Language (HQL) is the query language that Hibernate uses to obtain the objects from the database. Its main peculiarity is that the queries are made on the objects that make up our business model, that is, the entities that persist in Hibernate.
So hope you find this usefull and see you with the next tutorial , when we start to work with Hibernate!.
More info about Hibernate: