-->
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.  [ 2 posts ] 
Author Message
 Post subject: How to model Hibernate one to zero or one relationship
PostPosted: Wed Oct 29, 2014 6:53 am 
Newbie

Joined: Fri Sep 26, 2014 11:44 am
Posts: 2
** Sorry for this wordy post, but I wanted to describe as much as I could about my situation **

I've been trying to get a 1 -> 0..1 relationship modeled in Hibernate and I can't get it to work, even though I've tried all combinations of options - cascade, unique, constrained, etc. I've read through other posts like Hibernate one to zero or one mapping and https://forum.hibernate.org/viewtopic.php?t=949458 but they are about querying the data and I'm having trouble with insert/updates. Maybe someone can help me with the right configuration.

I have person and data classes :

Code:
class Person {
   int    id;
   String type;
   Data   someData;
}

class Data {
    int    personId;
    Person person;
    . . .
}


And the mapping :

Code:
<hibernate-mapping>
<class name="Person" table="person">
    <id name="id" type="java.lang.Integer">
        <column name="ID" />
        <generator class="identity" />
    </id>
    <property name="type" type="string">
        <column name="type"/>
    </property>
    <many-to-one name="someData" class="Data" column="id"
               unique="true" insert="false" update="false"
               cascade="all"/>
</class>

<class name="Data" table="data">
    <id name="personId" column="personId" type="java.lang.Integer">
        <generator class="foreign">
            <param name="property">person</param>
        </generator>
    </id>
    <one-to-one name="person" class="Person" constrained="true"/>
</class>

Other requirements :

    1) I want to use the same ID in both tables, generated by the DB on the initial Person insert.
    2) If type == 'a', then the Person will have 'someData' attached to it. Otherwise there is no 'someData' object nor 'data' row.
    3) Sometimes type == 'a' is known during creation; sometimes type is set to 'a' after the initial Person insert, in which case a new 'data' row must be inserted. Type may also be changed to 'b', in which case the 'data' row should be removed (but I really don't care if it hangs around.)

I dont think this should be so complicated, but somehow it is. What I found is :

1) When my form is submitted, struts2 builds a new 'Person' and attaches a new 'someData' object to it no matter what, since struts2 doesnt know about the 'type==a' rule. (Q - Can I make struts2 build it conditionally? Also, how does Hibernate persistence work when struts2 builds new objects every time? Doesnt that totally defeat the persistence of an object in memory being linked to a DB row??) Anyway, because of this, I can't have Hibernate create a 'data' row based on the 'someData' object existing. So MY code checks if 'type == 'a' and if so, calls saveOrUpdate with the 'someData' object.

2) I have to set the backward pointing link from someData->Person. Why? I don't use this link in my code - is there a way to configure this scenario without the backwards someData->Person pointer?

3) The real problem I have is with the ID of 'someData', and whether or not my code needs to set it.

==> If Person exists and the user changes type to 'a', someData' now has to be created in the DB. If I set the Id attribute of someData, Hibernate tries to Update the row, and returns an error saying since the row in 'data' table doesn't exist. If I DON'T set the Id attribute of someData, it will be inserted correctly with the Person ID.

==> OK, so my code won't set the Id attribute of someData. But then, if someData already existed in the DB so this operation is an update, Hibernate returns an error saying 'Duplicate row' since without the Id set in the object, it tries to insert the row with the Person Id. So I can't set the Id, and I can't NOT set the Id. ???

How can I configure Hibernate or change my code to have this work correctly?


Top
 Profile  
 
 Post subject: Re: How to model Hibernate one to zero or one relationship
PostPosted: Wed Oct 29, 2014 9:48 am 
Newbie

Joined: Sat Oct 25, 2014 10:31 am
Posts: 7
There is property-ref attribute specified in one-to-one tag in JPWH book. You haven't specifiy it. Maybe it is the cause.


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