In this post you will know how hibernate framework works. Hibernate is an ORM(Object-Relational-Mapping) framework in which like all frameworks we have some layers which coordinate with each other.
We have java classes and database tables and our aim is to build a connection between these two. For this Hibernate uses ORM technique in which it maps each java class entity to table entity present in database. Thus Hibernate is the architechture which is present between java objects and database.
The main part of Hibernate is its configuration. There are two parts of that configuration one that is for making the connection and other which is for mapping. This first part of configuration is present in ..cfg.xml file. And other part is present in ...hbm.xml file. There is only one cfg file but if we have three java classes then we have three mapping or hbm file.
Using these property file Hibernate creates a sessionFactory which is used to create sessions. This session is the entry path into database and it is shared among all classes of application. Hibernate provides us many methods like save(), persist(), saveUpdate() which are used to make changes in the database table. These operations are done inside a transaction which maintains databse consistency. In transaction either our action is committed or rolledback if unsuccessful.
In Hibernate there are three phases of a Hibernate mapped object. When we make object of a POJO or java class then it is in its transient stage which means object is created but it is not in any database. Then we call methods of Hibernate(save(),persist() etc.) to change it to persistent state which means it is associated with the session now. A persistent object can be detached which means it is no longer present in session now. So these are three phases of an object in Hibernate.
Also there are many advantages related with Hibernate which make it quite popular with developers. Hibernate has a well layered architechture through which it becomes quite easier for any developer to operate. Also Hibernate can be used easily with any RDBMS.
Relations between objects that are more complex than "a reference" are basically implemented in the way you'd use in a pure relational model: a table for the relation, with various requirements where appropriate.
The key to the whole business is the mapping: Which field and which class matches which column and which table.
While simple types can be deduced from either the object model or the DB schema, relationships between objects must be done explicitly (Early versions of Hibernate would require an XML file describing it; Currently annotations are common). Today, most of the mapping is usually performed at run-time based on reflection, but in some cases the mapping is statically generated ahead of time.
ORMs will usually either generate classes from a DB schema, or the other way around, saving you the trouble of specifying everything twice.
The ORM framework adds a load and save methods - depending on the specific case, it might be added to the actual objects, or be a static on some central location, or even be called implicitly, although that's probably not going to end well.
To retrieve an object, you'd call the load method with some specification - could be the id of an object, or a search query, or maybe "everything", which will then be translated into SQL, and the results will be deserialized into the appropriate objects.
The search query almost always looks like a SQL WHERE clause, and will often be passed verbatim to the engine. JOINs will be added to the query as needed, based on the mapping schema.
Since ORMs are most usefull when there's just too much data to store in the computer memory, objects might hold references to objects that are not available; When trying to follow them, you might be required to explicitly load them, or they might be loaded explicitly.
With non-relational DBs getting popular this days, it's worth mentioning than ORM are only relevant to mapping Object-Oriented data to Relational model DBs.
No comments:
Post a Comment