Rabu, 18 Agustus 2010

How To Install And Configure Hibernate

MenuHome
Install and Configure WAMP
Install and Configure Java
Install and Configure Hibernate
Install and Configure Eclipse
Hibernate is an incredible open source Object-relational mapping (or ORM, O/RM, O/R mapping) tool in Java. Suppose you have many database tables that are related to each other in some ways. If you want to query them with raw SQLs it'd be a nightmare (lots of joins, criteria, etc.). But with Hibernate you simply define the mappings in XML configuration files and you'll be able to retrieve the data as Java objects and be able to manipulate them as such

Download the latest Hibernate distribution package from Hibernate's download website. Mine is hibernate-distribution-3.5.3-Final-dist.zip. If the latest version is not this one it's fine. Try it and Let me know if you encounter any issues.. Unzip it and drop the following jars in your java's extension directory (mine is C:\Program Files (x86)\Java\jdk1.6.0_20\jre\lib\ext\). If you don't know why we are doing this consult How Java Recognizes Where Things Are.

* hibernate-distribution-3.5.3-Final\hibernate3.jar
* hibernate-distribution-3.5.3-Final\lib\jpa\hibernate-jpa-2.0-api-1.0.0.Final.jar
* every jar in hibernate-distribution-3.5.3-Final\lib\required\

Download the latest slf4j package at http://www.slf4j.org/download.html. At the time of writing it is slf4j-1.6.0.zip. Unzip it and drop slf4j-api-1.6.0.jar in java's extension folder; then delete slf4j-api-1.5.8.jar in the same folder if you see it.

Download and drop mysql-connector-java-5.1.13-bin.jar in java's extension directory.


Now you should be able to use Hibernate! Let me give you a sample setup. Create hibernate.cfg.xml in C:\ and put the following in it. Hibernate automatically reads hibernate.cfg.xml from the directory of the Java class you are running. Questions?

<?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>
<property name="connection.url">jdbc:mysql://localhost:3306/mffl</property>
<property name="connection.username">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- <property name="connection.password">iamroot</property>-->
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<!-- thread is the short name for
org.hibernate.context.ThreadLocalSessionContext
and let Hibernate bind the session automatically to the thread
-->
<property name="current_session_context_class">thread</property>
<!-- this will show us all sql statements -->
<property name="hibernate.show_sql">false</property>

<!-- mapping files -->
<mapping resource="config/automate.hbm.xml" />

</session-factory>
</hibernate-configuration>


From this configuration file Hibernate learns about underlying properties of the database and where to look for mapping files (specified by mapping tag). Here's the corresponding automate.hbm.xml in C:\config\: Questions?

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping default-lazy="false">
<class name="entity.Brand" table="brand">
<id name="brandId" column="brand_id">
<generator class="increment" />
</id>
<property name="title" column="title" />
<set name="stores" table="brand_to_store" cascade="all" lazy="false">
<key column="brand_id" />
<many-to-many column="store_id" class="entity.Store" />
</set>
</class>
<class name="entity.Store" table="store">
<id name="storeId" column="store_id">
<generator class="increment" />
</id>
<property name="title" column="title" />
<set name="brands" table="brand_to_store" cascade="all" lazy="false">
<key column="store_id" />
<many-to-many column="brand_id" class="entity.Brand" />
</set>
</class>
</hibernate-mapping>


You should be able to create the corresponding database tables brand, store, and brand_to_store. Then create the corresponding Java files Brand.java and Store.java. Questions? Let me know!

◀ Install and Configure JavaInstall and Configure Eclipse ▶

0 komentar:

 
support by: infomediaku.com