-->
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.  [ 5 posts ] 
Author Message
 Post subject: how can i define a 0-1 relation?
PostPosted: Sat Feb 21, 2004 5:05 pm 
Newbie

Joined: Mon Jan 12, 2004 7:05 am
Posts: 16
i want define a 0-1 relation between person and cat.
cat has owner as property, and person knows nothing about cat.

Below are the Person and Cat mappings respectively:
<class name="model.Cat" table="CAT">
<id name="id" type="long">
<column name="CAT_ID" not-null="true" />
<generator class="native" />
</id>
<property name="name"><column name="name" not-null="false" /> </property>
<property name="sex"><column name="sex" not-null="false" /> </property>
<property name="weight"><column name="weight" not-null="false" /> </property>
<many-to-one name="owner" class="model.Person" column="person_id"/>
</class>

<class name="model.Person" table="PERSON">
<id name="id" type="long">
<column name="PERSON_ID" not-null="true" />
<generator class="native" />
</id>
<property name="forename">
<column name="forename" not-null="false" />
</property>
<property name="surname">
<column name="surname" not-null="false" />
</property>
</class>

after insert i get null for person_id in cat table.
Code:
       Person p = new Person();
      p.setForename("Xiaofeng");
      p.setSurname("Wu");
      
      Cat cat = new Cat(p);
      cat.setName("mimi");

did i do something wrong? how can i define a 0-1 relation in HB?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 22, 2004 1:07 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Instread of the constructor try setting it using the property.
Code:
      Person p = new Person();
      ....

      Cat cat = new Cat();
      cat.setName("mimi");
      cat.setOwner(p);


Make sure you are commiting the transaction.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 22, 2004 2:36 pm 
Newbie

Joined: Mon Jan 12, 2004 7:05 am
Posts: 16
it works. Thanks.

But it is strange, why i can't use the constructor to insert the relation of person and cat.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 22, 2004 4:14 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
I don't know, sounds pretty strange to me. Is your constructor doing just a property set? Have you changed anything else in the meantime?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 22, 2004 5:23 pm 
Newbie

Joined: Mon Jan 12, 2004 7:05 am
Posts: 16
perhaps i have something wrong before, now it works also with the constructor.


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