-->
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.  [ 5 posts ] 
Author Message
 Post subject: Not inserting association column based on composite keys
PostPosted: Tue Jan 17, 2006 6:08 am 
Newbie

Joined: Sun Jul 31, 2005 11:12 pm
Posts: 8
I'm trying to add a sheet with an association to a 'guido'. I call setGuido(guido) on the sheet object then I try to save the sheet, the columns of the sheet are inserted except for the guido_id column. Why isn't the guido_id column inserted(please see generated SQL).

Hibernate version:
3.1

Mapping documents:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jan 16, 2006 10:45:02 PM by Hibernate Tools 3.1.0 beta1JBIDERC2 -->
<hibernate-mapping>
    <class name="com.allentown.db.Guido" table="guido" schema="public">
        <composite-id name="id" class="com.allentown.db.GuidoId">
            <key-property name="acId" type="integer">
                <column name="ac_id" />
            </key-property>
            <key-property name="id" type="integer">
                <column name="id" />
            </key-property>
        </composite-id>
        <many-to-one name="accountUser" class="com.allentown.db.AccountUser" update="false" insert="false" fetch="select">
            <column name="ac_id" not-null="true" />
            <column name="account_user_id" />
        </many-to-one>
        <many-to-one name="address" class="com.allentown.db.Address" fetch="select">
            <column name="address_id" />
        </many-to-one>
        <many-to-one name="account" class="com.allentown.db.Account" update="false" insert="false" fetch="select">
            <column name="ac_id" not-null="true" />
        </many-to-one>
        <property name="contactName" type="string">
            <column name="contact_name" length="100" />
        </property>
        <property name="companyName" type="string">
            <column name="company_name" length="100" />
        </property>
        <property name="cut" type="big_decimal">
            <column name="cut" precision="10" scale="5" />
        </property>
        <property name="tz" type="integer">
            <column name="tz" />
        </property>
        <set name="sheetMoneies" inverse="true">
            <key>
                <column name="ac_id" not-null="true" />
                <column name="guido_id" />
            </key>
            <one-to-many class="com.allentown.db.SheetMoney" />
        </set>
        <set name="sheets" inverse="true">
            <key>
                <column name="ac_id" not-null="true" />
                <column name="guido_id" />
            </key>
            <one-to-many class="com.allentown.db.Sheet" />
        </set>
    </class>
</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jan 16, 2006 10:45:02 PM by Hibernate Tools 3.1.0 beta1JBIDERC2 -->
<hibernate-mapping>
    <class name="com.allentown.db.Sheet" table="sheet" schema="public">
        <composite-id name="id" class="com.allentown.db.SheetId">
            <key-property name="acId" type="integer">
                <column name="ac_id" />
            </key-property>
            <key-property name="id" type="integer">
                <column name="id" />
            </key-property>
        </composite-id>
        <many-to-one name="guido" class="com.allentown.db.Guido" update="false" insert="false" fetch="select">
            <column name="ac_id" not-null="true" />
            <column name="guido_id" />
        </many-to-one>
        <many-to-one name="sheettype" class="com.allentown.db.Sheettype" fetch="select">
            <column name="ad_type_id" />
        </many-to-one>
        <many-to-one name="account" class="com.allentown.db.Account" update="false" insert="false" fetch="select">
            <column name="ac_id" not-null="true" />
        </many-to-one>
        <property name="makeGood" type="integer">
            <column name="make_good" />
        </property>
        <property name="name" type="string">
            <column name="name" length="100" />
        </property>
        <property name="expireDate" type="timestamp">
            <column name="expire_date" length="8" />
        </property>
        <property name="status" type="string">
            <column name="status" length="10" />
        </property>
        <set name="sheetMoneies" inverse="true">
            <key>
                <column name="ac_id" not-null="true" />
                <column name="sheet_id" not-null="true" />
            </key>
            <one-to-many class="com.allentown.db.SheetMoney" />
        </set>
        <set name="activityLogsForActivityLogAcIdFkey2" inverse="true">
            <key>
                <column name="ac_id" not-null="true" />
                <column name="site_id" not-null="true" />
            </key>
            <one-to-many class="com.allentown.db.ActivityLog" />
        </set>
        <set name="totalLogsForTotalLogAcIdFkey2" inverse="true">
            <key>
                <column name="ac_id" not-null="true" />
                <column name="site_id" not-null="true" />
            </key>
            <one-to-many class="com.allentown.db.TotalLog" />
        </set>
        <set name="activityLogsForActivityLogAcIdFkey1" inverse="true">
            <key>
                <column name="ac_id" not-null="true" />
                <column name="sheet_id" not-null="true" />
            </key>
            <one-to-many class="com.allentown.db.ActivityLog" />
        </set>
        <set name="baskets" inverse="true">
            <key>
                <column name="ac_id" not-null="true" />
                <column name="sheet_id" />
            </key>
            <one-to-many class="com.allentown.db.Basket" />
        </set>
    </class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
