Wednesday, 14 September 2011

Dynamic mapping with hibernate

In this post I will share my experience in implementing dynamic mapping with hibernate. The idea is to write generic code that can be made to query data from any table with any no.of columns, ofcourse the actual table & column information is provided as a configuration.

1. Use dynamic map entity mode
This can be done by adding the following in your hibernate.cfg.xml:

<property name="default_entity_mode">dynamic-map</property>

2. Now provide the mapping, this is the only part where you provide the actual table & column details, just changing this will suffice if new columns are added or if you intend to reuse this code to query some other table.

Following is an example:

<class entity-name="TestEntity" table="ENTITY">
    <id name="id"  type="long" column="ID">
          <generator class="sequence"/>
    </id>

    <property name="title" column="TITLE" type="string"/>
    <property name="text" column="TEXT" type="string"/>
</class>

3. Code to query the table:

  SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

  Session s = sessionFactory.openSession();

   // Retrieve the data.
    String QUERY ="from TestEntity"; // Should use the entity name as defined in the hibernate mapping.
     Query query = s.createQuery(QUERY);
     HashMap<String, Object> temap = null;
     for(Iterator it=query.iterate();it.hasNext();){
       temap =(HashMap<String, Object>)it.next();  // for each row you get a map.
       // copy the map contents to your object or add to some list as needed.

     }


4. Similarly inserting can be done by creating a map with all the column/value pairs and then

Map teMap = new HashMap<String,Object>();

// fill the map with the column names & values.

s.save("TestEntity", teMap);


This can also be extended to generate the web client code based on the configuration, for example if you use extjs as the javascript framework to build the web ui, then the above can be exposed as restful service and then the table can be dynamically created based on the columns in the DB table.  I will explore that in my next post.




9 comments:

  1. Nice article! Do you have any performance results (eg. query execution 'n' result fetch time) in such kind of dynamic mapping scenario?
    Eclipse link as well support dynamic mapping (http://wiki.eclipse.org/EclipseLink/Examples/JPA/Dynamic#How_to_use_Dynamic_EclipseLink_JPA). I wonder why such a feature is missing from the JPA specification itself!

    ReplyDelete
  2. No performance results as of now, but I would guess that will not be as good as a statically mapped case. Btw, if you have any results, do share them.

    There are quite many vendor specific implementations to achieve this, but hibernate is the most popular ORM today, hence the post is restricted to it.

    The need to have a dynamic model itself breaks the concept of ORM to some extent. JPA provides a mapping of the Relational entities to java objects, now if the relational entity itself is a variable then mapping cannot be static.

    However there are still cases wherein such flexibility is required, so yes it could be a good candidate for the next revision of JPA.

    ReplyDelete


  3. Amazing & Great informative blog,it gives very useful practical information to developer like me. Besides that Wisen has established as Best Hibernate Training in Chennai . or learn thru Online Training mode Hibernate Online Training | Java EE Online Training. Nowadays Hibernate ORM has tons of job opportunities on various vertical industry.

    ReplyDelete
  4. Great thoughts you got there, believe I may possibly try just some of it throughout my daily life.
    Great thoughts you got there, believe I may possibly try just some of it throughout my daily life.
    Best Devops online Training
    Online DevOps Certification Course - Gangboard
    Best Devops Training institute in Chennai

    ReplyDelete
  5. Hello I am so delighted I found your blog, I really found you by mistake, while I was looking on Yahoo for something else, anyways I am here now and would just like to say thanks for a tremendous post. Please do keep up the great work.
    java training in chennai

    java training in omr

    aws training in chennai

    aws training in omr

    python training in chennai

    python training in omr

    selenium training in chennai

    selenium training in omr



    ReplyDelete