One day late in 2008 we found that starting with hibernate is not easy - even for experienced Programmers. Against this background I decided to give this documentation - with the intention to provide a SIMPLE start with hibernate. This Document is not to make a proper hibernate project which is using all features and has only the needed jars referenced. It is explicitly opposite to that: The intention is to get quick success, to produce a running project with hibernate and to understand the idea behind that - quick and dirty :-). A minimum of swing knowledge is helpful as we use a simple dialog to write to db.
mkdir /home/mydev
Copy following jars from download (either from ./ (root) of your downloaded file and/or from ./lib folder you should have the following jars:
from hibernate32 File New Java Project
Set build Path:
Create in src folder of your project a file named log4j.properties and add the following content
log4j.rootLogger=INFO,MeinConsoleAppender, FileAppender
log4j.appender.MeinConsoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.MeinConsoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.MeinConsoleAppender.layout.ConversionPattern=%d{ISO8601} %-5p [%t] %c: %m%n
log4j.appender.FileAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FileAppender.file=MeineLogDatei.log
log4j.appender.FileAppender.Append=true
log4j.appender.FileAppender.MaxFileSize=10000KB
log4j.appender.FileAppender.MaxBackupIndex=2
log4j.appender.FileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.FileAppender.layout.ConversionPattern=%d{ISO8601} %-5p [%t] %c: %m%n
Create a new Testclass
In Eclipse press (in Menu) File -> new ->Java class and
name it Test (case-sensitive).
Paste the following code into the Test class (note that it is directly
in src Folder - no packages for simpleness ...)
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class Test extends JFrame {
private JButton button;
private JTextField textField;
private SessionFactory sessionFactory;
public static void main(String[] args){
new Test();
}
public Test(){
// get a sessionfactory from hibernate on startup - this is needed to get a
// session later
try{
// if you do not like annotations, you might configur with xml Files -
// if so, the Factory is a different (Configuration())
sessionFactory = new AnnotationConfiguration().configure()
.buildSessionFactory();
}
catch(Throwable e){
throw new ExceptionInInitializerError(e);
}
createView();
createListener();
pack();
setVisible(true);
}
private void createView(){
setLayout(new FlowLayout());
add(textField = new JTextField(6));
add(button = new JButton("button"));
}
private void createListener(){
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
// in hibernate you usually communicate with DB by
// getting / providing POJOs
POJO p = new POJO();
p.setValue(textField.getText());
// take a session
Session sess = sessionFactory.getCurrentSession();
// Start transaction
sess.beginTransaction();
sess.save(p);
// commit transaction
sess.getTransaction().commit();
}
});
}
}
This Test class is just
providing a Dialog with a Textbox and a Button.
On start it will set the Sessionfactory for Hibernate Annotations.
In the Actionlistener it will create a POJO (which is a pojo - see next
Chapter), sets the value of that POJO and stores it in DB.
As you can see in source above,
communication is done using POJOs - we need to create one.
In eclipse press on File ->New -> Java Class and name it
POJO.
Paste the following content in POJO class.
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name = "POJO")
public class POJO {
@Column(name = "value", length = 60)
public String getValue(){
return value;
}
public void setValue(String value){
this.value = value;
}
@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
public String getIdentifier(){
return identifier;
}
public void setIdentifier(String identifier){
this.identifier = identifier;
}
private String value;
private String identifier;
As you can see, the POJO is just a POJO - plus some Annotations. Some explanation might help you:
This file is the fix-point for
Configuration of hibernate.
It provides the Info how to connect to DB along with the info which File
will be mapped (name and relative Path, here not necessary due to all
Files are in one folder).
In Eclipse in Menu press on File ->New ->File , name it
hibernate.cfg.xml (case sensitive) in the src folder.
Paste the following code in this 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">
<hibernate-configuration>
<session-factory>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">org.h2.Driver</property>
<property name="hibernate.connection.password">obiwan</property>
<property name="hibernate.connection.url">jdbc:h2:file:~/marcel</property>
<property name="hibernate.connection.username">kenobi</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping class="POJO"/>
</session-factory>
</hibernate-configuration>
This will create a Database EACH
time you will start the program (due
to <property
name="hibernate.hbm2ddl.auto">create</property>)
Usually you will use it only on first start up, because it automatically
create an empty Database on EVERY Start
Therefore after first start you should change it to <property
name="hibernate.hbm2ddl.auto">update</property>
By this only new Columns will be added if you add them to the POJO
AND you have annotated them.
Now you might start the program (right-click on Test class and select
runas Java Application).
A dialog will be displayed.
You can write something into the textfield and press the button - the
content of the textfield will be written into the DB.
You can stop it with eclipse (Terminate button in Console View) - due
to simpleness and laziness a Windowadapter is missing.
If you do not trust the results
download a db tool (eg. Squirrel) and configure it to connect to db.
Be aware that this is only possible if you will have the Test class NOT
running - due to the fact that in the given scenario only one
connection to DB is configured.
A simple configuration for Squirrel would be:
Say OK and doubleclick on Myhib, confirm user and password and the db
will be opened ...
You will see in the public / table node the table POJO with the value
you gave ...
That's all, folks ...!
If you need more than one POJO,
you can create it, annotate id accordingly and add a mapping in
hibernate.cfg.xml.
If the Pojo is in a package, add the package in dotted notation e.g.
my.package.hib.Person.
Read about the generators in the hibernate documentation!
In my eyes one of the most famous tools for hibernate is the hibernate
tools - by which you
can reengineer from a database the complete hibernate stuff including
annotated Pojos!
If you would like to test it, TAKE A LOOK into
COMPATILIBITY MATRIX on Hibernate Homepage, otherwise you will be very
soon highly frustrated.
Be aware that for me a one2many annotation did NOT remove the orphan
elements, although it was proper annotated.
An alternative to hibernate might be OpenJPA -not Mainstream, but it is
an Apache child.
That cannot happen :-)
... but if...
Depending on the Eclispe Version / Settings files are not copied to the
folder which holds the .class files- but this is mandatory.
Check if hibernate.cfg.xml and log4j.properties, POJO.class and
Test.class are all in one Folder.
Marcel (by the way: Thank You!) found that the latest version of H2
causes some trouble (I could not reproduce). However, he solved it by
taking the November 2008 Version (103).
I hope that some Colleagues will read and and give Feedback (Infotech
(Hyderabad, India) as well as some friends here, e.g. "Hochwürden",
"Hartmudus K....saurus", ...)