-->
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.  [ 9 posts ] 
Author Message
 Post subject: One-to-one mapping problem
PostPosted: Tue Feb 17, 2004 6:16 am 
Newbie

Joined: Mon Jan 05, 2004 10:46 am
Posts: 13
Hi,

I have the following class which is used to build a tree of groups:

Code:
public final class SystemEventGroup
{
    private long id;
    private SystemEventGroup parentEventGroup;
    private String name;
    private String descr;

    // getters and setters
    ...
}


I could not figure out how to write the mapping for the parentEventGroup
property.

How can a mapping for such recursive object graphs be realized? I searched the forum but could not find a solution.

Any help greatly appreciated.

Thanks,

juergen


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2004 6:34 am 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
I'm not sure a one-to-one mapping is what you need: will a parent have possibly more than one child, but a child a single parent ?
In such case, you should use a many-to-one (read from the Child):

Code:
   <class name="SystemEventGroup" >

      <!-- Unique Identifier -->
      <id name="id" type="long">
         <generator ....>
        ....
         </generator>
      </id>
      
      <property name="name" />
      <property name="descr" />
   
      <many-to-one
        name="parentEventGroup"
      />
   
    ....
  </class>


Note: if a one-to-one is what you need, just replace the many-to-one...


Top
 Profile  
 
 Post subject: One-to-one mapping problem
PostPosted: Tue Feb 17, 2004 7:20 am 
Newbie

Joined: Mon Jan 05, 2004 10:46 am
Posts: 13
I have a 1..1 association. I tried your solution approach, but this does not work.

Here is my mapping:
Code:
    <class name="wilken.openshop.core.event.SystemEventGroup" table="system_event_group">
        <id name="id" type="long" unsaved-value="-1">
            <column name="id" sql-type="int(11)" not-null="true"/>
            <generator class="identity"/>
        </id>
        <one-to-one name="parentEventGroup" class="wilken.openshop.core.event.SystemEventGroup"/>
        <property name="name" />
        <property name="descr" />
    </class>


When I do the schema update I only get a table created with id, name and descr, additionally an SQL exception is thrown because Hibernate trys to execute the following statement: "select from ". (hibernate.show_sql=true).

Any clue, how I can solve this problem?

Thanks for your help.

juergen


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2004 7:25 am 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
Please read the section that deals with one-to-one relationships.

