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.
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.
Nice article! Do you have any performance results (eg. query execution 'n' result fetch time) in such kind of dynamic mapping scenario?
ReplyDeleteEclipse 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!
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.
ReplyDeleteThere 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.
ReplyDeleteAmazing & 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.
Great thoughts you got there, believe I may possibly try just some of it throughout my daily life.
ReplyDeleteGreat 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
This is a nice post in an interesting line of content.Thanks for sharing this article, great way of bring this topic to discussion.
ReplyDeleteData Science Training in Chennai
Data Science training in kalyan nagar
Data science training in Bangalore
Data Science training in marathahalli
Data Science interview questions and answers
Data science training in bangalore
very useful information you share all from this post!!thanks lot!!
ReplyDeleteandroid training in chennai
android online training in chennai
android training in bangalore
android training in hyderabad
android Training in coimbatore
android training
android online training
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.
ReplyDeletejava 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
I have to search sites with relevant information on given topic and provide them to teacher our opinion and the article.
ReplyDeletekeep it up!!
data science training in chennai
data science training in tambaram
android training in chennai
android training in tambaram
devops training in chennai
devops training in tambaram
artificial intelligence training in chennai
artificial intelligence training in tambaram
ümraniye bosch klima servisi
ReplyDeletekartal arçelik klima servisi
ümraniye arçelik klima servisi
beykoz samsung klima servisi
beykoz mitsubishi klima servisi
üsküdar mitsubishi klima servisi
tuzla samsung klima servisi
çekmeköy arçelik klima servisi
ataşehir arçelik klima servisi