-->
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.  [ 6 posts ] 
Author Message
 Post subject: One-to-one relationship
PostPosted: Wed May 12, 2004 4:42 pm 
Beginner
Beginner

Joined: Tue May 11, 2004 10:40 am
Posts: 37
Location: Belgium
Hi,
I have the following simple situation:
Two classes who has a one-to-one relationship.
Class Component generates the key for class Currency.

When I store an Object of the Component class type, hibernate only stores the component in the component table, but he doesn't store anything in the currency table.

Can somebody help me with this?

Here's my code:

Code:
<hibernate-mapping>
   
    <class
        name="logic.Component"
        table="COMPONENT">
       
        <id
            name="id"
            column="ID"
            type="java.lang.Long"
            unsaved-value="null">         
            <generator class="native"/>           
        </id>
       
...
         
        <one-to-one name="price"/>
                           
...                 
               
    </class>

</hibernate-mapping>


Code:
<hibernate-mapping>

    <class
        name="logic.Currency"
        table="CURRENCY">
       
        <id
            name="id"
            column="ID"
            type="long">           
            <generator class="assigned">       
            </generator>                     
        </id>
               
  ...
           
        <one-to-one
             name="component"
             class="logic.Component"
             constrained="true"
             foreign-key="componentfk"/>             
                                                                         
    </class>
   
</hibernate-mapping>


Code:
public abstract class Component extends Persistent implements Serializable {
   protected String name;
   private String description;
   private Blob photo = null;
   protected Currency price;
   protected boolean selected;
   protected List children;
   protected CompositeComponent parent;
...


Code:
public class Currency extends Persistent{
   private double amount;
   private int moneyType;
   public double exchangeRates[];
   private Component component;
...


That's the last thing hibernate prints in the console:
Code:
22:25:17,937  INFO DriverManagerConnectionProvider:143 - cleaning up connection pool: jdbc:mysql://127.0.0.1:3306/carco
Hibernate: insert into COMPONENT (NAME, DESCRIPTION, PHOTO, PARENT, DISCRIMINATOR) values (?, ?, ?, ?, 'stdcomposite')


Like you can see, he doesn't insert anything in the currency table.

Thanks in advance ...


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 12, 2004 5:56 pm 
Regular
Regular

Joined: Thu Jan 29, 2004 10:34 am
Posts: 52
Location: Austin, TX
based on your cardinality save currency object first and try to cascade the save from currency to component (or save them separately)

Code:
<class
        name="logic.Currency"
        table="CURRENCY">                 
  ...
       <one-to-one
             name="component"
             ...
             cascade="save-update"/>


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 12, 2004 6:08 pm 
Regular
Regular

Joined: Wed May 05, 2004 3:41 pm
Posts: 118
Location: New Jersey,USA
The cascase property must be set for Hibernate to insert dependent objects of an object. Use the following mapping :-

Code:
<one-to-one name="price" casecade="all"/>


Depending on yoour needs you can choose the respective cascade option.

-anand


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 12, 2004 6:09 pm 
Beginner
Beginner

Joined: Tue May 11, 2004 10:40 am
Posts: 37
Location: Belgium
I made the changes, but it is the same result again.
Nothing is pushed on my currency table.

The second proposal to store the both object separatly is almost impossible to do in my program.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 12, 2004 6:13 pm 
Beginner
Beginner

Joined: Tue May 11, 2004 10:40 am
Posts: 37
Location: Belgium
When I write cascade="all" like you said, I get the following error:
ids for this class must be manually assigned before calling save(): logic.Currency


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 12, 2004 6:34 pm 
Regular
Regular

Joined: Thu Jan 29, 2004 10:34 am
Posts: 52
Location: Austin, TX
get rid of assigned id generator on Currency. change 'assigned' to 'native'.


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