Quantcast

Using the same hibernate session across services

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Using the same hibernate session across services

34m0
I'll try to articulate this one as best I can.

For a simple example, let's say I have three entities:

Country, County, and City

with relationships like:

Country {
        hasMany = [
                counties:County,
                bigCities:City
        ]
}

County {
        City bigCity
        Country country
}

City {
        County county
        Country country
}

and I have services:

cityService {
        def delete(City city) {
                city.country.removeFromBigCities(country)
                city.county.bigCity = null
                city.delete()
        }
}

countyService {
        def delete(County county) {
                cityService.delete(county.bigCity)
                county.country.removeFromCounties(county)
                county.delete()
        }
}

There might be some inaccuracies in the above, but you can take it that
if I just want to delete a city the cityService works, if I want to
delete a county with no bigCity it also works, but if I want to delete a
county with a bigCity it fails.

I get an exception: deleted object would be re-saved by cascade

Why does if fail? Because the county.country, and city.country are
acting as different objects even though they are not. When the
cityService removes the city from the country, the county service does
not recognize this.

I guess it is just the way Hibernate is, but it seems wrong to me. What
is the point of the hibernate session, if city.country and
county.country are not the same object in memory?



--
Eamonn O'Connell
Solutions Architect
http://www.eazybusiness.com
+353 1 403 8487
3015 Lake View Drive, Citywest Business Campus, Co. Dublin, Ireland


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Using the same hibernate session across services

Gordon Ross
On 24 Aug 2010, at 12:55, Eamonn O'Connell wrote:

> I guess it is just the way Hibernate is, but it seems wrong to me. What
> is the point of the hibernate session, if city.country and
> county.country are not the same object in memory?


This is the "joy" of Hibernate ;-)

You can prove this yourself by seeing that city.country!=county.country (yet they will describe the same database row)

GTG
--
Gordon Ross.
Telecomms Office,
Cambridge University.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Using the same hibernate session across services

34m0
In reply to this post by 34m0
I was actually wrong here. Luckily I came across this jira:
http://jira.codehaus.org/browse/GRAILS-5778

The problem lay in the removeFrom method, or my use of it.

By implementing equals and hashCode for the City entity, it started
working.



On Tue, 2010-08-24 at 12:55 +0100, Eamonn O'Connell wrote:

> I'll try to articulate this one as best I can.
>
> For a simple example, let's say I have three entities:
>
> Country, County, and City
>
> with relationships like:
>
> Country {
> hasMany = [
> counties:County,
> bigCities:City
> ]
> }
>
> County {
> City bigCity
> Country country
> }
>
> City {
> County county
>         Country country
> }
>
> and I have services:
>
> cityService {
> def delete(City city) {
> city.country.removeFromBigCities(country)
> city.county.bigCity = null
> city.delete()
> }
> }
>
> countyService {
> def delete(County county) {
> cityService.delete(county.bigCity)
> county.country.removeFromCounties(county)
> county.delete()
> }
> }
>
> There might be some inaccuracies in the above, but you can take it that
> if I just want to delete a city the cityService works, if I want to
> delete a county with no bigCity it also works, but if I want to delete a
> county with a bigCity it fails.
>
> I get an exception: deleted object would be re-saved by cascade
>
> Why does if fail? Because the county.country, and city.country are
> acting as different objects even though they are not. When the
> cityService removes the city from the country, the county service does
> not recognize this.
>
> I guess it is just the way Hibernate is, but it seems wrong to me. What
> is the point of the hibernate session, if city.country and
> county.country are not the same object in memory?
>
>
>

--
Eamonn O'Connell
Solutions Architect
http://www.eazybusiness.com
+353 1 403 8487
3015 Lake View Drive, Citywest Business Campus, Co. Dublin, Ireland


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Loading...