Configuration of Hibernate, MySQL & Eclipse

6 06 2007

Hi!

I’m gonna tell you how you could configure these 3 tools. Together, they provide great facilities to us to construct and to manipulate web applications. This configuration will be based on:

First of all, you have to create a new project of type Dynamic Web Project with the Eclipse environment; we are going to call it “test“. In the field “Target Runtime” we must indicate which server we are gonna use. In our case, we will use the Tomcat Apache 5.5. With the created project and a good database in the MySQL, now, we must import all the Hibernate libs into /WebContent/WEB-INF/lib folder. There is another way: you must put all the Hibernate libs into /tomcat/common/lib folder. This last one it’s better if you have to export the project in a “*.war” file.

We are ready to establish connection and mapping between the database and our project.

We are going to create the Hibernate Configuration File. This one, will contain the information to access to the database.

ROUTE: File –> New –> Other –> Hibernate –> Hibernate Configuration File

hibernate.cfg.xml

(I have to say that this configuration is right for all of the available databases. If you don’t use MySQL, you must select the correct “database dialect” and “driver class”..)

Now, we are going to create the Hibernate Console Configuration file.

ROUTE: File –> New –> Other –> Hibernate –> Hibernate Console Configuration

In addition, we are going to create the Hibernate Reverse Engineering file. This one, will allow to us to generate the source code of VO and DAO layers (VO = Value Object; DAO = Data Access Object).

ROUTE: File –> New –> Other –> Hibernate –> Hibernate Reverse Engineering

We must select the console that we had created and then clicking on the “Refresh” button, we have to be able to see the database structure. Finalize.

That’s all! Now it is everything ready to generate the source code.

ROUTE: Run –> Hibernate Code Generation –> Hibernate Code Generation
  • Main: We must select the console,  to specify the output directory, to mark the “Reverse Engineer From JDBC connection” option and select the path of the Hibernate Reverse Engineering file in “reveng.xml”.
  • Exporters: we must choose to work with Java 5 and to mark:
    • Domain Code
    • Hibernate XML mappings
    • DAO code
    • Hibernate XML configuration
  • Run and.. source code created! ;)

I hope this little manual of the Hibernate configuration be useful. If someone has any question, please post it.

See ya!!


Actions

Information

10 responses

7 06 2007
ximo

kines kurrades et fas no ara?

8 06 2007
e$cRi

Ets el putu jefe tiu!….quin crack!

25 09 2007
priya

hi ,

i am very beginnner to Hibernate.can you please explain a little bit more..

If u explain with a example then it will be very helpful for a beginnner like me

thanks

priya

16 02 2008
S.Sathishkumar

How to open the HQL editor in Hibernate Configuration

Do you mean something like this??

session.createQuery("select emp.sal from comp.Emp as emp").list();

More info here:

http://www.developer.com/java/ent/article.php/10933_3322131_2
http://www.hibernate.org/hib_docs/reference/en/html/index.html

14 05 2008
Nhan

Thanks a lot

30 01 2009
Efrain

Muchas gracias, me has ayudado a configurar eclipse-hibernate-jboss.

Si os da algún problema que que no encuentra la clase conector del driver, añadir al proyecto manualmente el driver, que se descarga desde la página oficial de MySQL buscando connector en el menú lateral izquierdo.

30/Enero/2008

Tengo un problema con al refrescar la base de datos, algo relacionado con SWTResource.

Si alguien sabe algo?

He probado descargándome el plugin Workbench pero parece ser que necesita eclipse descargarse uva versión mas actual de ese mismo plugin

30 01 2009
Efrain

Perdón por lo de la fecha, no había visto que ya estaba comentada y encima me he equivocado en el año

11 02 2009
Jotnarta

Thanx, but can you give us a simple example how to connect to database from a Jsp or a servlet file???

23 03 2009
twista

Thanx for your nice tutorial.It helped me a lot .
Merci!

8 05 2009
Mohan

Thanks its very useful
If u explain with an example its very useful to me

Leave a comment