-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Select, Insert, Updates
PostPosted: Fri Nov 10, 2006 2:00 am 
Newbie

Joined: Fri Nov 10, 2006 1:14 am
Posts: 2
Hi Everyone:

I am using Spring 2.0 and Hibernate 3.1. I am seeing "Select" statements when inserting a row into the database using Hibernate. After the insert, I see an update to the database. Is this normal? If not what can I do to tune it so it just does an insert.

Any suggestions to tune Hibernate so that it does not do a "Select" and "Update" would be appreciated. The batch stuff gives me headaches, when it lose the Session and throws an exception when it can not perform the batch updates.

In my Spring DAO I am using the HibernateTemplate.

import org.springframework.orm.hibernate3.HibernateTemplate;

public void insertCollection(Collection collection){
this.hibernateTemplate.saveOrUpdateAll(collection);
}

Spring config stuff

<bean id="dbSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="myDataSource"/>
</property>
<property name="mappingResources">
<list>
<value>asset.hbm.xml</value>
<value>assetmungo.hbm.xml</value>
<value>assetgroup.hbm.xml</value>
<value>assetAttributeMap.hbm.xml</value>
<value>attribute.hbm.xml</value>
<value>parentAttributeMap.hbm.xml</value>
<value>parentMungo.hbm.xml</value>
<value>assetPublication.hbm.xml</value>
<value>parentAsset.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<!--
Oracle (any version)
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>

Oracle 9i/10g
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
-->

<prop key="hibernate.dialect">org.hibernate.dialect.SybaseAnywhereDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.c3p0.min.size">5</prop>
<prop key="hibernate.c3p0.max.size">20</prop>
<prop key="hibernate.c3p0.timeout">300</prop>
<prop key="hibernate.c3p0.max_statements">50</prop>
<prop key="hibernate.c3p0.idle_test_period">300</prop>
<prop key="hibernate.jdbc.batch_size">200</prop>
</props>
</property>
</bean>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.xxxx.xxxx.ingestion">
<class name="com.xxxx.xxxx.ingestion.mungo.AssetMungo" table="Citi_Mungo">
<id name="id" column="id" type="long"/>
<property name="csAssetGroupId" column="cs_assetgroupid" type="long"/>
<property name="csAttrId" column="cs_attrid" type="long"/>
<property name="floatvalue" column="floatvalue" type="float"/>
<property name="intvalue" column="intvalue" type="long"/>
<property name="stringvalue" column="stringvalue" type="string"/>
<many-to-one name="asset" column="cs_ownerid" class="CitiAsset"/>
</class>
</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.xxxx.xxxx.ingestion">
<class name="CitiAsset" table="Citi">
<id name="id" column="id" type="long"/>
<property name="flextemplateid" column="flextemplateid" type="long"/>
<property name="createdBy" column="createdby" type="string"/>
<property name="status" column="status" type="string"/>
<property name="name" column="name" type="string"/>
<property name="description" column="description" type="string"/>
<property name="updatedby" column="updatedby" type="string"/>
<property name="updatedDate" column="updateddate" type="timestamp"/>
<property name="createDate" column="createddate" type="timestamp"/>
<!-- Here we map the attributes to the Citi_Mungo-->
<set name="attributes"
table="Citi_Mungo"
cascade="save-update"
inverse="true">
<key column="cs_ownerid"/>
<one-to-many class="com.xxxx.xxxx.ingestion.mungo.AssetMungo"/>
</set>

<!-- Map to the Citi_AMap table and let Hibernate handle the transitive persistence -->
<set name="attributeMap"
table="Citi_AMap"
cascade="save-update"
inverse="true">
<key column="ownerid"/>
<one-to-many class="com.xxxx.xxxx.ingestion.AssetAttributeMap"/>
</set>

<set name="assetGroup" table="Citi_P_Group" cascade="save-update" inverse="true">
<key column="childid"/>
<one-to-many class="com.xxxx.xxxx.ingestion.AssetGroup"/>
</set>
<!-- Here we set the one-to-one relationship with the AssetPublication table -->
<one-to-one name="assetPub" class="com.xxxx.xxxx.ingestion.AssetPublication" cascade="save-update"/>
</class>
</hibernate-mapping>

Hibernate:
select
assetmungo_.id,
assetmungo_.cs_ownerid as cs2_1_,
assetmungo_.cs_assetgroupid as cs3_1_,
assetmungo_.cs_attrid as cs4_1_,
assetmungo_.floatvalue as floatvalue1_,
assetmungo_.intvalue as intvalue1_,
assetmungo_.stringvalue as stringva7_1_
from
Citi_Mungo assetmungo_
where
assetmungo_.id=?
Hibernate:
insert
into
Citi_Mungo
(cs_ownerid, cs_assetgroupid, cs_attrid, floatvalue, intvalue, stringvalue, id)
values
(?, ?, ?, ?, ?, ?, ?)
Hibernate:
update
Citi_Mungo
set
cs_ownerid=?,
cs_assetgroupid=?,
cs_attrid=?,
floatvalue=?,
intvalue=?,
stringvalue=?
where
id=?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 10, 2006 9:53 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Since you do not have a generator for your identifiers, Hibernate will use an "assigned" generator. When using an assigned generator, Hibernate will not set the unsaved-value property and it will be forced to go to the database (SELECT) to determine if the object is transient (needs an INSERT) or detached (needs an UPDATE). A couple of solutions:

  • Add a version or timestamp field to each table
  • Use a different id generator, I suggest sequence since you are on Oracle
  • Redefine Interceptor.isUnsaved()


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 10, 2006 11:56 am 
Newbie

Joined: Tue Aug 15, 2006 10:02 am
Posts: 9
Ananasi wrote:
  • Use a different id generator, I suggest sequence since you are on Oracle


You can modify your configuration file to use the oracle sequence as follows
Code:
        <id name="id" column="ID" >
            <generator class="sequence">
                <param name="sequence">ASSET_ID_SEQ</param>
            </generator>
        </id>


Note I am assuming the "ASSET_ID_SEQ" already exist in your database.

For the hibernate version that you are using you do not need to declare the unsaved-value.


Top
 Profile  
 
 Post subject: Thank you all
PostPosted: Fri Nov 10, 2006 5:06 pm 
Newbie

Joined: Fri Nov 10, 2006 1:14 am
Posts: 2
Hi Everyone:

Thank you all for all the replies. It helps alot.

Regards,

Southin


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.