Code:
      Session s = HibernateUtil.currentSession();
      Guido a = (Guido) s.load(Guido.class,new GuidoId(1,1));
      Account ac = (Account)s.load(Account.class,1);
      newSheet.setId(new SheetId(1,1));
      newSheet.setGuido(a);
      newSheet.setAccount(ac);
      newSheet.setSheettype((Sheettype)s.load(Sheettype.class,0));
      newSheet.setStatus("blah");
      newSheet.setName("name");
      s.save(newSheet);

Later I commit the transaction
Full stack trace of any exception that occurs:

Name and version of the database you are using:
Postgresql 8.0.5

The generated SQL (show_sql=true):
Hibernate: insert into public.sheet (ad_type_id, make_good, name, expire_date, status, ac_id, id) values (?, ?, ?, ?, ?, ?, ?)

(guido_id column is not being inserted)
Debug level Hibernate log excerpt:
Code:
Filter: opening session
01:44:02,218 DEBUG JDBCTransaction:54 - begin
01:44:02,219 DEBUG ConnectionManager:313 - opening JDBC connection
01:44:02,219 DEBUG DriverManagerConnectionProvider:93 - total checked-out connections: 1
01:44:02,220 DEBUG DriverManagerConnectionProvider:109 - opening new JDBC connection
01:44:02,319 DEBUG DriverManagerConnectionProvider:115 - created connection to: jdbc:postgresql://127.0.0.1/ads, Isolation Level: 2
01:44:02,320 DEBUG JDBCTransaction:59 - current autocommit status: false
01:44:02,325 DEBUG JDBCContext:202 - after transaction begin
In security filter for guido
01:44:02,325 DEBUG AbstractBatcher:311 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
01:44:02,326 DEBUG SQL:346 - select accountuse0_.ac_id as ac1_6_0_, accountuse0_.id as id6_0_, accountuse0_.username as username6_0_, accountuse0_.password as password6_0_, accountuse0_.tz as tz6_0_ from public.account_user accountuse0_ where accountuse0_.ac_id=? and accountuse0_.id=?
Hibernate: select accountuse0_.ac_id as ac1_6_0_, accountuse0_.id as id6_0_, accountuse0_.username as username6_0_, accountuse0_.password as password6_0_, accountuse0_.tz as tz6_0_ from public.account_user accountuse0_ where accountuse0_.ac_id=? and accountuse0_.id=?
01:44:02,327 DEBUG AbstractBatcher:424 - preparing statement
01:44:02,327 DEBUG IntegerType:79 - binding '1' to parameter: 1
01:44:02,328 DEBUG IntegerType:79 - binding '1' to parameter: 2
01:44:02,347 DEBUG AbstractBatcher:327 - about to open ResultSet (open ResultSets: 0, globally: 0)
01:44:02,357 DEBUG IntegerType:123 - returning '1' as column: ac1_6_0_
01:44:02,357 DEBUG StringType:123 - returning 'JBob' as column: username6_0_
01:44:02,358 DEBUG StringType:123 - returning 'p' as column: password6_0_
01:44:02,359 DEBUG IntegerType:123 - returning '1' as column: tz6_0_
01:44:02,359 DEBUG AbstractBatcher:334 - about to close ResultSet (open ResultSets: 1, globally: 1)
01:44:02,360 DEBUG AbstractBatcher:319 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
01:44:02,361 DEBUG AbstractBatcher:470 - closing statement
01:44:02,362 DEBUG AbstractBatcher:311 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
01:44:02,363 DEBUG SQL:346 - select guido0_.ac_id as ac1_8_0_, guido0_.id as id8_0_, guido0_.account_user_id as account3_8_0_, guido0_.address_id as address4_8_0_, guido0_.contact_name as contact5_8_0_, guido0_.company_name as company6_8_0_, guido0_.cut as cut8_0_, guido0_.tz as tz8_0_ from public.guido guido0_ where guido0_.ac_id=? and guido0_.id=?
Hibernate: select guido0_.ac_id as ac1_8_0_, guido0_.id as id8_0_, guido0_.account_user_id as account3_8_0_, guido0_.address_id as address4_8_0_, guido0_.contact_name as contact5_8_0_, guido0_.company_name as company6_8_0_, guido0_.cut as cut8_0_, guido0_.tz as tz8_0_ from public.guido guido0_ where guido0_.ac_id=? and guido0_.id=?
01:44:02,363 DEBUG AbstractBatcher:424 - preparing statement
01:44:02,365 DEBUG IntegerType:79 - binding '1' to parameter: 1
01:44:02,366 DEBUG IntegerType:79 - binding '1' to parameter: 2
01:44:02,368 DEBUG AbstractBatcher:327 - about to open ResultSet (open ResultSets: 0, globally: 0)
01:44:02,369 DEBUG IntegerType:123 - returning '1' as column: ac1_8_0_
01:44:02,370 DEBUG IntegerType:123 - returning '1' as column: account3_8_0_
01:44:02,371 DEBUG IntegerType:123 - returning '1' as column: address4_8_0_
01:44:02,372 DEBUG IntegerType:123 - returning '1' as column: ac1_8_0_
01:44:02,372 DEBUG StringType:123 - returning 'Mr. Guido' as column: contact5_8_0_
01:44:02,379 DEBUG StringType:123 - returning 'TheCompanyName' as column: company6_8_0_
01:44:02,379 DEBUG BigDecimalType:123 - returning '0.00000' as column: cut8_0_
01:44:02,380 DEBUG IntegerType:123 - returning '1' as column: tz8_0_
01:44:02,381 DEBUG AbstractBatcher:334 - about to close ResultSet (open ResultSets: 1, globally: 1)
01:44:02,382 DEBUG AbstractBatcher:319 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
01:44:02,382 DEBUG AbstractBatcher:470 - closing statement
01:44:02,383 DEBUG AbstractBatcher:311 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
01:44:02,384 DEBUG SQL:346 - select guido0_.ac_id as ac1_1_, guido0_.account_user_id as account3_1_, guido0_.id as id1_, guido0_.ac_id as ac1_8_0_, guido0_.id as id8_0_, guido0_.account_user_id as account3_8_0_, guido0_.address_id as address4_8_0_, guido0_.contact_name as contact5_8_0_, guido0_.company_name as company6_8_0_, guido0_.cut as cut8_0_, guido0_.tz as tz8_0_ from public.guido guido0_ where guido0_.ac_id=? and guido0_.account_user_id=?
Hibernate: select guido0_.ac_id as ac1_1_, guido0_.account_user_id as account3_1_, guido0_.id as id1_, guido0_.ac_id as ac1_8_0_, guido0_.id as id8_0_, guido0_.account_user_id as account3_8_0_, guido0_.address_id as address4_8_0_, guido0_.contact_name as contact5_8_0_, guido0_.company_name as company6_8_0_, guido0_.cut as cut8_0_, guido0_.tz as tz8_0_ from public.guido guido0_ where guido0_.ac_id=? and guido0_.account_user_id=?
01:44:02,385 DEBUG AbstractBatcher:424 - preparing statement
01:44:02,385 DEBUG IntegerType:79 - binding '1' to parameter: 1
01:44:02,386 DEBUG IntegerType:79 - binding '1' to parameter: 2
01:44:02,388 DEBUG AbstractBatcher:327 - about to open ResultSet (open ResultSets: 0, globally: 0)
01:44:02,389 DEBUG IntegerType:123 - returning '1' as column: ac1_8_0_
01:44:02,390 DEBUG IntegerType:123 - returning '1' as column: id8_0_
01:44:02,390 DEBUG IntegerType:123 - returning '1' as column: ac1_1_
01:44:02,391 DEBUG IntegerType:123 - returning '1' as column: account3_1_
01:44:02,393 DEBUG IntegerType:123 - returning '1' as column: ac1_1_
01:44:02,396 DEBUG IntegerType:123 - returning '1' as column: id1_
01:44:02,397 DEBUG AbstractBatcher:334 - about to close ResultSet (open ResultSets: 1, globally: 1)
01:44:02,398 DEBUG AbstractBatcher:319 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
01:44:02,398 DEBUG AbstractBatcher:470 - closing statement
01:44:02,399 DEBUG AbstractBatcher:311 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
01:44:02,400 DEBUG SQL:346 - select account0_.id as id16_0_, account0_.address_id as address2_16_0_, account0_.name as name16_0_, account0_.contact_name as contact4_16_0_, account0_.core_server_id as core5_16_0_, account0_.tz as tz16_0_ from public.account account0_ where account0_.id=?
Hibernate: select account0_.id as id16_0_, account0_.address_id as address2_16_0_, account0_.name as name16_0_, account0_.contact_name as contact4_16_0_, account0_.core_server_id as core5_16_0_, account0_.tz as tz16_0_ from public.account account0_ where account0_.id=?
01:44:02,405 DEBUG AbstractBatcher:424 - preparing statement
01:44:02,406 DEBUG IntegerType:79 - binding '1' to parameter: 1
01:44:02,408 DEBUG AbstractBatcher:327 - about to open ResultSet (open ResultSets: 0, globally: 0)
01:44:02,409 DEBUG IntegerType:123 - returning '1' as column: address2_16_0_
01:44:02,410 DEBUG StringType:123 - returning 'jc' as column: name16_0_
01:44:02,410 DEBUG StringType:123 - returning 'Robert' as column: contact4_16_0_
01:44:02,411 DEBUG IntegerType:123 - returning '1' as column: core5_16_0_
01:44:02,411 DEBUG IntegerType:116 - returning null as column: tz16_0_
01:44:02,418 DEBUG AbstractBatcher:334 - about to close ResultSet (open ResultSets: 1, globally: 1)
01:44:02,419 DEBUG AbstractBatcher:319 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
01:44:02,420 DEBUG AbstractBatcher:470 - closing statement
Verified auth of this user: JBob for guido Mr. Guido, account jc
In NewSheetSubmitAction
01:44:02,421 DEBUG AbstractBatcher:311 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
01:44:02,422 DEBUG SQL:346 - select sheettype0_.id as id20_0_, sheettype0_.width as width20_0_, sheettype0_.height as height20_0_, sheettype0_.description as descript4_20_0_ from public.ad_type sheettype0_ where sheettype0_.id=?
Hibernate: select sheettype0_.id as id20_0_, sheettype0_.width as width20_0_, sheettype0_.height as height20_0_, sheettype0_.description as descript4_20_0_ from public.ad_type sheettype0_ where sheettype0_.id=?
01:44:02,425 DEBUG AbstractBatcher:424 - preparing statement
01:44:02,426 DEBUG IntegerType:79 - binding '2' to parameter: 1
01:44:02,427 DEBUG AbstractBatcher:327 - about to open ResultSet (open ResultSets: 0, globally: 0)
01:44:02,428 DEBUG IntegerType:123 - returning '120' as column: width20_0_
01:44:02,429 DEBUG IntegerType:123 - returning '600' as column: height20_0_
01:44:02,431 DEBUG StringType:123 - returning '120x600 Skyscraper' as column: descript4_20_0_
01:44:02,432 DEBUG AbstractBatcher:334 - about to close ResultSet (open ResultSets: 1, globally: 1)
01:44:02,433 DEBUG AbstractBatcher:319 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
01:44:02,433 DEBUG AbstractBatcher:470 - closing statement
Added sheet 1, adv acct was 1
Filter: closing session
01:44:02,478 DEBUG JDBCTransaction:103 - commit
01:44:02,480 DEBUG AbstractBatcher:311 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
01:44:02,480 DEBUG SQL:346 - insert into public.sheet (ad_type_id, make_good, name, expire_date, status, ac_id, id) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into public.sheet (ad_type_id, make_good, name, expire_date, status, ac_id, id) values (?, ?, ?, ?, ?, ?, ?)
01:44:02,481 DEBUG AbstractBatcher:424 - preparing statement
01:44:02,481 DEBUG IntegerType:79 - binding '0' to parameter: 1
01:44:02,482 DEBUG IntegerType:71 - binding null to parameter: 2
01:44:02,482 DEBUG StringType:79 - binding 'name' to parameter: 3
01:44:02,483 DEBUG TimestampType:71 - binding null to parameter: 4
01:44:02,483 DEBUG StringType:79 - binding 'blah' to parameter: 5
01:44:02,484 DEBUG IntegerType:79 - binding '1' to parameter: 6
01:44:02,485 DEBUG IntegerType:79 - binding '1' to parameter: 7
01:44:02,488 DEBUG AbstractBatcher:28 - Adding to batch
01:44:02,489 DEBUG AbstractBatcher:55 - Executing batch size: 1
01:44:02,491 DEBUG AbstractBatcher:319 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
01:44:02,492 DEBUG AbstractBatcher:470 - closing statement
01:44:02,493 DEBUG JDBCContext:193 - before transaction completion
01:44:02,570 DEBUG JDBCTransaction:116 - committed JDBC Connection
01:44:02,571 DEBUG JDBCContext:207 - after transaction completion
01:44:02,571 DEBUG ConnectionManager:296 - aggressively releasing JDBC connection
01:44:02,572 DEBUG ConnectionManager:333 - closing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
01:44:02,572 DEBUG DriverManagerConnectionProvider:129 - returning connection to pool, pool size: 1
01:44:02,573 DEBUG ConnectionManager:267 - connection already null in cleanup : no action


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 17, 2006 12:07 pm 
Regular
Regular

