Basic Hibernate Final

1,467 views

Published on

0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,467
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
0
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide
  • Metadata API for dynamic mapping change????
  • Basic Hibernate Final

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

    ×