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.  [ 1 post ] 
Author Message
 Post subject: Bulk Insert and composite id field
PostPosted: Thu Dec 06, 2007 1:25 pm 
Newbie

Joined: Thu Nov 29, 2007 4:59 am
Posts: 3
Location: Milano - Italy
Hibernate version: 3.1.3, 3.2.3, 3.2.5

I had a problem of this type: I want to use bulk insert to make insertion on objects.
I already have a map of the objects.
The table that I want to insert to has a composed primary key and one of the fieds of the key maps to another object, so my definition is something like this:
Code:
<hibernate-mapping>
    <class name="my.orm.mapping.TheTable" table="the_table">
        <composite-id name="id" class="my.orm.mapping.TheTableId">
            <key-many-to-one name="otherTable" class="my.orm.mapping.TheOtherTable">
                <column name="other_id" />
            </key-many-to-one>
            <key-property name="lineNum" type="java.lang.Integer">
                <column name="line_num" />
            </key-property>
        </composite-id>
        <many-to-one name="anotherOne" class="my.orm.mapping.AnotherOne" fetch="select">
            <column name="another_id" />
        </many-to-one>
        <property name="aField" type="java.lang.Double">
            <column name="a_field"/>
        </property>
    </class>
</hibernate-mapping>


When I try to write an HQL insert I cannot figure out way to refer to the fields of the key, I've tried
Code:
insert into my.orm.mapping.TheTable(id, ...) select new my.orm.mapping.TheTableId(v1,v2), ...

with error:
Code:
Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: unexpected token: ,
(I think this is related to HHH-2460 http://opensource.atlassian.com/projects/hibernate/browse/HHH-2460 problem as the select part alone give the same result),
so I tried to insert only the id :
Code:
insert into my.orm.mapping.TheTable(id) select new my.orm.mapping.TheTableId(v1,v2) from ...

and the result was:
Code:
Exception in thread "main" org.hibernate.QueryException: number of select types did not match those for insert
(but now the select alone works)

So I tried to explicitly map the key fields:
Code:
insert into (id.otherTable.id,id.lineNum) select v1,v2 from ...

and I had:
Code:
Exception in thread "main" org.hibernate.QueryException: could not resolve property:  of: my.orm.mapping.TheTable



I've found a workaround creating a new "plain" mapping (that is: no relations) with a false primary key (that is I said hibernate that the primary key was just one of the fields of the real primary key)
I use that mapping only for HQL insert so no java object will need a "real" primary key for binding with db row and the DB just knows what is the real key,
In this way the whole thing works:
I use this mapping
Code:
<hibernate-mapping>
    <class name="my.orm.mapping.TheTableFake" table="the_table">
        <id name="otherId" type="java.lang.Integer">
            <column name="other_id" />
        </id>
        <property name="lineNum" type="java.lang.Integer">
             <column name="line_num" />
        </property>
        <property name="anotherOneId" type="java.lang.Integer">
            <column name="another_id" />
        </many-to-one>
        <property name="aField" type="java.lang.Double">
            <column name="a_field"/>
        </property>
    </class>
</hibernate-mapping>


and this insert
Code:
insert into TheTableFake (otherId, lineNum,anotherOneId, aField) select 10,v2,...


note that I have inserted lines with a constant value in the fake id field: Hibernate do not argue and the thing is ok for the DB cos the real key is unique.


Anyone out there have found a better solution? (well: the solution, the above is only a workaround)

Thank you,
Marco


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

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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.