Tuesday, May 11, 2010

Hibernate with MySQL Setup

Recently, I followed the Hibernate Tutorial posted on official Hibernate site. Here is the link to the tutorial. I ran into some issues and my previous posts explain how I resolved them.

In this post, I want to step you through the Hibernate setup with a MySQL DB. It is very simple and straight forward.

Prerequisite: You need to have MySQL installed/setup.

Step 1: Hibernate Configuration File
Here is the code for hibernate.cfg.xml


"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">



com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/tutorial
tutor
passwrd


1


org.hibernate.dialect.MySQL5InnoDBDialect


thread


org.hibernate.cache.NoCacheProvider


true


update









Step 2: Maven Dependency

If you are using Maven, here is the dependency you need to add (to the list of dependencies)


mysql
mysql-connector-java
5.0.5



Step 3: Create Database and add permission for Hibernate to access it
You will have to create a database (or use an existing one).
In this case my database name is 'tutorial'.
The user accessing this database with username 'tutor'@'localhost' and
password is 'passwrd'
In your console/command prompt run the following commands:

> mysql -u root -p
mysql> CREATE DATABASE tutorial;
mysql> CREATE USER 'tutor'@'localhost' IDENTIFIED BY 'passwrd';
mysql> GRANT ALL PRIVILEGES ON tutorial.* TO 'tutor'@'localhost'
-> WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;


That's all you need to start using MySQL DB with Hibernate.

No comments:

Post a Comment