Professional Documents
Culture Documents
• Introduction of HibernateV3
Configuration & Setup of Hibernate/Java
• Code
• Pros and Cons
• RESOURCES
• Q & A.
2/2
copyright 2006, rhinosystemsinc.c
om all rights reserved
Intro:Comparison to SQL
• Problem with SQL
– never lived up to promise of standardization amongst
database vendors
– Uses Jdbc to access database – (no forced design)
– Is Relational
• With Hibernate:
– Caching
– Easier to code
– Standard access
– Is Object Oriented and maps to Relational.
1/2
copyright 2006, rhinosystemsinc.c
om all rights reserved
Intro:Comparison to SQL
• Speed/performance
– Prepared Statements and caching (hib)
– Can batch process many DML statements. (hib)
– Better performance from PL/SQL (sql/hib)
• Maintenance/Updates (hib)
• Legacy system (depends)
– Using hibernate with legacy database systems
& Legacy java code retrofit with Hibernate
2/2
copyright 2006, rhinosystemsinc.c
om all rights reserved
Configuration Hibernate/Java
2/5
copyright 2006, rhinosystemsinc.c
om all rights reserved
Config: Download & Config
Xdoclet
[OPTIONAL]
• In order to run the "generation" of hibernate
XML you need to setup your classpath to include
Xdoclet libraries
• Download latest (v1.2.3) from
http://xdoclet.sourceforge.net/xdoclet/index.html
(www.xdoclet.org?)
– Download direct from
http://sourceforge.net/project/showfiles.php?group_id=31602
– Tag References http://xdoclet.sourceforge.net/xdoclet/tags/hibernate-
tags.html
3/5
copyright 2006, rhinosystemsinc.c
om all rights reserved
config: Download & Config ANT
[OPTIONAL]
• In order to use ANT you'll need to setup your
PATH to include ANT. Check – run "ant"
• Download latest (v1.6.5) from http://ant.apache.org/
(www.ant.org?)
– Download direct from
http://www.axint.net/apache/ant/binaries/apache-ant-1.6.5-bin.zip
– Unzip and make sure "ant" is in your path. (right-click my computer ->
properties ->Advanced->Environmental Variables -> update PATH with directory
<ant_install>/bin)
4/5
copyright 2006, rhinosystemsinc.c
om all rights reserved
config: Download & Config ANT
• SUMMARY:
– JDK [required]: PATH,CLASSPATH
– HIBERNATE [required]: CLASSPATH
• ORACLE or DBMS client [required]:
CLASSPATH (connection/lib info w/ hibernate.properties)
– XDOCLET [optional]:CLASSPATH
– ANT [optional]:CLASSPATH
5/5
copyright 2006, rhinosystemsinc.c
om all rights reserved
Code & Fragments
See SAMPLE 0
1/4
copyright 2006, rhinosystemsinc.c
om all rights reserved
Code[0]: XML for Entity
<!--Must be named: Person.hbm.xml and reside in CLASSPATH-->
<?xml
<?xml version="1.0"
version="1.0" encoding="UTF-8"?>
encoding="UTF-8"?>
<!DOCTYPE
<!DOCTYPEhibernate-mapping
hibernate-mappingPUBLIC
PUBLIC"-//Hibernate/Hibernate Mapping
"-//Hibernate/Hibernate DTD 3.0//EN"
Mapping "http://hibernate.sourceforge.net/hibernate-mapping-
DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-
3.0.dtd">
3.0.dtd">
<hibernate-mapping>
<hibernate-mapping>
<class name="com.rhinosystemsinc.hibernate.samples_0.Person"
<id name="PERSON_ID" table="SAMPLE0_Person" >
column="PERSON_ID" type="java.lang.Long" unsaved-value="null">
<generator class="hilo">
<id name="PERSON_ID" column="PERSON_ID" type="java.lang.Long" unsaved-value="null">
<param name="table">SAMPLE0_PERSON_SEQ</param>
<generator class="hilo">
name="column">NEXT</param>
<param name="table">SAMPLE0_PERSON_SEQ</param>
</generator>
<param name="column">NEXT</param>
</id> </generator>
<property
</id>
name="FIRST_NAME"
<property
type="java.lang.String"
name="FIRST_NAME" Define the manufactured surrogate
update="true"
type="java.lang.String"
insert="true"
update="true"
primary key, based on SEQUENCE.
column="FIRST_NAME"
insert="true"
not-null="true"
column="FIRST_NAME"
unique="false"
not-null="true"
length="100"
unique="false" Also natural composite primary keys:
/>length="100"
<property
<composite-id>
/>
name="LAST_NAME"/>
<property name="LAST_NAME"/> <key-property name="name"/>
</class> <key-property name="ssn"/>
</hibernate-mapping>
</composite-id>
2/4
copyright 2006, rhinosystemsinc.c
om all rights reserved
Code: Primary Keys
3/4
copyright 2006, rhinosystemsinc.c
om all rights reserved
Code[0]: Java Main Class
//SAVING A NEW OBJECT
org.hibernate.Session sess = sessFact.openSession(); //more on SessionFactory in a minute.
Person p = new Person();
p.setFIRST_NAME("John");
p.setLAST_NAME("Smith");
Transaction tx = sess.beginTransaction();
sess.saveOrUpdate(p);
tx.commit();
At this point, what if we
do an
sess.close();
update, via SQLPLUS?
See SAMPLE 0
4/4
copyright 2006, rhinosystemsinc.c
om all rights reserved
Code[0]: Java 1-to-Many
public class Person {
Long PERSON_ID=null; //manufactured surrogate key
String FIRST_NAME=null;
String LAST_NAME=null;
Set ADDRESSES=null;
public Set getADDRESSES()
{
return ADDRESSES;
} NEW
public void setADDRESSES(Set ADDRESSES)
{
this.ADDRESSES = ADDRESSES;
}
…//rest of get/set methods
}
p2.setLAST_NAME("Thompson");
p2.setADDRESSES(new HashSet());
p2.getADDRESSES().add(addr);
addr.getPersons().add(p2);
• Hibernate installation
– <installdir>/doc/
{api,other,reference}/en/pdf/hibernate_reference.pdf (great for
reference, yet not sufficient for beginners to "learn").
– <installdir>/eg – samples
• Miscellaneous
– Book: Hibernate In Action http://www.manning.com/bauer
– Performance #'s:
http://www.sourcelabs.com/?sidemenu=3&page=software&sub=sash_hibernateperftest
• Hibernate user FORUM - http://forum.hibernate.org/
(+200 msg/day)
• CaveatEmptor Sample from Hibernate:
http://caveatemptor.hibernate.org/
Thank you!
• Extra topics session follow, if time.
~ Or ~
• Q&A
• Future Questions:
email: joel@rhinosystemsinc.com
• Person.java • Person.hbm.xml
Long PERSON_ID=null; <id name="PERSON_ID"