Some useful options in Class element in Hibernate
Hibernate mapping: the <Class> element: Some useful options
In hibernate mapping file the <class> element maps the domain object with corresponding entity in the database. It will be defined under <hibernate-mapping> tag. <Hibernate-mapping> tag also allows multiple <class> tags inside it. (i.e we can save multiple class mapping information in a single file).
A sample class element looks like below:
<class table=”user_tasks” >
Here we don’t discuss all the class element attributes, but some which I found very useful:
1. dynamic-insert, dynamic-update attributes: these 2 attributes are optional and defaults to false.
Hibernate actually generates SQL statements for each <class> element when startup. (The statements like select, update delete statements). The UPDATE statement generated will by default update all the columns. So when there is any change is single column by default all the columns will be updated. This can be problem if the table is big. Same with insert statements also.
We can turn this feature off and ask the hibernate not to generate dynamic insert and update statements. We can do this by specifying the dynamic-insert=”true” and dynamic-update=”true”.
<calss name=”contact” dynamic-insert=”true” dynamic-update=”true” />
If you want to do in using annotaions then, you can mark the class with following annotation (below @Entity annotation):
2. select-before-update (optional – defaults to false):
This deals with the detached objects. We fetch entity data using session or persistent context. The entities will be normal bean if they are not inside a persistent context or session. Session gets open and when you select, or fetch, the resulted entity object becomes attached. I.e any chages done to entity obect will also effect its database status. But once the persistencecontext is closed or the session is closed, the objects become detached. Changes made to it donot effect corresponding table. So to save the change done to detached entity, we need to reattach it and merge changes, by calling update().
Session sess = sessionFactory.getSession();
Transaction tx = sess.beginTransaction();
Contact.setAddress(“sg jasgd ajsdg “);
Here when update() called, the entity got attached again. This always issues an UPDATE statement. This happens irrespective of weather the object has changed after detaching or not. One way to avoid this UPDATE statement while reattaching is setting the select-before-update= “true”. If this is set, hibernates tries to determine if the UPDATE needed by executing the SELECT statement.
<class name=”contact” select-before-update= “true”> or
3. mutable (default value= true, optional) : by setting a classs mutable you can tell hibernate that there will be no UPDATE statement will ever be needed for the table. This prevents the Hibernate to do dirty checking(checking if any row has changed) and Hibernate can optimize itself.
<class name=”contact” mutable=”false”> or
You can mark a class by :