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? I did not modify the cat object at all in my transaction?
Code:
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?
session.getTransaction().commit();
if (result.size() == 1) {
return (Cat) result.get(0);
}
}
Here is my mapping file:
[code ]
<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>
[/code]