Your SlideShare is downloading. ×
Hibernate
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Hibernate

734
views

Published on

Hibernate is a framework of Java technology.

Hibernate is a framework of Java technology.

Published in: Education, Technology

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
734
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
49
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Hibernate
  • 2. Content covered • Introduction to Hibernate • Hibernate Architecture • O/R Mapping in Hibernate
  • 3. Hibernate Architecture
  • 4. Configuration (org.hibernate.cfg.Configuration) • The Configuration object, as seen in the example code, is the first Hibernate object you use. It represents a configuration or properties file for Hibernate. The Configuration object is usually created once during application initialization. • The Configuration object reads and establishes the properties Hibernate uses to get connected to a database and configure itself for work. A Configuration object is used to create a SessionFactory and then typically is discarded.
  • 5. Example <?xml version="1.0" encoding="UTF-8"?> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class"> org.hsqldb.jdbcDriver </property> <property name="hibernate.connection.url">jdbc:hsqldb:hsql://localhost </property> <property name="hibernate.connection.username">sa</property> <property name="connection.password"></property> <property name="connection.pool_size">1</property> <property name="hibernate.dialect"> org.hibernate.dialect.HSQLDialect </property> <mapping resource="com/vaannila/student/Student.hbm.xml"/> <mapping resource="com/vaannila/student/Address.hbm.xml"/> </session-factory> </hibernate-configuration>
  • 6. SessionFactory (org.hibernate.SessionFactory) • Created from a Configuration object, and as its name implies it is a factory for Session objects. • An expensive object to create. It, like the Configuration object, is usually created during application start up. However, unlike the Configuration object, It should be created once and kept for later use.
  • 7. SessionFactory • SessionFactory object, – used by all the threads of an application. It is a thread safe object. – One SessionFactory object is created per database. – Multiple SessionFactory objects (each requiring a separate Configuration) are created when connecting to multiple databases.
  • 8. Hibernate Flow of Work
  • 9. Session (org.hibernate.Session) • Provide the main interface to accomplish work with the database. Persistent objects are saved and retrieved through a Session object. • Lightweight & inexpensive to create, does the work of getting a physical connection to the database. • Not thread safe. Therefore, session objects should not be kept open for a long time. • Applications create and destroy these as needed. • When modifications are made to the database, Session objects create a Transaction object.
  • 10. Transaction (org.hibernate.Transaction) • Represents a unit of work with the database (and potentially other systems). Transactions in Hibernate are handled by an underlying transaction manager and transaction (from JDBC, JTA or CORBA). • Abstracts the developer from having to deal with the underlying transaction manager/transaction. • Transaction objects should be kept open for as short of time as possible.
  • 11. Example Sessionsession= SessionFactory.newSession(); Transaction tx = session.beginTransaction( ); tx.commit(); tx.rollback(); session.close()
  • 12. Query & Criteria • Both objects are used to retrieve (and recreate) persistent objects. • Query objects use SQL or Hibernate Query Language (HQL) string to retrieve data from the database & create objects. • Criteria enables developer to create and execute the object-oriented criteria queries.
  • 13. Hibernate O/R Mapping • ORM is a technique to map object- oriented data with the relational data. • Used to convert datatype supported in object-oriented programming language to a datatype to a data type supported by a database. (if mapping is not provided, exception is thrown while transferring an object oriented data to the database)
  • 14. Hibernate O/R Mapping • Mapping in the format that relate – Classes with tables. – Properties with columns – SQL types with Java types. • O/R Mapping can be written for any database that can be oracle, DB2, MySql, Sybase.
  • 15. Hibernate O/R Mapping • xml file XML mapping documents allows you to: – Bridge the gap between object-oriented systems and relational databases – Define the object-relational mapping – Help to generate and export database table
  • 16. Associations strategies • Mainly 3 Association strategies available in O/R mapping: • 1. One-2-one • 2. One-2-many • 3. Many-2-one • 4. Many-2-many
  • 17. Hibernate Mapping files <?xml version="1.0"?> <hibernate-mapping> <class name="com.vaannila.student.Student" table="STUDENT"> <id name="studentId" type="long" column="STUDENT_ID"> <generator class="native“/></id> <property name="studentName" type="string" not-null="true" length="100" column="STUDENT_NAME" /> <many-to-one name="studentAddress" class="com.vaannila.student.Address" column="STUDENT_ADDRESS" not-null="true" cascade="all" unique="true" /> </class> </hibernate-mapping>