Joined: Fri Sep 09, 2005 11:35 am
Posts: 101
<many-to-one name="guido" class="com.allentown.db.Guido" update="false" insert="false" fetch="select">
<column name="ac_id" not-null="true" />
<column name="guido_id" />
</many-to-one>

I believe the highlighted stuff is causing you problems.


Top
 Profile  
 
 Post subject: Thanks
PostPosted: Tue Jan 17, 2006 12:47 pm 
Newbie

Joined: Sun Jul 31, 2005 11:12 pm
Posts: 8
Thanks, now I'm getting this error:

Code:
org.hibernate.MappingException: Repeated column in mapping for entity: com.allentown.db.Sheet column: ac_id (should be mapped with insert="false" update="false")


This is due to the fact that ac_id is also part of the primary key thus part of the hibernate composite index which can also be inserted/updated, creating a situation where ac_id can be updated in 2 hibernate properties which Hibernate understandably doesn't like. So it seems I'm painting myself into a corner.

Maybe I should add another hibernate property, modifiableGuidoId or something and leave the above association " insert="false" update="false" " Then in my business logic I can just make sure to set the modifiableGuidoId property when adding/udpating a sheet.

Having to add a another property is a bummer since I'm using automatic POJO generation from the DB courtesy of the hibernate plugin -- I think I have to now disable the sheet table from generation so all associations pointing to a sheet or vice versa will be lost ,which means because of this I cannot use POJO generation with the hibernate plugin(if I'm not missing something). If the hibernate revenge file allowed for adding a property or something then that would be great.

I may also want to reconsider composite keys since there seems to be many gotchas and mines in the field. That way I could use automatic schema generation.

At any rate, thanks for your help.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 17, 2006 12:53 pm 
Regular
Regular

Joined: Fri Sep 09, 2005 11:35 am
Posts: 101
can you use key-many-to-one in the composite-id which will make Guido part of the primary key of sheet.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 17, 2006 3:06 pm 
Newbie

Joined: Sun Jul 31, 2005 11:12 pm
Posts: 8
abg1979 wrote:
can you use key-many-to-one in the composite-id which will make Guido part of the primary key of sheet.


I just learned about that thanks to you.

This wouldn't work since the association shouldn't be in the ID schema-wise.

I thought about it and the best things to do is add a guidoId integer that can be used for inserts/updates.

So this doesnt break the hibernate tools code generation, I'll apply patches after the files are generated which will add the guidoId.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.