Configure MySql Database With Hibernate Mappings

SHARE & COMMENT :

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/

also read:

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 (Read: List of Hibernate Dialects) is the dialect name for MySQL database. Each database has its own dialect declared in the hibernate package.

<?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>

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Comments

  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.

    • 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

  2. 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.

  3. hi, krishna

    could you send me info if in projects which use mysql 5.2.x and hibernate 4.2 there is a need to include mysql-connector-java-5.x.x-bin.jar to the hibernate user library in project

    thanks

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better