Thursday, July 9, 2009

Using Dynamic model in Hibernate

When using Hibernate we are not forced to have POJOs alawsys for representing persistent entities. Hibernate also supports Dynamic models like Maps and DOM4J trees.

In plain english what this means is - we need to have only a mapping file (.hbm.xml) and no pojo class is required. However be careful when using this feature , as it may undergo change in the future releases.

Here we will see how to use the Hashmap for dynamic model.

Our table is
TEST_EMPLOYEE
+---------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| id | varchar(255) | NO | PRI | | |
| name | varchar(255) | YES | | NULL | |
| emailId | varchar(255) | YES | | NULL | |
+---------+--------------+------+-----+---------+-------+

Lets have a look at the curresponding mapping file -


<hibernate-mapping>
<class entity-name="com.check.Employee" table="TEST_EMPLOYEE" polymorphism="implicit" lazy="false">

<id name="id" type="string">
<column name="id" null="true">
</column></id>

<property name="name" column="name" type="string">
<property name="emailId" column="emailId" type="string">

</property></property></class>
</hibernate-mapping>

Check the use of "entity-name", this is required for dynamic model. We need not specify any class name here.

Sample code for CRUD operations.
Here HibernateHandler is a utility class used for managing sessionFactory.











01 public void createEmp(String id) {

02 Map emp = new HashMap();

03 emp.put("id", id);

04 emp.put("name", "emp_" + id);

05 emp.put("emailId", "emailId_" + id);

06

07 Session sess = HibernateHandler.getSession();

08 HibernateHandler.beginTransaction();

09 sess.save("com.check.Employee", emp);

10 HibernateHandler.commitTransaction();

11 HibernateHandler.closeSession();

12 }

13 public void readEmpUsingId(String id) {

14 Session sess = HibernateHandler.getSession();

15 Map empRead = (Map) sess.load("com.check.Employee", id);

16 System.out.println(empRead);

17 HibernateHandler.closeSession();

18 }

19 public void readUsingQuery(String mailId) {

20 String hsql = "from com.check.Employee where emailId = :emailId";

21 Session sess = HibernateHandler.getSession();

22 HibernateHandler.printStats();

23 Query q = sess.createQuery(hsql);

24 q.setString("emailId", mailId);

25 List data = q.list();

26 Map emp = (Map) data.get(0);

27 System.out.println(emp);

28 HibernateHandler.closeSession();

29 HibernateHandler.printStats();

30 }

31 public void updateEmpSimpleId(String id) {

32 Session sess = HibernateHandler.getSession();

33 HibernateHandler.beginTransaction();

34 Map empRead = (Map) sess.load("com.check.Employee", id);

35 System.out.println(empRead);

36 empRead.put("emailId", empRead.get("emailId")+"_updated");

37 System.out.println(empRead);

38 HibernateHandler.commitTransaction();

39 HibernateHandler.closeSession();

40 }

41 public void readEmpUsingObject(String id) {

42 HashMap emp = new HashMap();

43 emp.put("id", id);

44 emp.put("name", "emp_" + id);

45

46 Session sess = HibernateHandler.getSession();

47 Map empRead = (Map) sess.load("com.check.EmployeeCompo", emp);

48 System.out.println(empRead);

49 HibernateHandler.closeSession();

50 }





Here "readEmpUsingObject" is used for reading object with composit id.
Curresponding mapping file will contain -

<composite-id >
<key-property name="id" type="string" column="id" />
<key-property name="name" type="string" column="name" />
</composite-id>