SlideShare a Scribd company logo
Let’s Hibernate  A brief introduction to Hibernate persistence service Rafael Coutinho (rcouto@br.ibm.com)
Topics to be addressed Introduction to O/R Mapping What is Hibernate? Why Hibernate? Simple Example Features Tools Conclusions Questions
Object/Relational mapping Motivation Development is Object Oriented Current main development languages Java, C++, C# Current main enterprises enviroments Websphere, JBoss, .Net Data needs to be persisted Current main DBMS are relational DB2, Oracle, MySQL.
Object/Relational mapping Why Mapping? Objects needs to be persisted OODBMS didn’t sell Get the best of two worlds Relational: Information management Object: behavior Relational know-how and tools
Object/Relational mapping Why a mapping engine? Even knowing how to map object to relational models Developers stay focused on the object model Isolates relational world from objects Have more robust structural mapping Data access layer creation is tedious Less interesting the work is, higher the risk of errors. Don’t re-invent the wheel !!!
What is Hibernate? Opensource object / relational mapping tool Persistence service Non intrusive approach Uses XML mapping files Currently in version 3.1.3  (April 12 2006)  Provides transparent persistence for POJO
Why Hibernate? Open Source (LGPL) Doesn’t need to be so free Mature software driven by user requests Popular (15.000 downloads/month) No application server needed Java and .Net
Architecture Overview XML  Configuration Hibernate Database Application Persisted Objects XML Mappings
Simple Example Person Address
Simple Example … <hibernate-mapping> <class  name = &quot;vo.Address&quot;   table = &quot;address&quot; > <id  name = &quot;id&quot;   column = &quot;id&quot; > <generator  class = &quot;increment&quot;  /> </id> <property  name = &quot;address&quot; />  </class> </hibernate-mapping> Address.hbm.xml
Simple Example … <hibernate-mapping> <class  name = &quot;vo.Person&quot;   table = &quot;person&quot; > <id  name = &quot;id&quot;   column = &quot;id&quot; > <generator  class =&quot;increment&quot; /> </id> <one-to-one  name = &quot;address&quot;   class = &quot;vo.Address&quot;   constrained = &quot;true&quot;   foreign-key = &quot;addressid&quot;  /> <property  name = &quot;name&quot;   type = &quot;string&quot; > <column  name = &quot;name&quot;  /> </property> </class> </hibernate-mapping> Person.hbm.xml
Simple Example Hibernate.cfg.xml <hibernate-configuration> <session-factory  name = &quot;Diagra&quot; > <property  name = &quot;hibernate.connection.username&quot; > userid   </property> <property  name = &quot;hibernate.connection.password&quot; > wantmommy   </property> <property  name = &quot;hibernate.dialect&quot; > org.hibernate.dialect.MySQLDialect  </property> <property  name = &quot;hibernate.connection.url&quot; > jdbc:mysql:///test  </property> <property  name = &quot;hibernate.connection.driver_class&quot; > com.mysql.jdbc.Driver  </property> <mapping  resource = &quot;Address.hbm.xml&quot; /> <mapping  resource = &quot;Person.hbm.xml&quot; /> </session-factory> </hibernate-configuration>
Code sample Open Session: Session session = HibernateUtils.currentSession(); Transaction tx = session.beginTransaction(); Load: Person aPerson = (Person) session.load(Person. class , personId); Store: session.save(aPerson); List: List result = session.createQuery(&quot; from Person &quot;).list(); Close Session:  tx.commit();  HibernateUtils.closeSession();
Features Transparent persistence Any class can be a  persistent  class (POJO) Nonintrusive No interface or base classes No source or byte-code generation/processing But at runtime Extensible type system User-defined types  Automatic dirty checking Detached object support Constraint transparency Foreign keys Needs no argument constructor Cascading
Features Flexible O/R mapping Driven by XML mapping Supports some inheritance table per class hierarchy  table per subclass  table per concrete class  implicit polymorphism  Support one-to-many, one-to-one and many-to-many Bidirectional and unidirectional associations (dependent objects)
Features Simple API Core for application Extension API for customization Object-Oriented query language HQL (Hibernate query language)  SQLlike Support polymorphic queries Native SQL
Features Operate in managed and non-managed environment J2EE  Can be configured via JMX(Java Management Extension) Can be integrated with application server transactions via JTA (Java Transaction API) Stand-alone application .Net
Features High performance Lazy Loading Fetching Strategies Retrieve associated objects Support for optimistic concurrency control Automatic versioning and time stamping
Development Scenarios Allow four development scenarios Top Down Start with JavaBeans Bottom Up  Start with relational schema Middle out Start with the mapping Meet-in-middle Start with both JavaBeans and relational schema
Tools Hibernate Tools 3 UML Model  XML/XMI POJO Java Source Mapping  Metadata Database Schema AndroMDA  XDoclet  SchemaExport  (hbm2ddl) Middlegen   CodeGenerator  (hbm2java)
Tools Eclipse IDE Plugin Wizard to Create Configuration File XML Mapping Editor Context dependent code completion in the XML mapping editor
Tools Hibernate Console   View Entity Structure Prototype Queries Properties view
Tools Reverse engineering  Code generation Mapping files  generation Configuration file  generation
Hands on Real life example
Conclusion Data access layer creation is tedious Automatic mapping is needed Mature robust software Open source No application servers needed Development tools No silver bullet
References Hibernate Website www.hibernate.org Tutorial www.gloegl.de/17.html Hibernate tools http://www.hibernate.org/255.html http://www.jboss.com/products/seam/SeamHBTools.html
Questions & maybe answers E-mail: rcouto@br.ibm.com
Questions I do have!!! Next presentation on May 10 th !

