I have an object, say a person, and the db has a list of foods that a person might eat in another table. The table that holds person ids and food ids has no primary key, so I'm using a composite-id of person id and food id.
In the program I want to add/change the foods a person might eat by creating a set of food items and then persisting the person. From what I've read, I need to implement hashCode() and equals(), which I've done. With just those things done, persisting the person leads to hibernate updating the PersonFood objects in the set, not inserting. I have read in the Hibernate docs that I have to implement Interceptor.isUnsaved() in order for it to know whether to update the items or insert. Clearly this means deleting items that are no longer in the set isn't being taken care of either.
Is there a better way than using Interceptor? I'd rather not implement all of its methods. If I do, will it delete the entries that are no longer in the set or will I have to do that myself? Also, does creating an id object have any actual impact. Since the PersonFood object only contains the members that make up its composite-id it seems redundant to create an id object, but the Hibernate docs seem to suggest you need to do it to insert the objects.
Thanks to anyone who can help,
Todd
PS people and food is in fact just the example.
|