Pages

Showing posts with label orm. Show all posts
Showing posts with label orm. Show all posts

Wednesday, November 21, 2012

Save or Update vs Merge operations in hibernate

Following is from the hibernate documentation.

saveOrUpdate() does the following:
if the object is already persistent in this session, do nothing
if another object associated with the session has the same identifier, throw an exception
if the object has no identifier property, save() it
if the object's identifier has the value assigned to a newly instantiated object, save() it
if the object is versioned (by a <version> or <timestamp>), and the version property value is the same value assigned to a newly instantiated object, save() it
otherwise update()

merge() is very different:
if there is a persistent instance with the same identifier currently associated with the session, copy the state of the given object onto the persistent instance
if there is no persistent instance currently associated with the session, try to load it from the database, or create a new persistent instance
the persistent instance is returned
the given instance does not become associated with the session, it remains detached

You should use merge when there is an  object that was detached from the session and a persistence object of it is currently associated with the session.

Wednesday, September 12, 2012

Hibernate configuration for mysql in hibernate 4.0

this is the normal configuration of a hibernate.cfg.xml in hibernate 4

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>

<!-- Database connection settings. hibernatedb is my database name. you can choose anything you want -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernatedb</property>
<property name="connection.username">root</property>
<property name="connection.password">pass</property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

Sunday, September 9, 2012

What does hibernate do?

Hibernate is an Object relational mapping tool. We normally say it as ORM

say we have an Item object now we have these member variables.

  1. itemPrice
  2. itemName
  3. itemId
Now we need to have a table for this entity. Say the table name is Item then the following columns should be there
  1. item_price
  2. item_name
  3. item_id
If we use normal method

We need to write boiler plate codes for the classes and need to write code to map models with databases.

Another problem is mapping relationships. If we have a Computer class and a MotherBoard class then we need to map MotherBoard object to the computer class as a member variable of computer class.

Also data type conversion has to be done. Say we have a boolean and database stores it as integers. This conversion is also a problem.

It is hard to manage code changes. say we have change in object status. Then we have to write queries for each of the column value  changes. 

what does hibernate do?

Well in simple hibernate removes the inadequacies mentioned above. In simple what hibernate does is it removes the gap between the object and the database. we can persist the object into a database without much effort.