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>

Advertisements

2 Comments »

  1. Soy mariquita y una nenaza.
    Te voy a pedir que me expliques un poco de hibernate un dia si puedes.
    Xaop

    Comment by Jewi — March 9, 2009 @ 11:07 am

  2. Hello it’s me, I am also visiting this web page on a regular basis, this web site is truly pleasant and the visitors are actually sharing pleasant thoughts.

    Comment by premiumpress wordpress themes — February 22, 2013 @ 10:27 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: