We have to look for our drive to the database that we're working. For example, I work with MySQL5 then I have to choose a Hibernate SQL dialect for that: http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/session-configuration.html#configuration-optional-dialects
We need mapping our classes so that accessing to entities or tables of our working data base.
They exist two options to work in Hibernate for mapping:
- Using annotations (It's my favorite)
- Using XML file (I won't show it there. I don't like this)
So that Starting to work, we have to go to the page of downloads (http://www.hibernate.org/downloads) and to download the last release pack. Inside of this we find a lot of libraries, but at this moment we need these:
- hibernate-core-[version number].jar (It's the heart of Hibernate)
- hibernate-jpa-[version number]-api[version number].jar (this is for to use annotations)
- slf4j-api-[version number].jar (it's a log for java framework: http://www.slf4j.org/ and hibernate uses a log)
- slf4j-simple-[version number].jar
- mysql-connector-java-[version number].jar (it's mysql driver for java: http://www.mysql.com/downloads/mirror.php?id=403046#mirrors)
I work with Eclipse IDE and I'll show the image captures there. (http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/indigor)
Let's go to start!
I always create a folder in my java projects to keep use libraries and I add them to build path.
Next, we create 4 packages: dao, model, utils and test.
Then we need to create a configuration file to Hibernate:
Now, we need to copy this in the hibernate.cfg.xml (it's the configuration file to Hibernate):
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">com.mysql.jdbc.Driver jdbc:mysql://localhost/yourdatabase youruser yourpassword 1 org.hibernate.dialect.MySQL5Dialect true <mapping class="model.Client"/> <mapping class="model.Product"/>
I'm going to explain some parts of this xml file: The first one, they're the connection parameters that you have to know:
com.mysql.jdbc.Driver jdbc:mysql://localhost/yourdatabase youruser yourpassword
- yourdatabase: it's the name of your database
- youruser: it's the name of your user that you use to connect to the database
- yourpassword: it's the password of your user
We're mapping our class so that adding java objects persistence here. Client and product classes are in model package:
<mapping class="model.Client"/> <mapping class="model.Product"/>Eventually, we have done Hibernate initial configuration. In the next part, I'll continue to explain how to access to the database very easy, because Hibernate help us!!!!
Have a nice week!




Por esto me preguntabas sobre hibernate no? jajaja Recopilando información jeje. Muy interesante el blog por cierto
ReplyDeleteJeje, gracias crack!!
ReplyDelete