-->
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 to add to Set when id is identity before insert
PostPosted: Mon Sep 13, 2004 4:14 am 
Newbie

Joined: Mon Sep 13, 2004 3:40 am
Posts: 2
[b]Hibernate version:2.6.1[/b]
I have one to many Set relationship between AirItinerary and FlightItem.

Construct Data pass to save method:
airItinerary.getFlightItems().add(flightItem1);
airItinerary.getFlightItems().add(flightItem2);// will not add since id unknow and Set will not add
New Saver saver=new Saver();
saver.save(airItinerary);

Class:Saver
public AirItinerary save(AirItinerary data){ session.openSesson() session.save(data); session.close()}

I need to construct AirItinerary Data with FlightItems in it to pass to save(). Since FlightItem.id generated by identity, so I do not know id before save. Since I use Set relationShip and FlightItem equal() use Id to compare, Set.add will not add another to HashSet. How to do that?

public boolean equals(Object other) {
if ( !(other instanceof FlightItem) ) return false;
FlightItem castOther = (FlightItem) other;
return new EqualsBuilder()
.append(this.getId(), castOther.getId())
.isEquals();
}

[b]Mapping documents:[/b]
<class
name="com.netsboss.marsrover.biz.hibernate.AirItinerary"
table="AirItinerary"
>
<meta attribute="class-description" inherit="false">
@hibernate.class
table="AirItinerary"
</meta>

<id
name="id"
type="java.lang.Integer"
column="Id"
unsaved-value="0"
>
<generator class="identity" />
</id>

<property
name="depCity"
type="java.lang.String"
column="depCity"
not-null="true"
unique="true"
length="3"
>
</property>
<property
name="arrCity"
type="java.lang.String"
column="arrCity"
not-null="true"
unique="true"
length="3"
>
</property>
<property
name="depDate"
type="java.util.Date"
column="depDate"
not-null="true"
unique="true"
length="16"
>
</property>

<!-- Associations -->

<!-- bi-directional one-to-many association to FlightItem -->
<set
name="flightItems"
lazy="true"
inverse="true"
cascade="none"
>
<key>
<column name="parentId" />
</key>
<one-to-many
class="com.netsboss.marsrover.biz.hibernate.FlightItem"
/>
</set>
<!-- bi-directional one-to-many association to CabinPrice -->
<set
name="cabinPrices"
lazy="true"
inverse="true"
cascade="none"
>
<key>
<column name="superId" />
</key>
<one-to-many
class="com.netsboss.marsrover.biz.hibernate.CabinPrice"
/>
</set>

</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 13, 2004 5:21 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
change equals & hashcode using business key

http://www.hibernate.org/109.html

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 27, 2004 7:11 pm 
Newbie

Joined: Mon Sep 27, 2004 6:26 pm
Posts: 3
I'm not so sure I like the solution of having business keys for a few reasons:

1. It creates different equality algorithms between your database and your application code. You could concievably end up in a situation where 2 objects are equal according to the database and not in the code, which could lead to some tough to debug problems.

2. Some objects have no business key. We have some entities that could contain all the same data and still be 2 different objects.

We have been thinking about this for a couple of days and could only come up with 2 other solutions, some with problems of there own.

1. Use a UU Id algorithm and assign the id in the constructor. This is probably the best solution we have come up with. I believe it solves all the major problems with equals and hashCode, assuming you are wiling and able to use UU ids.

Does anyone know an easy way of getting a hold of the id generator in hibernate? It would be nice to be able to use it, but if not then the java.rmi.server.UID can also be used.

2. We can use the Object.equals and Object.hashCode algorithms for new instances (with no id), when an id is assigned the entity can then use it for equals and hashCode.

Obviously this has the same problem already discussed. For this issue, i've built a HashSet implementation that implements Observer and adds itself as an observer to any entries that extend from Observable. This way, all the entity has to do is notify its observers when its key changes and my custom hash set can move it to the correct bucket.

The problem i'm running into with this is that it doesn't seem possible to tell hibernate to use a custom implementation of Set instead of HashSet. Is there a way around this?



Has anyone else come up with any better solutions?

Thanks,

Tom Litton


Top
 Profile  
 
 Post subject: UUID alright but not quite
PostPosted: Fri Feb 04, 2005 1:24 am 
Regular
Regular

Joined: Mon Nov 03, 2003 6:10 am
Posts: 75
We use UUID for all our objects and I toyed with the concept of having the SETTER use a java algorithm (or db call 'newid()') to set the id of the objects so they could be added to the Set. Unfortunately then you can't use saveorupdate anymore because it would assume that it was already existing. I was considering a big hack... using an interceptor to check the key value and if it matched some pattern ({UUID}_TEMP) for instance, nulling it prior to save. But it does't appeal to me.

Have you gotten a better solution yet?


Top
 Profile  
 
 Post subject: solution maybe
PostPosted: Fri Feb 04, 2005 1:28 am 
Regular
Regular

Joined: Mon Nov 03, 2003 6:10 am
Posts: 75
Now that I gave it another look and after reading: http://www.hibernate.org/109.html

What about adding another attribute called transientId and modifying the hashcode to use it and the primary key (id) together. The transientId would have no mapping and would exist only in the POJO - specifically to act as a distiguisher for adding these unsaved keyless objects to the set.

I think that would work..


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.