Hibernate
From CSWiki
Hibernate is a solution for object relational mapping and a persistence management solution or persistent layer. Hibernate provides a solution to map database tables to a class. It copies one row of the database data to a class. In the other direction it supports to save objects to the database. In this process the object is transformed to one or more tables. Saving data to a storage is called persistence. And the copying of tables to objects and vice versa is called object relational mapping.
Software Requirement :
Eclipse 3.x
MyEclipse 4.x is recommended but optional
Hibernate 3.x (I used 3.1)
Steps To Create a Hibernate Project Demo :
-> Create a Java Project named : FirstHibernateExample
-> If you are using MyEclipse, right click on your project in the package explorer and choose Add Hibernate capabilities. Continue the wizard and create a new hibernate.cfg.xml in the src directory.
If you are not using MyEclipse, download Hibernate 3.x. then, Open your project properties, select “Java Build Path”,click on “Add External Jars” and add the libaries shown below to your project path.
-> Create a session factory, It implements a design pattern, that ensures that only one instance of the session is used per thread. You should only get your Hibernate session from this factory.
-> Confuring Log4j Properties file - This library does like a configuration file in the source directory
-> Add Database Driver.(for Ex. MySql)
-> Add table in "firsthibernate" database.
CREATE TABLE `honey` ( `id` int(11) NOT NULL auto_increment, `name` varchar(250) default NULL, `taste` varchar(250) default NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1
-> Create a new class named “Honey” in the package “de.laliluna.example”. Add three fields id, name and taste and generate (Context menu -> Source -> Generate Getter and Setter) or type the getters and setters for the fields. Then create an empty constructor.
-> Create a new mapping file named “hibernate.cfg.xml” in your root directory if it is not already created.Which should include following tags :
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/webdatabase</property> <property name="connection.username">root</property> <property name="connection.password">test</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="transaction.factory_class"> org.hibernate.transaction.JDBCTransactionFactory </property>
<property name="current_session_context_class">thread</property> <property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property> <property name="myeclipse.connection.profile">Test1</property> <mapping resource="de/laliluna/example/Honey.hbm.xml" />
</session-factory>
</hibernate-configuration>
-> Create a Java class that includes methods to create entries in the database, to update and to list them.
-> Run this class as a Java Application.


