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: Foreign-key values not inserted in one-to-many relation
PostPosted: Thu Mar 06, 2008 10:29 am 
Newbie

Joined: Fri Feb 01, 2008 9:30 am
Posts: 2
Greetings!

This seems to be a trivial issue, but I didn't manage to find a solution with the documentation, google and my colleagues - hence this post. I prepared a simplified test case for the problem I'm experiencing with a more complicated model. Let's start with general information and mapping files:

Hibernate version: 3.2.6.ga
Database engines: HSQLDB / Oracle 9i

Mapping documents:
Transaction:
Code:
<hibernate-mapping>
    <class name="Transaction" table="Transaction">
   
        <id type="integer">
           <generator class="increment"></generator>
        </id>
       
        <many-to-one name="Agent" class="Agent" update="false" insert="false" fetch="select">
            <column name="ValueA" length="8" />
            <column name="ValueB" length="5" />
        </many-to-one>
       
        <property name="code" type="string">
            <column name="code" length="3" />
        </property>
   </class>
</hibernate-mapping>


Agent:
Code:
<hibernate-mapping>
    <class name="Agent" table="Agent">
       <composite-id>
      <key-property name="valueA" type="string">
         <column name="valueA" />
      </key-property>
       <key-property name="valueB" type="string">
         <column name="valueB" />
      </key-property>
       </composite-id>
       
        <set name="transactions" inverse="true">
            <key>
                <column name="ValueA" length="8" />
                <column name="ValueB" length="5" />
            </key>
            <one-to-many class="Transaction" />
        </set>
    </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Code:
try {
         session.beginTransaction();
      
         Transaction trans = new Transaction();
         Agent agent = new Agent();
         
         agent.setValueA("agent value A");
         agent.setValueB("agent value B");
         trans.setCode("transaction code");
         
         agent.getTransactions().add(trans);
         trans.setAgent(agent);
         
         session.save(agent);
         session.save(trans);
         
         session.getTransaction().commit();
      } catch(HibernateException ex) {
         session.getTransaction().rollback();
         throw ex;
      }


The generated SQL (show_sql=true):
Code:
Hibernate: select max(id) from Transaction
Hibernate: insert into Agent (valueA, valueB) values (?, ?)
Hibernate: insert into Transaction (code, id) values (?, ?)


Now, onto the problem itself: as you can see in the SQL log, saving both objects generates two inserts, but values valueA and valueB are not inserted into the transaction table.

Of course, if I get the Transaction back from the database it has the reference to Agent set to null.

What did I miss here?


Thanks in advance,
Karol


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.