Configure MySql database with Hibernate mappings

July 29, 2008

Hibernate

Configure MySql database

Configuring MySql database with hibernate is the same as with other databasses.The only difference will be connection url and the database dialect to be specified in the configuration file. This tips provides a basic example configuration file for configuaring the MySql with hibernate. But it doesn’t explain you installing the MySql database. If you are looing for installing MySql database please read it here : http://dev.mysql.com/downloads/

Sample URL is jdbc:mysql://localhost:3306/SampleDB. In this URL port 3306 is default for the mysql database. If you are not specifying that in the URL, it will assume the port is 3306.localhost can be replaced with your system IP address and machine name.SampleDB is the database or catelog name you have created. Every session factory can have only one database. if you want to use multiple database, create multiple session factory with different database names.

org.hibernate.dialect.MySQLDialect is the dialect name for MySQL database. Each database has its own dialect declared in the hibernate package. You can find the list of dialects here : http://www.hibernate.org/hib_docs/reference/en/html/session-configuration.html.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?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">
 
<hibernate-configuration>
    <session-factory>
        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/SampleDB</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
 
        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>
 
        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
 
        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
 
        <!-- Mapping files -->
        <mapping resource="Author.hbm.xml"/>
    </session-factory>
</hibernate-configuration>
email

Comments

comments

Related posts:

  1. How to use datasource in Hibernate application?
  2. How to Configure hibernate using XML files?
  3. Hibernate basic configuration example
  4. Batch insert in Hibernate
  5. jUDDI and Configuration in jBoss and MySQL database

3 Responses to “Configure MySql database with Hibernate mappings”

  1. hemanth Says:

    hi,
    I am unable to connect mysqldb using hibernate.i have to connect from onelinux system to anotherlinux system . and i gave the ip address of another linux system but am unable to connect db please help me on this issue.

    Reply

    • suthukrish Says:

      Hello Hemanth,

      Are you doing the developemnt in Linux system. Did you check whetheter all the network connection is proper. Can you post your connection code here?

      Thanks,
      Krishna

      Reply

  2. hem Says:

    hi,

    I am using hibernate and i have doubt in sessions. can u please guide to me.
    When am post (save or update) time only am using session.i.e begintranasaction and rollback and close functionalists. When i want to fetch some records from database am not following the begin transaction and close transaction .Can u please guide me is am followed is correct or not.

    Reply

Leave a Reply