Hibernate programozás

A hibernate egy perzisztencia kezelő segédeszköz. Tranzakciók kezelésére, több adatbázis párhuzamos használatára alkalmas. Itt egy mintaprogram az első lépésekhez.

package com.pazo.jdbc.Examples;
 
import java.io.File;
import java.util.Hashtable;
import java.util.Iterator;
 
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
 
public class ExampleHibernate {
 
	static String fileName = "src\\com\\pazo\\jdbc\\Examples\\hibernate-mysql.cfg.xml";
 
	public static void main(String[] args) {
		File configFile = new File(fileName);
		Configuration result = new Configuration();
		Configuration hibernateConfig = result.configure(configFile);
		SessionFactory hibernateFactory = hibernateConfig.buildSessionFactory();
		Session session = hibernateFactory.getCurrentSession();
		Transaction tx = session.beginTransaction();
		Iterator iter = session.createQuery("from SystemDescriptor").list().iterator();
		while (iter.hasNext()) {
			SystemDescriptor systemDescriptor = (SystemDescriptor) iter.next();
		}
		tx.commit();
	}
}

A hibernate-mysql.cfg.xml tartalma:

<?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/demand
        </property>
 
        <property name="connection.username">
            root
        </property>
        <property name="connection.password">
        </property>        
		<!-- JDBC connection pool, use Hibernate internal connection pool -->
        <property name="connection.pool_size">
            5
        </property>
 
        <!-- SQL dialect -->
        <property name="dialect">
            org.hibernate.dialect.MySQLDialect
        </property>
 
        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">
            thread
        </property>
 
        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">
            org.hibernate.cache.NoCacheProvider
        </property>
 
        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">
            true
        </property>
 
        <mapping resource="com/pazo/jdbc/Examples/systemdescriptor.hbm.xml"/>
    </session-factory>
 
</hibernate-configuration>

A systemdescriptor.hbm.xml tartalma

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 
<hibernate-mapping>
	<class name="com.pazo.jdbc.Examples.SystemDescriptor"
		table="SystemDescriptor" node="systemdescriptor">
		<id name="id" column="id" node="@id">
			<generator class="native" />
		</id>
		<property name="name" node="name" />
		<property name="descriptorXML" type="text"
			column="descriptorXML" node="descriptorxml" />
	</class>
</hibernate-mapping>

A SystemDescriptor osztály:

package com.pazo.jdbc.Examples;
 
public class SystemDescriptor {
	private int id;
	private String name;
	private String descriptorXML;
 
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDescriptorXML() {
		return descriptorXML;
	}
	public void setDescriptorXML(String descriptionXML) {
		this.descriptorXML = descriptionXML;
	}
 
}

A szükséges segédkönyvtárak a hibernate csomagból:

  • antlr
  • asm
  • cglib
  • commons-collections
  • commons-io
  • commons-logging
  • concurrent
  • dom4j
  • hibernate3
  • jta
  • MySQL kapcsolat létrehozásához mysql-connector-java segédkönyvtár szükséges. Oracle adatbázishoz az ojdbc14.jar segédkönyvtár alkalmazható.