I think the problems comes from the fact that two entities linked by a one-to-one relationship will share the same ID (at least by default - never tried). This, of course, will not work in your case ('cause same table).

Anyway, you could achieve exactly the same behavior (at least in your case) by using a many-to-one from the child to the parent...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2004 7:28 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Phew, thats a hard one ... I never saw anyone do this. I think you will have to try to map the relationship property explicitly (eg. map parentId) and use the property-ref attribute in the one-to-one ... probably this might work out.


Top
 Profile  
 
 Post subject: One-to-one mapping problem
PostPosted: Tue Feb 17, 2004 7:39 am 
Newbie

Joined: Mon Jan 05, 2004 10:46 am
Posts: 13
Thanks for your help.

I will try your solutions.

Best regards,

juergen


Top
 Profile  
 
 Post subject: One-to-one mapping problem
PostPosted: Tue Feb 17, 2004 9:23 am 
Newbie

Joined: Mon Jan 05, 2004 10:46 am
Posts: 13
Hi,

I have tried your solution proposal, but unfortunatly it does not work. I do not think what I am trying to do so unusual that Hibernate cannot deal with it.

Basically its a linked list which I would like to map to one database table.

I also tried to change my object model, just to test if this could work.
My class looks like this now.

Code:
public final class SystemEventGroup
{
    private long id;
    private long eventKey;
    private Set parentEventGroups = new HashSet();
    private String name;
    private String descr;
...
}


This is the mapping I am using:

Code:
    <class name="wilken.openshop.core.event.SystemEventGroup" table="system_event_group">
        <id name="id" type="long" unsaved-value="-1">
            <column name="id" sql-type="int(11)" not-null="true"/>
            <generator class="identity"/>
        </id>
        <property name="eventKey" column="event_key"/>
        <set name="parentEventGroups" table="system_event_group">
            <key column="parent_key"/>
            <one-to-many class="wilken.openshop.core.event.SystemEventGroup"/>
        </set>               
        <property name="name" />
        <property name="descr" />
    </class>


If I do the schema update I still get this strange sql statement "select from " executed by Hibernate which obviously results in a sql exception.

When trying to store an Object which has a parent the child gets inserted but as soon as the parent should be inserted the following exception is thrown. This will not happen if I use two different classes.

Code:
14:01:06,053 DEBUG [SessionImpl] opened session
14:01:06,053 DEBUG [JDBCTransaction] begin
14:01:06,053 DEBUG [JDBCTransaction] current autocommit status:true
14:01:06,053 DEBUG [JDBCTransaction] disabling autocommit
14:01:06,069 DEBUG [SessionImpl] saving [wilken.openshop.core.event.SystemEventGroup#<null>]
14:01:06,069 DEBUG [EntityPersister] Inserting entity: wilken.openshop.core.event.SystemEventGroup (native id)
14:01:06,069 DEBUG [BatcherImpl] about to open: 0 open PreparedStatements, 0 open ResultSets
14:01:06,069 DEBUG [SQL] insert into system_event_group (event_key, name, descr) values (?, ?, ?)
Hibernate: insert into system_event_group (event_key, name, descr) values (?, ?, ?)
14:01:06,069 DEBUG [BatcherImpl] preparing statement
14:01:06,069 DEBUG [EntityPersister] Dehydrating entity: [wilken.openshop.core.event.SystemEventGroup#<null>]
14:01:06,069 DEBUG [LongType] binding '0' to parameter: 1
14:01:06,069 DEBUG [StringType] binding 'event1' to parameter: 2
14:01:06,069 DEBUG [StringType] binding null to parameter: 3
14:01:06,069 DEBUG [BatcherImpl] done closing: 0 open PreparedStatements, 0 open ResultSets
14:01:06,069 DEBUG [BatcherImpl] closing statement
14:01:06,069 DEBUG [BatcherImpl] about to open: 0 open PreparedStatements, 0 open ResultSets
14:01:06,069 DEBUG [SQL] SELECT LAST_INSERT_ID()
Hibernate: SELECT LAST_INSERT_ID()
14:01:06,069 DEBUG [BatcherImpl] preparing statement
14:01:06,069 DEBUG [EntityPersister] Natively generated identity: 20
14:01:06,069 DEBUG [BatcherImpl] done closing: 0 open PreparedStatements, 0 open ResultSets
14:01:06,069 DEBUG [BatcherImpl] closing statement
14:01:06,069 DEBUG [JDBCTransaction] commit
14:01:06,069 DEBUG [SessionImpl] flushing session
14:01:06,069 DEBUG [SessionImpl] Flushing entities and processing referenced collections
14:01:06,100 DEBUG [WrapVisitor] Wrapped collection in role: wilken.openshop.core.event.SystemEventGroup.parentEventGroups
14:01:06,100 DEBUG [SessionImpl] Collection found: [wilken.openshop.core.event.SystemEventGroup.parentEventGroups#20], was: [<unreferenced>]
14:01:06,100 DEBUG [SessionImpl] Processing unreferenced collections
14:01:06,100 DEBUG [SessionImpl] Scheduling collection removes/(re)creates/updates
14:01:06,116 DEBUG [SessionImpl] Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects
14:01:06,116 DEBUG [SessionImpl] Flushed: 1 (re)creations, 0 updates, 0 removals to 1 collections
14:01:06,116 DEBUG [Printer] listing entities:
14:01:06,116 DEBUG [Printer] wilken.openshop.core.event.SystemEventGroup{eventKey=0, descr=null, name=event1, parentEventGroups=[SystemEventGr
14:01:06,116 DEBUG [SessionImpl] executing flush
14:01:06,116 DEBUG [BasicCollectionPersister] Inserting collection: [wilken.openshop.core.event.SystemEventGroup.parentEventGroups#20]
14:01:06,116 DEBUG [BatcherImpl] about to open: 0 open PreparedStatements, 0 open ResultSets
14:01:06,116 DEBUG [SQL] update system_event_group set parent_key=? where id=?
Hibernate: update system_event_group set parent_key=? where id=?
14:01:06,116 DEBUG [BatcherImpl] preparing statement
14:01:06,116 DEBUG [LongType] binding '20' to parameter: 1
14:01:06,116 DEBUG [Cascades] id unsaved-value: -1
14:01:06,116 DEBUG [LongType] binding '0' to parameter: 2
14:01:06,116 DEBUG [BatcherImpl] Adding to batch
14:01:06,116 DEBUG [BasicCollectionPersister] done inserting collection: 1 rows inserted
14:01:06,116 DEBUG [BatcherImpl] Executing batch size: 1
14:01:06,131 DEBUG [BatcherImpl] done closing: 0 open PreparedStatements, 0 open ResultSets
14:01:06,131 DEBUG [BatcherImpl] closing statement
14:01:06,131 ERROR [SessionImpl] Could not synchronize database state with session
net.sf.hibernate.HibernateException: Batch update row count wrong: 0
        at net.sf.hibernate.impl.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:65)
        at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:118)
        at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2311)
        at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2265)
        at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2187)
        at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
        at wilken.openshop.test.TestBean.store(TestBean.java:36)


This is really strange, I do not have a clue why.

Any opinion what can be done to solve this problem?

I would like to avoid to adjust my object model in order for Hibernate to be able to work with it.

Thanks for your help.

Best regards,

juergen


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2004 10:26 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
set batch_size=0 to see a more detailed error.
Check unsaved-value

_________________
Emmanuel


Top
 Profile  
 
 Post subject: One-to-one mapping problem, solution found
PostPosted: Tue Feb 17, 2004 10:29 am 
Newbie

Joined: Mon Jan 05, 2004 10:46 am
Posts: 13
Hi,

I think I found a solution for my problem.

This is my class:

Code:
public final class SystemEventGroup
{
    private Long id;
    private long eventKey;
    private SystemEventGroup parentEventGroup;
    private String name;
    private String descr;
...
}


This is the mapping which does the job for me:

Code:
    <class name="wilken.openshop.core.event.SystemEventGroup" table="system_event_group">
        <id name="id" type="long" unsaved-value="-1">
            <column name="id" sql-type="int(11)" not-null="true"/>
            <generator class="identity"/>
        </id>
        <property name="eventKey" column="event_key"/>
        <many-to-one name="parentEventGroup"
            class="wilken.openshop.core.event.SystemEventGroup"
            column="parent_key"
            property-ref="eventKey"
            cascade="all"/>
        <property name="name" />
        <property name="descr" />
    </class>


Thanks for your help.

Best regards,

juergen


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.