-->
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: bidirectional relationship and indexed collection
PostPosted: Mon Jun 20, 2005 1:19 pm 
Newbie

Joined: Sun Oct 24, 2004 8:53 pm
Posts: 6
Location: Philadelphia, PA
I have a class TimeUnit and another class TimePeriod. The way they are related are one to many, with a bidirectional relationship.

public class TimeUnit {
private List timePeriods;

}

public class TimePeriod {
private TimeUnit getTimeUnit();

}

TimeUnit class contains a list of TimePeriod instances, and each TimePeriod has a back reference to the containing TimeUnit object.

Using hibernate's examples I have set up the one to many relationship such that on the side of collection, I have inverse=true.

<list
name="timePeriods"
inverse="true"
>
<key column="TP_TU_ID"/>
<index column="TP_ORDER"/>
<one-to-many class="TimePeriod"/>
</list>

Because of inverse=true the order does not get saved for the timeperiod objects. What do I do in this case ? I need to have the list there, I need to also have timeperiod having reference to timeunit, and I need to also leave order manipulation up to hibernate.

How can I achieve all this ? Has anyone come across such case ? The hibernate-in-action says you cannot have an indexed collection on the inverse=true side. What's the workaround without losing any of my 3 goals ?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 20, 2005 4:39 pm 
Newbie

Joined: Thu Jun 16, 2005 4:25 pm
Posts: 3
What we did was keep an int "orderIndex" member variable in what would be your TimePeriod class.

Add it as a <property> in your Hibernate mapping file to TimePeriod, but the accessors in the code like:

Code:
    public int getOrderIndex() {
        if (parent != null)
            return parent.getTimePeriods().indexOf(this);
        else
            return -1;
    }
    protected void setOrderIndex(int orderIndex) {
        // Calculated
    }


Good luck!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 20, 2005 4:43 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
In HIbernate3 you can use:


<list name="timePeriods">
<key column="TP_TU_ID" not-null="true"/>
<index column="TP_ORDER"/>
<one-to-many class="TimePeriod"/>
</list>


And set the other end of the relationship insert="false" update="false"


Top
 Profile  
 
 Post subject: inverse=true removed?
PostPosted: Tue Jun 21, 2005 10:02 am 
Newbie

Joined: Sun Oct 24, 2004 8:53 pm
Posts: 6
Location: Philadelphia, PA
gavin wrote:
In HIbernate3 you can use:

<list name="timePeriods">
<key column="TP_TU_ID" not-null="true"/>
<index column="TP_ORDER"/>
<one-to-many class="TimePeriod"/>
</list>


And set the other end of the relationship insert="false" update="false"


The biggest difference I see in this and the mapping that I had posted, is that inverse=true is removed. Was that the idea ? If that is the case, I can remove the inverse attribute from my mapping also - without adding not-null="true" to <key> and not adding insert=false, update=false to many-to-one.

This is how the other side of association looks like in TimePeriod class
<many-to-one name="timeUnit"
class="TimeUnit"
column="TP_TU_ID"
not-null="true" />

I tried out the suggestion but it creates following sql
Hibernate: insert into TIME_UNITS (VERSION, NAME, TU_ID) values (?, ?, ?)
Hibernate: insert into TIME_PERIODS (VERSION, TP_TU_ID, TP_ORDER, TP_ID) values (?, ?, ?, ?)
Hibernate: insert into TIME_PERIODS (VERSION, TP_TU_ID, TP_ORDER, TP_ID) values (?, ?, ?, ?)
Hibernate: update TIME_PERIODS set TP_TU_ID=?, TP_ORDER=? where TP_ID=?
Hibernate: update TIME_PERIODS set TP_TU_ID=?, TP_ORDER=? where TP_ID=?

which is same as if I just remove inverse=true from my current mapping.
The question is should I remove the inverse? In that case hibernate would think the two associations are different and do the INSERT for one and UPDATE for another, for every timeperiod object I have. Is that good?


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.