HIBERNATE/Java Overview of technology for Hibernate by محمد حسن کاظمی پوران Master : M.M.Nematollahi
Intro: About Hibernate - Open Source since, and now with JBoss (since late 2003) w/ commercial support & training. Founder: Gavin King Provides a layer for java to interact with the database. –Is technically a set of java-class libraries that you use to gain access to your database Hibernate caches database objects –& Hibernate services your Java program
Object-Relational Mapping It is a programming technique for converting object-type data of an object oriented programming language into database tables. Hibernate is used convert object data in JAVA to relational database tables.
What is Hibernate? It is open source object-relational mapping (ORM) for Java. Hibernate is responsible for making data persistent by storing it in a database.
Why Hibernate and not JDBC? JDBC maps Java classes to database tables (and from Java data types to SQL data types) Hibernate automatically generates the SQL queries. Hibernate provides data query and retrieval facilities and can significantly reduce development time as more time is required to manually handle data in SQL and JDBC. It makes an application portable to all SQL databases.
Architecture Hibernate sits between your code and the database Maps persistent objects to tables in the database
Hibernate Basics. SessionFactory – Is a factory which provides Session. Session – Single unit of work. Dirty checking Hibernate xml file – ‘.hbm’ It uses reflection
Hibernate Configuration A typical POJO class
Hibernate configuration Event.hbm.xml
hibenrate.hbm.xml
Singleton class to create SessionFactory object
Hibernate Configuration The hibernate.properties file –Used for hibernate configuration –Contains -Database configuration -Datasource configuration -Transaction configuration -Caching configuration -Connection pool configuration -Other settings
Hibernate Configuration... Contd The hibernate.cfg.xml –Alternative approach of configuration –Can be used as replacement of hibernate.properties –Picked up from classpath –Has got precedence on hibernate.properties file
Hibernate Configuration... Contd Non managed environment COM.ibm.db2.jdbc.app.DB2Driver jdbc:db2:SAMPLE db2admin 10 true net.sf.hibernate.dialect.DB2Dialect
Hibernate Configuration... Contd Managed environment (App Server) java:comp/env/jdbc/my_ds1 org.hibernate.transaction.CMTTransactionFactory org.hibernate.transaction.WebSphereExtendedJTATransactionLookup true org.hibernate.dialect.DB2Dialect
Hibernate Mapping The hibernate-mapping xml file
Use Hibernate in code Access the persistent objects just like you would any normal java POJO Use the dao operations (save, delete, etc.) to control the lifetimes of objects Take advantage of the Hibernate tools
Example App revisit Same basic structure –Alpha is the main class –Bravo handles user interaction –Charlie handles application logic –Delta handles data access New implementation of the Delta interface –UserString model class and hbm
Any questions? Hibernate: Spring ORM ce/orm.html