Yet Another Blog

February 8, 2009

Setting up Hibernate with MySQL under GNU/Linux

Filed under: linux, Software — guilleml @ 1:01 am

We’ll need to have installed mysql packages, of course, as long as the java libraries for hibernate, mysql jdbc connector or any other things needed for the project as struts, spring…maybe an IDE as Eclipse or Netbeans.

Query browser may be useful to check database updates, modify tables o whatever you could need, anyway, what I wish to comment is the hibernate.cfg.xml file:

<?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”&gt;
<hibernate-configuration>
<session-factory>
<property name=”hibernate.dialect”>org.hibernate.dialect.MySQLDialect</property>
<property name=”hibernate.connection.driver_class”>com.mysql.jdbc.Driver</property>
<property name=”hibernate.connection.url”>jdbc:mysql://localhost:3306/empresas</property>
<property name=”hibernate.connection.username”>root</property>
<property name=”hibernate.connection.password”>toor</property>
<mapping resource=”hibernate.hbm.xml”/>
</session-factory>
</hibernate-configuration>

Take a look I’m using JDBC driver for MySQL, you should change the hibernate.connection.url with your own following the same format, let’s sayour database’s name is “enterprises”, then your connection url will be jdbc:mysql://localhost:3306/enterprises

netbeans - hibernate

The classes and tables mapping will be in hibernate.hbm.xml with its own format, for example:

<class dynamic-insert=”false” dynamic-update=”false” mutable=”true” name=”com.myapp.struts.Empresa” table=”empresahib” optimistic-lock=”version” polymorphism=”implicit” select-before-update=”false”>
<id name=”id” column=”id” type=”long”>
<generator class=”increment”/>
</id>
<property name=”nif” type=”java.lang.String”/>
<property name=”fecha” type=”java.sql.Date”/>
<property name=”rep_legal” type=”java.lang.String”/>
<property name=”denomsocial” type=”java.lang.String”/>
<property name=”telefono” type=”java.lang.String”/>
<property name=”email” type=”java.lang.String”/>
</class>

Create a free website or blog at WordPress.com.