-->
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.  [ 7 posts ] 
Author Message
 Post subject: Deleting objects in a mixed environment
PostPosted: Tue Nov 29, 2005 4:57 pm 
Beginner
Beginner

Joined: Tue Nov 29, 2005 4:42 pm
Posts: 49
Location: Atlanta, GA
I'm trying to integrate hibernate slowly and introduce it incrementally. I have a some objects that I'm using hibernate to persist and some objects I'm not using hibernate to persist. My problem is when I have foriegn key constraints across those boundaries. I'm having trouble when I delete one of my hibernate objects it's used in other tables.

I'd like to set those foreign keys to NULL when I delete my hibernate objects. It seems that hibernate would handle this for me if I was mapping both objects through hibernate. What is the best strategy for tackling this boundary problem?

I've tried executing SQL queries with session.createSQLQuery(), but since I'm doing an update call it didn't work. I then tried using createQuery(), but I still had trouble figuring it out. The documentation doesn't really have much in the way of doing your own updates.

Thanks
Charlie


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 29, 2005 5:23 pm 
Regular
Regular

Joined: Thu Oct 27, 2005 8:06 am
Posts: 55
Location: München, Germany
Not knowing which errors you got concretely, I can't tell you the exact reason, but I can guess a few things.

I never did that mixed kind of thing, but I both use Hibernate in a project where associations are mapped in Hibernate, and in an EJB2 project where no associations are present, but Java objects have foreign object ids (in contrast to object references).

Both works well with Hibernate. If your Java object has a foreign key, then this has a primitive data type (like Long or String). Setting, updating, or nulling this key is a regular field update from Hibernate's view, and it works smoothly using any of Hibernate's update operations.

If your Java object has references to another Hibernate-mapped Java object, again there is no problem. You use the <set...>, <many-to-one...> etc. constructs.

I guess your problem arises because your Java objects do have references to other Java objects (so you can't map them as primitive data types), but Hibernate doesn't know about one of these objects. Now, one of the core functions of its mapping logic is transforming Java object references to and from foreign keys. If one of the classes isn't known to Hibernate (including its table and key column), how should it map the association?

Therefore, your problem probably doesn't have to do with which update operation you use, but lies deeper in the mapping of those references at the boundary of Hibernate-mapped and non-H-mapped. I don't know how many classes and tables you have, but probably it would be the most time-effective way to clench your teeth and map all of them.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 29, 2005 5:46 pm 
Beginner
Beginner

Joined: Tue Nov 29, 2005 4:42 pm
Posts: 49
Location: Atlanta, GA
To give a little more detail I have the following situation:

Asset(Not Hibernate) ----> AssetOwner(Hibernate)

Many Assets can have a single AssetOwner

So Asset has a foreign key with AssetOwner. When I delete an AssetOwner I want all Assets managed by that owner to simply be set to NULL. The only way I see this working is by grabbing session.connection(), and getting familiar with JDBC all over again before I call session.delete( owner ). I was just wondering if I there was a easier way in handling this through hibernate mappings.

Thanks
Charlie


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 29, 2005 6:06 pm 
Regular
Regular

Joined: Thu Oct 27, 2005 8:06 am
Posts: 55
Location: München, Germany
If you had mapped Asset in Hibernate as well, you would work the other way round: set the association to AssetOwner to null programmatically, and let Hibernate decide to delete the AssetOwner via a cascade="delete-orphan".

The way you are describing it, I wonder what support you expect. As long as Hibernate doesn't know about Asset, who should assist you in setting any attributes to null?

By the way, even if all persistent classes are mapped in Hibernate, it can still happen that a non-persistent object holds a reference to a Hibernate-managed object. Just imagine a temporary object in the web tier holding a reference to a business object. That requires caution, as there are situations in which Hibernate will create a new Java object for the persistent object, not knowing that a non-Hibernate object has still a reference to the old one.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 30, 2005 9:39 am 
Beginner
Beginner

Joined: Tue Nov 29, 2005 4:42 pm
Posts: 49
Location: Atlanta, GA
Quote:

PostPosted: Wed Nov 30, 2005 3:06 am Post subject:
If you had mapped Asset in Hibernate as well, you would work the other way round: set the association to AssetOwner to null programmatically, and let Hibernate decide to delete the AssetOwner via a cascade="delete-orphan".


Well that's not really how the UI works. A user can delete an AssetOwner at any point, and when that happens I need to set all Assets owned by that AssetOwner to NULL. I was wondering if I could add some mappings to AssetOwner to give it hints about Asset's foreign key without having to explicitly map Asset. And when I delete AssetOwner it would know to do something like:

update Asset set AssetOwnerID=null where AssetOwnerID=?

From reading the documentation it sounds like Hibernate would do that if I had mapped out Asset. I just don't know if it would do it when I haven't mapped Asset.

Right now I'm doing manual SQL which is my least favorite, but I know I'm in a funny situation in the way I'm using hibernate.

Thanks for your help
Charlie


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 30, 2005 1:32 pm 
Beginner
Beginner

Joined: Tue Nov 29, 2005 4:42 pm
Posts: 49
Location: Atlanta, GA
I guess after reading through the hibernate documentation I'm missing some mappings. Really AssetOwner is a one-to-many relationship with Asset. However, I don't really have the need to include a collection of Assets that an AssetOwner owns. I wouldn't ever need to have that collection other than the purposes of deleting it I suppose.

So, I guess one way of thinking about my problem would be can I include some markup about a relationship for which there is no member variable?

Normally if I mapped this it would be uni-directional Asset has a one-to-one mapping with AssetOwner because one Asset can have only one AssetOwner. So how do I tell hibernate that Asset is not in charge of the lifetime of AssetOwner? So if I delete AssetOwner, Asset's reference is set to NULL?

Thanks again,
Charlie


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 30, 2005 3:45 pm 
Regular
Regular

Joined: Tue Oct 28, 2003 8:25 am
Posts: 72
Location: Belgium
Just remove the Asset from the AssetOwner's set and make sure the set's cascading property is none or save-update.

See http://www.hibernate.org/hib_docs/v3/re ... ns-mapping


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