Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
3.1.2
[
Oracle 9.2
Hi all,
This is more of a general question. Its probably pretty obvious, and I'm totally missing the point, but I searched the forum for similar stuff and didn't find anything, which probably means I'm really bad at searching ;)
I'm wondering if it is correct to do this. Say you have a main object, that is joined to linked objects, one-many. I.e. the linked objects hold the key to the main obj. Lets also say adding these links can be built up over subsequent session interactions, in the same or different session.
You update the linked objs by adding and deleting from the set, bag and list objects in the main object, in java-land, by adding/deleting to the sets, etc.
My question is, do you need to save off the individual objects separately to the main object, or can you cause the linked objects to be created/updated/deleted by manipulating the collections of these objects in the main object and merely saving the main object, by exploiting the cascade attribute?
Its just that I've been trying to do this, but I always run into difficulties, which I can usually solve by saving the linked object and its link after saving the main object, with the links removed first. And I have to mess around with the cascade and inverse attributes on the set/bag/list nodes. But I get it working, by writing ugly code to compensate.
Usually the main issue I encounter is where it is trying to insert the linked records before the main one. and as they hold the main object link, thats null, and then it inserts the main object, and the linked object main ref is null. This is because I've done stuff like
main = new Main();
link.setMain(main);
main.getLinks().add(link);
main.save();
So it createds link, then creates main, and link.mainid is null in the database.
Am I misusing hibernate trying to do it this way? The docu does not indicate that I can do this directly. Or is this the beauty of hibernate that you can do this, and I've totally missed it?
Apologies if this has been covered before, it must have been as its fundamental to how you use it. If I've got it wrong, thats a lot of code I'll have to rewrite :)