-->
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.  [ 8 posts ] 
Author Message
 Post subject: one-to-one basic OR.....please help.........
PostPosted: Mon Sep 06, 2004 10:59 am 
Newbie

Joined: Thu Aug 12, 2004 9:32 am
Posts: 17
Hibernate version:2.1.6

Hi,
I know it's a basic stuff what im about to ask but I cannot understand this ......

I have ClassA containing ClassB and what I need is, that when creating class A It will insert 2 records (classA table and ClassB table)

ClassA (has no id property)
private x
private y
private classBinstance ClassB

ClassB
private id
private z

tables:
Code:
   TABLE ClassA
        CLASSA_ID char(32),
        X ...
        y...
       

   TABLE ClassB
       CLASSB_ID number(10),
       CLASSA_ID char(32),
        Z...


mapping:
Code:
    <class name="ClassAr" table="CLASSA">
       <id type="string" unsaved-value="null" access="field">
           <column name="CLASSA_ID" sql-type="char(32)" not-null="true"/>
                <generator class="uuid.hex"/>
       </id>
        <one-to-one name="classBinstance" class="ClassB" cascade="all" constrained="true"/>
    </class>


   <class name="ClassB" table="CLASSB">
       <id name="id" type="long" unsaved-value="null" access="field">
           <column name="CLASSB_ID" sql-type="number(10)" />
                <generator class="assigned"/>
       </id>
       ...
       ...
    </class>




now when I tried
cb ClassB = new ClassB();
ca ClassA = new ClassA(cb); //sets the inner property

storage.save(ca);

If FAILED ON UPDATE CLASS B !!!!
I saw
Insert into ClassA......
Update ClassB (when it did not insert it there yet).....

Is that mapping is right ?
i did not understand if i should put the class="foreign" in generator and where.....

help ? anyone ????


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 06, 2004 12:16 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
either do
* s.save(b) to help Hibernate to know this is an insert and not an Update
* use foreign generator in B and add b.getA() to refer to
* set unsaved-value of B to any (you'll have to do s.update() by hands however)
* and my favorite add a version number to B and put unsaved-value=null inside it. Hibernate will be able to know wheter B is new or already eprsisted

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 07, 2004 5:06 am 
Newbie

Joined: Thu Aug 12, 2004 9:32 am
Posts: 17
Setting version required me to change the class and add a version property.

I am trying to change the software architecture as minimum as possible.
however define mapping and the changes in the database are more acceptable.

When I saved class B is was OK.
then I saved class A and it was OK

the problem is that in one-to-one relation it is using the primary_id for the relation, so when read classA it tried reading classB using comparison of both ID's which are different.

I can use property-ref but as I mentioned I dont want to add any properties to the class......

In many-to-one you can define column and not "property" how come i cannot do that in one-to-one ???? or is there different way to do that ???

Isn't it possible that I will just have a column ( classBid ) in classA table,
and when i will save classA it will update the ID of classB in it, AND when i will read classA it will read classB using its classBid column ????


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 18, 2004 6:15 pm 
Newbie

Joined: Fri Sep 17, 2004 3:07 pm
Posts: 14
Had a pretty similar problem with some code based on the Hibernate ref doc example.

This helped me (using the unique foreign key approach):
- cascade="all" in the mapping of the "parent" object you intend to save
- in addition to the many-to-one declaration in the "child" object mapping, declare the foreign key field itself (hibernate will not complain if missing)

Perhaps add these to the doc examples, Hibernate team ?

//markku


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 18, 2004 6:21 pm 
Newbie

Joined: Fri Sep 17, 2004 3:07 pm
Posts: 14
Oh yes, and set the child-to-parent relationship in addition to parent-child.

//m


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 19, 2004 7:50 am 
Newbie

Joined: Sun Sep 19, 2004 7:38 am
Posts: 4
Location: Prague
Hi.

I have similar problem (actually with my head :-)).

Can you show me solution of this problem (<one-to-one> bidirectional mapping)? I mean all java bean definiton code, SQL DDL, hiberanate mapping file and Java code between sessionFactory.openSession() and session.close()

Thanks very much.

Petr


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 19, 2004 10:58 am 
Newbie

Joined: Thu Aug 12, 2004 9:32 am
Posts: 17
I think it is uni-direction only but maybe you can expand that.....


Mapping:
Code:
    <class name="ProcessManager" table="ProcessManager">
       <id type="string" unsaved-value="null" access="field">
           <column name="Process_Def_Id" sql-type="char(32)" not-null="true"/>
                <generator class="foreign">
                   <param name="property">processDefinition</param>
                </generator>
       </id>
        <property name=
        ...
        ...
        ...
        <one-to-one name="processDefinition"  class="ProcessDefinition" cascade="all" constrained="true"/>
    </class>


    <class name="ProcessDefinition" table="ProcessDefinition">
       <id type="string" unsaved-value="null" access="field">
           <column name="Process_Def_ID" sql-type="char(32)" not-null="true"/>
                <generator class="uuid.hex"/>
       </id>
       <property name="processId"
       ....
       ....
       ....

    </class>


DATABASE:
Code:
Create table ProcessManager (
       process_Def_ID       char(32),
       other_properties......


create table ProcessDefinition (
     Process_Def_ID    char(32),
     processId            number(10),
     other_properties.....



Nir


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 19, 2004 11:29 am 
Newbie

Joined: Sun Sep 19, 2004 7:38 am
Posts: 4
Location: Prague
to mooki

Thank You Very Much.

Unfortunately, I found out that my problems are not in mapping (only).
I dont want pollute this forum by my stupid question. Would it be possible discus by email? My @ is in my profile.


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