-->
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: one-to-one with shared id
PostPosted: Thu Oct 04, 2007 7:36 am 
Newbie

Joined: Tue Sep 18, 2007 3:30 pm
Posts: 17
Hi,

I have a strange situation in my Hibernate application:

I have a class Person, which has a field called Representant. Representant may be sometimes null and if it is not - representant id is always the same as person's id.


class Person {
...
private Representant representant;
...
}

in my hbm.xml file this association is defined as:

<class name="org.dyndns.orzekanie.Person" table="PERSONS">
<id name="id">
<generator class="native"/>
</id>
...
<one-to-one name="representant" class="org.dyndns.orzekanie.Representant" lazy="false" cascade="all"/>
</class>

class Representant is defined as:

<class name="org.dyndns.orzekanie.Representant" table="REPRESENTANTS">
<id name="id">
<generator class="assigned"/>
</id>
...
</class>


the problem is that where i try to save a person (session.save(person)) as a new record in the database, the representant id is not initialized - so it ends with
org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save()

I thought I can do something like that:

<class name="org.dyndns.orzekanie.Person" table="PERSONS">
<id name="id">
<generator class="foreign">
<param name="property">representant</param>
</generator>
</id>
...
<one-to-one name="representant" class="org.dyndns.orzekanie.Representant" lazy="false" cascade="all"/>
</class>

<class name="org.dyndns.orzekanie.Representant" table="REPRESENTANTS">
<id name="id">
<generator class="native"/>
</id>
...
</class>

but the representant may be sometimes null so when I save the person with null representant it would be impossible to get an id.

I have no idea how I should implement my hibernate associations. Could anyone give me some advices?

Thanks,
Pawel


Hibernate version: 3.2.24

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using: MySQL 4.1

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject: look the link below
PostPosted: Thu Oct 04, 2007 8:53 am 
Regular
Regular

Joined: Tue Feb 24, 2004 11:42 am
Posts: 56
http://www.hibernate.org/hib_docs/v3/re ... n-onetoone


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 16, 2007 12:22 pm 
Newbie

Joined: Tue Sep 18, 2007 3:30 pm
Posts: 17
Thanks. I read chapter you'd mentioned more carefully than before and I've found a solution:


<class name="org.dyndns.orzekanie.Person" table="PERSONS">
<id name="id">
<generator class="native">
<param name="property">representant</param>
</generator>
</id>
...
<one-to-one name="representant" class="org.dyndns.orzekanie.Representant" lazy="false" cascade="all"/>
</class>

<class name="org.dyndns.orzekanie.Representant" table="REPRESENTANTS">
<id name="id">
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
...
<one-to-one name="person" class="org.dyndns.orzekanie.Person" constrained="true"/>
...
</class>


The thing is that I should add Person class to my Representant class. It makes no sense for me but it does not prevent in any case. "constrained" property tells hibernate the order in which it should get/update/save objects.

Pawel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 30, 2008 3:56 pm 
Newbie

Joined: Tue Jan 15, 2008 4:27 pm
Posts: 5
Thought I'd bump this up - not sure how to rate it... This is a good post.

Posting my redacted mapping files just for another example

Code:
  <class name="" table="" optimistic-lock="version">

    <id name="Id" column="OrderTicketId">
      <generator class="native">
        <param name="property">OrderItem</param>
      </generator>
    </id>
   
    <version name="Version" />
    <one-to-one name="OrderItem" class="OmegaWeb.domain.OrderItem, OmegaWeb" cascade="all" />
  </class>



Code:
  <class name="" table="" optimistic-lock="version">

    <id name="Id" column="OrderTicketId">
      <generator class="foreign">
        <param name="property">OrderTicket</param>
      </generator>
    </id>
    <version name="Version" />

    <one-to-one name="OrderTicket" class="OmegaWeb.domain.OrderTicket, OmegaWeb" constrained="true" />
   
  </class>


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.