-->
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: Collection in Collection persisting problem
PostPosted: Fri Aug 17, 2007 6:08 am 
Newbie

Joined: Tue Jul 31, 2007 8:37 am
Posts: 12
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.2.4 SP1

Hello all!

Sorry for my english at first.

I have a legacy DB schema, and I'm not allowed to change anything.

The scheme looks like that:
Code:
                     
|--TableA----------|1           |--TableB----------|       1..*|--TableC----------|         |--TableD----------|
|TableA_ID (PK)    |<--|    1..*|TableB_ID (PK)    |<----------|TableB_ID (PK,FK) |    |--->|TableD_ID (PK,FK) |
|------------------|   |--------|TableA_ID (FK)    | 1         |TableD_ID (PK,FK) |----|   1|Bemerkung         |
                                |LFDNR             |           |LFDNR             |1..*     |------------------|
                                |------------------|           |------------------|


I wrote some mapping-files by hand for each table:
Mapping documents:
Mapping of Class A:
Code:
<hibernate-mapping>
  <class name="ClassA"  table="TableA">
     <id name="id" column="TableA_ID" />     
     <bag name="bTables" cascade="all" table="TableB" >
        <key column="TableA_ID"/>        
        <one-to-many class="ClassB"/>
     </bag>
  </class>
</hibernate-mapping>

Mapping of Class B:
Code:
<hibernate-mapping>
   <class name="ClassB" table="TableB">
      <id name="id" column="TableB_ID""/>
      <bag name="cTables" order-by="lfdNr" cascade="all"  fetch="join">
         <key column="TableB_ID" unique="true"/>
         <one-to-many class="ClassC" />
      </bag>
      <many-to-one name="classA" column="TableA_ID" cascade="all" class="ClassA"/>
      
      <property name="lfdNr">
         <column name="LFDNR" sql-type="Number(3)"/>
      </property>
   </class>
</hibernate-mapping>

Mapping of Class C:
Code:
<hibernate-mapping>
   <class name="ClassC" table="TableC" >
      <composite-id >               
         <key-many-to-one
            name="classD"
            column="TableD_ID" 
            class="ClassD"/>            
         <key-many-to-one
            name="classB"
            column="TableB_ID"   
            class="ClassB" />            
      </composite-id>      

        <many-to-one name="classD" not-null="true" insert="false" update="false" cascade="all," fetch="join" column="TableD_ID" />
        <many-to-one name="classB" not-null="true"  insert="false" update="false" cascade="all" fetch="join" column="TableB_ID" />      
      
      <property name="lfdNr" column="LFDNR" type="int"/>   
   </class>
</hibernate-mapping>

Mapping of Class D:
Code:
<hibernate-mapping>
   <class name="ClassD"  table="TableD">
      <id name="id">
         <column name="TableD_ID"/>
         <generator class="guid"></generator>
      </id>
      <bag name="cTables" cascade="all">
         <key column="TableD_ID"/>
         <one-to-many class="ClassC"/>         
      </bag>
      <property name="bemerkung">
         <column name="BEMERKUNG" sql-type="VARCHAR2(30)"/>
      </property>
   </class>
</hibernate-mapping>


All this stuff works, and no exceptions are thrown, but the problem is the creating of a new object of ClassC.

A new ClassC-object means, that a new ClassD-object should be created and the ClassB-object can be one of the existing objects.
So I create a new ClassC-object, whereas a ClassD-object is created automatically by the constructor and the ClassB Attribute is assigned to an existing ClassB-object.

After that I say to the session, that there are new objects:
Code:
        Session session = HibernateUtil.getSession();
        session.saveOrUpdate(classC.getClassD);
        session.saveOrUpdate(classC);
        session.saveOrUpdate(classA);
        session.close();


But I don't want to make a transaction at this time, so that the new objects are not persisted yet.
Later on when there should be the transaction, I want to persist the whole ClassA-object and all it's connections, and now I thougt Hibernate can detect all the new objects in the hierarchy and persist also the new ClassC-object and ClassD -object.

But this doesn't happen!! Nothing of the new children(ClassC) of the children(ClassB) from ClassA are persistet?

What should be changed in the mapping files?

thanks for helping.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 17, 2007 7:13 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
You simply shouldn't do updates outside a transaction - you can't be sure what will happen. If autocommit is on then these updates will commit one by one in individual transactions - probably not what you want. If autocommit is off the the behaviour is undefined - again, not what you want.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 17, 2007 7:31 am 
Newbie

Joined: Tue Jul 31, 2007 8:37 am
Posts: 12
Ok, thanks for fast reply.

Yes, you're right, I want that the all the changes in the ClassA-object (that means all changes in the Connections to the other objects, like new children, delete some children etc..) is done, when the user decide it.
That means he is ready with all his changes (and this can be a lot):

The problem is, that the changes can't be made persistent immediately, because the performance is to bad... and the user should have the posibility to discard all his changes.

I dont want to make an extra, temporal object, where all the changes are logged... I thought Hibernate use the session for that.

mhh... so this way isn't possible with Hibernate? or are there any other solutions...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 17, 2007 7:41 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Although I've never tried this, there is a discussion in the docs entitled "Extended session and automatic versioning" that might be worth a read. It talks about setting FlushMode.NEVER in the session so that all changes in previous transactions (I think) can be flushed explicitly on the last transaction.


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