[Running Hibernate 3.3.1]
I have a little bit of a challenge. I have got a situation where when I try to do an insert in a table by saving an object i get like 3 select statement executed be4 the actual insert takes place. My object structure is simple. An Item property contains category property which is an Entity. My mapping:
Code:
<hibernate-mapping package="com.pmega.easybuy.domain" >
<class name="ItemPost" table="item_posts">
<cache usage="read-write" />
<id name="postId" type="string" column="post_id" length="15">
<generator class="assigned" />
</id>
<property name="itemPostEnabled" type="boolean"
column="post_enabled" not-null="true" />
<property name="reactivateCount" type="integer"
column="reactivate_count" />
<property name="datePosted" type="calendar">
<column name="date_posted" not-null="true" />
</property>
<property name="expiryDate" type="calendar" >
<column name="date_expired" not-null="true" />
</property>
<property name="posterUsername" type="string" column="username" not-null="true" />
<many-to-one name="item" column="item_id" unique="true" cascade="delete"
not-null="true" class="Item" update="false" >
</many-to-one>
<set name="comments" table="post_comments" cascade="delete">
<cache usage="read-write"/>
<key column="post_id" />
<many-to-many column="comment_id" unique="true"
class="Comment" />
</set>
</class>
</hibernate-mapping>
The above is for my Item class
Code:
<hibernate-mapping package="com.pmega.easybuy.domain">
<class name="Category" table="categories" dynamic-update="true" >
<cache usage="read-only" />
<id name="categoryId" type="long" column="category_id">
<generator class="identity" />
</id>
<property name="description" type="string" column="description" />
<property name="name" type="string" column="name"
not-null="true" unique="true" />
</class>
</hibernate-mapping>
The above is for my Category class
My DAO looks like :
Code:
@Transactional(readOnly = false)
public ItemPost save(ItemPost itemPost) {
Session session = getSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.save(itemPost.getItem());
session.save(itemPost);
tx.commit();
} catch (Exception e) {
// TODO: handle exception
tx.rollback();
e.printStackTrace();
}
return itemPost;
}
Actually, for the DAO, I have an ItemPost which has been initialized with an Item Object, and d Item obj has been initialized with a Category obj with id of a persisted Category. i.e The Category obj only has categoryId property set, other properties arent set.
Challenge:
What i notice is dat wen i try saving: i see at least three select category statements
Code:
Hibernate: select category0_.category_id as category1_6_, category0_.description as descript2_6_, category0_.name as name6_ from categories category0_
Hibernate: select category0_.category_id as category1_6_, category0_.description as descript2_6_, category0_.name as name6_ from categories category0_
Hibernate: select category0_.category_id as category1_6_, category0_.description as descript2_6_, category0_.name as name6_ from categories category0_
before the Item object is eventually inserted into the DB, and becomes visible. I feel this select statements would cause sm kind of performance drawback for a production app.
I sincerely hope smone with a cue responds to this, cos my last two posts havent still gotten any reply. I hope i av bin xpilicit enof. Further info about d app wld b provided