Hibernate is an open-source object-relational mapping tool that provides transparent persistence for Java objects, allowing developers to focus on objects rather than relational databases. It automates the mapping between objects and relational databases so that data access code is less tedious to write and maintain. The document discusses Hibernate's features such as transparent persistence, query language, performance optimizations, and development tools.
1 of 28
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 = "vo.Address" table = "address" > <id name = "id" column = "id" > <generator class = "increment" /> </id> <property name = "address" /> </class> </hibernate-mapping> Address.hbm.xml
11. Simple Example … <hibernate-mapping> <class name = "vo.Person" table = "person" > <id name = "id" column = "id" > <generator class ="increment" /> </id> <one-to-one name = "address" class = "vo.Address" constrained = "true" foreign-key = "addressid" /> <property name = "name" type = "string" > <column name = "name" /> </property> </class> </hibernate-mapping> Person.hbm.xml
12. Simple Example Hibernate.cfg.xml <hibernate-configuration> <session-factory name = "Diagra" > <property name = "hibernate.connection.username" > userid </property> <property name = "hibernate.connection.password" > wantmommy </property> <property name = "hibernate.dialect" > org.hibernate.dialect.MySQLDialect </property> <property name = "hibernate.connection.url" > jdbc:mysql:///test </property> <property name = "hibernate.connection.driver_class" > com.mysql.jdbc.Driver </property> <mapping resource = "Address.hbm.xml" /> <mapping resource = "Person.hbm.xml" /> </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(" from Person ").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
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