Hi guys, we are back!
Today, we are going to see how to manage your classes to store, update, get, delete them of your data base, because a real application of management uses a data base to store the information to work.
We will work with a MySQL data base, Java (of course) and Hibernate (a framework). We use Hibernate because we need to do a mapping of our classes, for example, the Contact.java class, to store its objects (its contacts) on our MySQL data base. This task is very easy if we use this framework, Hibernate.
I have written a post about Hibernate. I think that is necessary to study something about Hibernate before, so you can take a look here: http://quickguide4engineers.blogspot.com/2011/09/introduction-to-hibernate-part-1.html
Then, you can watch the video-tutorial. I hope it is helpful for you.
You can get the eclipse project on our public mercurial repository cloning this url with tortoise mercurial hg: https://bitbucket.org/qg4engineers/mycontacts
This is a site for software engineers. I'll go posting a little tricks or how to do something in the different languages of programming that I know, and the main is Java. /* Este es un sitio para ingenieros de software. Iré creando entradas con pequeños trucos o cómo se hace algo en los diferentes lenguajes de programación que conozco, siendo el principal Java.*/
Showing posts with label Data Bases. Show all posts
Showing posts with label Data Bases. Show all posts
Saturday, January 28, 2012
Friday, September 23, 2011
Introduction to Hibernate - Part 1
Hibernate is a framework that helps to work with data access in databases and it improves the use of java objects persistence. The official page is http://www.hibernate.org/. Check out!
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:
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):
I'm going to explain some parts of this xml file: The first one, they're the connection parameters that you have to know:
We're mapping our class so that adding java objects persistence here. Client and product classes are in model package:
Have a nice week!
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!
Subscribe to:
Posts (Atom)



