1

I am using hibernate 3.5.0 final with spring.In this I want to save data to a table which has a composite key associated from three other tables.I have used hibernateTemplate.save().But when I see the logs it clearly shows that there is a select happening before every insert.I am not able to identify the reason still.

Please help!

Mapping:

<hibernate-mapping>
    <class name="class1" table="EVENT_ASSET_DISPOSITION">
        <composite-id name="id" class="Idclass">
            <key-property name="pk1" type="java.math.BigInteger">
                <column name="PK_1" precision="38" scale="0"  not-null="true"/>
            </key-property>
            <key-property name="pk2" type="long">
                <column name="PK_2" not-null="true" />
            </key-property>
            <key-property name="pk3" type="long">
                <column name="PK_3" precision="38" scale="0" not-null="true"/>
            </key-property>
        </composite-id>
        <property name="column1" type="java.math.BigDecimal" generated="insert">
            <column name="COLUMN_1" precision="38" scale="0" />
        </property> 
</hibernate-mapping>

SQL:-

[STDOUT] (pool-14-thread-1) insert into TABLE_1 (COLUMN_1,PK_1,PK_2,PK_3) values (?, ?, ?, ?,) 2011-02-14 08:28:30,312 INFO [STDOUT] (pool-14-thread-1) Hibernate: select table1_.COLUMN_1 as COLUMN1_280_ from TABLE_1 as table1_ where table1_.PK_1=? and table1_.PK_2=? and table1_.PK_3=?

Cheers, Dwarak

2 Answers2

0

One mechanism that hibernate uses to decide if an object is transient or not is versioning

if you add a version column to your mapping/object it should solve the problem

<version name="version" column="version" type="integer" unsaved-value="undefined"/> 

EDIT 1:

http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-declaration-version

@see section 5.1.9

fmucar
  • 14,361
  • 2
  • 45
  • 50
  • Should this have a column called version in the Table???I am getting column not found exception. – Dwarakanath Jagadeesan Feb 15 '11 at 14:23
  • http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-declaration-version see section 5.1.9 – fmucar Feb 15 '11 at 14:58
  • is this normal behavior? why would hibernate do a select before save? do we must add versioning all the time in order to avoid it? – Julia Apr 08 '11 at 08:45
  • as far as i know; yes, there is no other way for hibernate to distinguish the 2 objects. – fmucar Apr 13 '11 at 21:43
0

The log shows that select is issued after insert. It's caused by the fact that column1 is declared as generated="insert", therefore Hibernate need to fetch generated values of that column after insert.

See also:

axtavt
  • 239,438
  • 41
  • 511
  • 482