More Related Content

PPT
Introduction to hibernate
PPT
Intro To Hibernate
PPSX
DIWE - Coding HTML for Basic Web Designing
PPTX
Introduction to JavaScript
PDF
Smalltalk and Business
PPT
Js ppt
PDF
Enterprise PHP Architecture through Design Patterns and Modularization (Midwe...
PPTX
Hibernate working with criteria- Basic Introduction
Introduction to hibernate
Intro To Hibernate
DIWE - Coding HTML for Basic Web Designing
Introduction to JavaScript
Smalltalk and Business
Js ppt
Enterprise PHP Architecture through Design Patterns and Modularization (Midwe...
Hibernate working with criteria- Basic Introduction

What's hot (20)

PPTX
Introduction to JavaScript Programming
PPTX
Play Your API with MuleSoft API Notebook
PPT
Java script final presentation
PPTX
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
PPT
Web development basics (Part-4)
PDF
Basic JavaScript Tutorial
KEY
Object oriented javascript
PPT
JavaScript Workshop
PPTX
Javascript 01 (js)
PPT
Introduction to JavaScript
PPTX
Javascript
PPT
Java Script ppt
PDF
Wt unit 2 ppts client side technology
PPT
AJAX Workshop Notes
PDF
JavaScript - Chapter 3 - Introduction
PPTX
Java script
PDF
Baby steps to Domain-Driven Design
PPTX
GTAC 2010 -
Introduction to JavaScript Programming
Play Your API with MuleSoft API Notebook
Java script final presentation
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
Web development basics (Part-4)
Basic JavaScript Tutorial
Object oriented javascript
JavaScript Workshop
Javascript 01 (js)
Introduction to JavaScript
Javascript
Java Script ppt
Wt unit 2 ppts client side technology
AJAX Workshop Notes
JavaScript - Chapter 3 - Introduction
Java script
Baby steps to Domain-Driven Design
GTAC 2010 -
Ad

Similar to Basic Hibernate Final (20)

PPT
Apache Persistence Layers
PPT
Patni Hibernate
PPTX
Javazone 2010-lift-framework-public
PPT
Intro to-html-backbone
PPT
Boston Computing Review - Java Server Pages
PPT
I Feel Pretty
ODP
Ldap Synchronization Connector @ 2011.RMLL
PPTX
Dynamic Language Performance
PPT
Persisting Your Objects In The Database World @ AlphaCSP Professional OSS Con...
PPT
RomaFramework Tutorial Basics
PPT
Introduction to Alfresco Surf Platform
ODP
JavaEE Spring Seam
PPT
Ajax to the Moon
ODP
REST dojo Comet
PPTX
Building AOL's High Performance, Enterprise Wide Mail Application With Silver...
PPT
Dev381.Pp
PPT
PPT
PowerPoint
ODP
A Complete Tour of JSF 2
Apache Persistence Layers
Patni Hibernate
Javazone 2010-lift-framework-public
Intro to-html-backbone
Boston Computing Review - Java Server Pages
I Feel Pretty
Ldap Synchronization Connector @ 2011.RMLL
Dynamic Language Performance
Persisting Your Objects In The Database World @ AlphaCSP Professional OSS Con...
RomaFramework Tutorial Basics
Introduction to Alfresco Surf Platform
JavaEE Spring Seam
Ajax to the Moon
REST dojo Comet
Building AOL's High Performance, Enterprise Wide Mail Application With Silver...
Dev381.Pp
PowerPoint
A Complete Tour of JSF 2
Ad

Basic Hibernate Final

  • 1. Let’s Hibernate A brief introduction to Hibernate persistence service Rafael Coutinho (rcouto@br.ibm.com)
  • 2. Topics to be addressed Introduction to O/R Mapping What is Hibernate? Why Hibernate? Simple Example Features Tools Conclusions Questions
  • 3. Object/Relational mapping Motivation Development is Object Oriented Current main development languages Java, C++, C# Current main enterprises enviroments Websphere, JBoss, .Net Data needs to be persisted Current main DBMS are relational DB2, Oracle, MySQL.
  • 4. Object/Relational mapping Why Mapping? Objects needs to be persisted OODBMS didn’t sell Get the best of two worlds Relational: Information management Object: behavior Relational know-how and tools
  • 5. Object/Relational mapping Why a mapping engine? Even knowing how to map object to relational models Developers stay focused on the object model Isolates relational world from objects Have more robust structural mapping Data access layer creation is tedious Less interesting the work is, higher the risk of errors. Don’t re-invent the wheel !!!
  • 6. What is Hibernate? Opensource object / relational mapping tool Persistence service Non intrusive approach Uses XML mapping files Currently in version 3.1.3 (April 12 2006) Provides transparent persistence for POJO
  • 7. Why Hibernate? Open Source (LGPL) Doesn’t need to be so free Mature software driven by user requests Popular (15.000 downloads/month) No application server needed Java and .Net
  • 8. Architecture Overview XML Configuration Hibernate Database Application Persisted Objects XML Mappings
  • 10. Simple Example … <hibernate-mapping> <class name = &quot;vo.Address&quot; table = &quot;address&quot; > <id name = &quot;id&quot; column = &quot;id&quot; > <generator class = &quot;increment&quot; /> </id> <property name = &quot;address&quot; /> </class> </hibernate-mapping> Address.hbm.xml
  • 11. Simple Example … <hibernate-mapping> <class name = &quot;vo.Person&quot; table = &quot;person&quot; > <id name = &quot;id&quot; column = &quot;id&quot; > <generator class =&quot;increment&quot; /> </id> <one-to-one name = &quot;address&quot; class = &quot;vo.Address&quot; constrained = &quot;true&quot; foreign-key = &quot;addressid&quot; /> <property name = &quot;name&quot; type = &quot;string&quot; > <column name = &quot;name&quot; /> </property> </class> </hibernate-mapping> Person.hbm.xml
  • 12. Simple Example Hibernate.cfg.xml <hibernate-configuration> <session-factory name = &quot;Diagra&quot; > <property name = &quot;hibernate.connection.username&quot; > userid </property> <property name = &quot;hibernate.connection.password&quot; > wantmommy </property> <property name = &quot;hibernate.dialect&quot; > org.hibernate.dialect.MySQLDialect </property> <property name = &quot;hibernate.connection.url&quot; > jdbc:mysql:///test </property> <property name = &quot;hibernate.connection.driver_class&quot; > com.mysql.jdbc.Driver </property> <mapping resource = &quot;Address.hbm.xml&quot; /> <mapping resource = &quot;Person.hbm.xml&quot; /> </session-factory> </hibernate-configuration>
  • 13. Code sample Open Session: Session session = HibernateUtils.currentSession(); Transaction tx = session.beginTransaction(); Load: Person aPerson = (Person) session.load(Person. class , personId); Store: session.save(aPerson); List: List result = session.createQuery(&quot; from Person &quot;).list(); Close Session: tx.commit(); HibernateUtils.closeSession();
  • 14. Features Transparent persistence Any class can be a persistent class (POJO) Nonintrusive No interface or base classes No source or byte-code generation/processing But at runtime Extensible type system User-defined types Automatic dirty checking Detached object support Constraint transparency Foreign keys Needs no argument constructor Cascading
  • 15. Features Flexible O/R mapping Driven by XML mapping Supports some inheritance table per class hierarchy table per subclass table per concrete class implicit polymorphism Support one-to-many, one-to-one and many-to-many Bidirectional and unidirectional associations (dependent objects)
  • 16. Features Simple API Core for application Extension API for customization Object-Oriented query language HQL (Hibernate query language) SQLlike Support polymorphic queries Native SQL
  • 17. Features Operate in managed and non-managed environment J2EE Can be configured via JMX(Java Management Extension) Can be integrated with application server transactions via JTA (Java Transaction API) Stand-alone application .Net
  • 18. Features High performance Lazy Loading Fetching Strategies Retrieve associated objects Support for optimistic concurrency control Automatic versioning and time stamping
  • 19. Development Scenarios Allow four development scenarios Top Down Start with JavaBeans Bottom Up Start with relational schema Middle out Start with the mapping Meet-in-middle Start with both JavaBeans and relational schema
  • 20. Tools Hibernate Tools 3 UML Model XML/XMI POJO Java Source Mapping Metadata Database Schema AndroMDA XDoclet SchemaExport (hbm2ddl) Middlegen CodeGenerator (hbm2java)
  • 21. Tools Eclipse IDE Plugin Wizard to Create Configuration File XML Mapping Editor Context dependent code completion in the XML mapping editor
  • 22. Tools Hibernate Console View Entity Structure Prototype Queries Properties view
  • 23. Tools Reverse engineering Code generation Mapping files generation Configuration file generation
  • 24. Hands on Real life example
  • 25. Conclusion Data access layer creation is tedious Automatic mapping is needed Mature robust software Open source No application servers needed Development tools No silver bullet
  • 26. References Hibernate Website www.hibernate.org Tutorial www.gloegl.de/17.html Hibernate tools http://www.hibernate.org/255.html http://www.jboss.com/products/seam/SeamHBTools.html
  • 27. Questions & maybe answers E-mail: rcouto@br.ibm.com
  • 28. Questions I do have!!! Next presentation on May 10 th !

Editor's Notes

  • #17: Metadata API for dynamic mapping change????