I have the following code which just query an object from the database.
But when I turn on hibernate sql output. it causes 2 sqls:
1. query
2. update
My question is why hibernate generate the 2nd update command? I did not modify the cat object at all in my transaction?
private static Cat getCat(
String name, Home home) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
// cause hibernate to create 1 query. This is expected
Query query = session
.createQuery(
"from Cat as cat where cat.name= :name and cat.home.id = :homeid");
List result = query.setString("name", name).setLong("homeid",
home.getId().longValue()).list();
// why hibernate caused a sql update query here? I did not modify
// any of the persistent object
session.getTransaction().commit();
if (result.size() == 1) {
return (Cat) result.get(0);
}
}
Here is my mapping file:
<class name="com.testproject.model.Cat" table="BUILDS"
lazy="true">
<id name="id" column="CAT_ID">
<generator class="native" />
</id>
<version name="version" column="version"
type="java.lang.Integer" />
<property name="name" not-null="true" />
<many-to-one name="home" column="HOME_ID"
class="com.testproject.model.Home" not-null="true" />
</class>
Is there a way to disable it? I would like to minimize Hibernate sql if possible. The object that I get from the database should be 'clean' instead of 'dirty' so hibernate shouldn't need to update it. Right?
Thank you.