ºÝºÝߣ

ºÝºÝߣShare 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

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????