Using DELETE_ORPHAN did the trick, thanks very much! The only problem left is that when I create a new ObjectA and at the same time associate AAndBJoin objects (as opposed to first saving a new ObjectA and then editing it and associating AAndBJoin objects), the associated AAndBObjects are saved in the database with idObjectA=null, thus they are not properly associated. Why? Because at the moment ObjectA.save() is called, the new ObjectA has no id, so Hibernate is unable to properly save any associated AAndBJoin objects because it needs to fill in the idObjectA field.
I've tried using different combinations of Hibernate Annotations -- CascadeType.ALL, CascadeType.SAVE_UPDATE -- while always maintaining the CascadeType.ALL on the JPA @OneToMany Annotation and the CascadeType.DELETE_ORPHAN Hibernate Annotation, nothing seems to work. Do I have to somehow manually save the ObjectA first and then call save again once I have its id? Even if this were the solution it begs the question, why isn't Hibernate smart enough to do this all at once? Regardless, I don't think this will work because if I am not mistaken the save() method in the GenericDAO calls persist() (instead of save()), thus not guaranteeing that an id will be assigned for new objects until a flush() is called. So I would have to call flush manually? How would I do this in a Spring Controller? Is this good practice? (It seems like it is not) I am close, so close, I hope someone can help me nail this last problem! Bob syg6 wrote: > > Hmmm. Yea, I already had this set at one time and it didn't seem to be > doing anything so I got rid of it. Looking again at the documentation it > seems DELETE_ORPHAN isn't only for deleting from Collections when the > Parent object is deleted (as I thought) but is also used when any element > is removed from the Collection itself. That might just do the trick then, > who knows? As I said, up until a couple hours ago, when I gave up on > manually adding/updating/deleting elements in the Collection, I was under > the impression that simply having Cascade.ALL on the getter in my Parent > class wasn't working. I could swear it wasn't! But now it is. So perhaps > if I once again put the Hibernate DELETE_ORPHAN Annotation, it may just > work! > > At any rate, it will have to wait until Monday, as work is over. I'll let > you (the list) know then. > > Many thanks, once again, for the tip! > > Bob > > ps. What's everyone's feeling about mixing-and-matching JPA with Hibernate > Annotations? And why the heck doesn't JPA have anything similar to begin > with? Just curious ... > > > Mike Horwitz wrote: >> >> You may want to try adding the >> org.hibernate.annotations.CascadeType.DELETE_ORPHAN ( a Hibernate >> specific >> annotation) that should do what you want. See here for more details: >> http://tinyurl.com/29m6ek. >> >> Mike. >> >> On 10/26/07, syg6 <[EMAIL PROTECTED]> wrote: >>> >>> >>> Well I've been playing with this all week and, quite honestly, I was >>> very >>> close to a nervous breakdown and was cursing Hibernate up and down. I >>> gave >>> up on relying on Cascade and decided to manually manage my associated >>> Set >>> of >>> AAndBJoin Objects. >>> >>> This proved to be a royal pain in the a$$. I was getting all kinds of >>> errors >>> when trying to save a new ObjectA, or add a new AAndBJoin to an existing >>> ObjectA. It was a nightmare. So much so that I decided to say 'screw it' >>> and >>> go back to relying on Hibernate. >>> >>> And for some reason, and without having changed anything, it now it >>> works, >>> or mostly. Of my three main problems: >>> >>> 1. duplicated AAndBJoin Objects when saving an existing ObjectA that has >>> existing AAndBJoin(s) >>> 2. changes made in any AAndBJoin in objectaform.jsp not being updated >>> when >>> saving the ObjectA >>> 3. AAndBJoin(s) deleted in objectaform.jsp not being deleted from >>> database >>> when saving ObjectA >>> >>> ... only (3) remains. Do I have to somehow manually delete from the >>> database >>> any AAndBJoin deleted in the .jsp? I figured Hibernate would say 'oh, I >>> see >>> you had 2 AAndBJoins and now only 1 has been sent from the form/request, >>> so >>> I'll just go ahead and delete the one that's missing ...', but it seems >>> it >>> doesn't. >>> >>> Am I wrong? >>> >>> Here is the mapping: >>> >>> ObjectA: >>> private Set<ObjectAAndBJoin> objectAAndBJoins; >>> >>> @OneToMany(cascade = { CascadeType.ALL },mappedBy="objecta",fetch = >>> FetchType.EAGER) >>> getObjectAAndBJoins() >>> >>> ObjectB: >>> private Set<ObjectAAndBJoin> objectAAndBJoins; >>> >>> @OneToMany(mappedBy="objectb") >>> public Set<ObjectAAndBJoin> getObjectAAndBJoins () >>> >>> ObjectAAndBJoin: >>> private ObjectA objectA; >>> private ObjectB objectB; >>> >>> @ManyToOne >>> @JoinColumn(name="idObjectA") >>> public ObjectA getObjectA() >>> >>> @ManyToOne >>> @JoinColumn(name="idObjectB") >>> public ObjectB getObjectB() >>> >>> >>> Many thanks!!! >>> Bob >>> >>> >>> syg6 wrote: >>> > >>> > I spoke too soon ... >>> > >>> > While Hibernate is no longer duplicating my data, it's also not: >>> > >>> > 1. updating any changes made >>> > 2. deleting >>> > >>> > In the first case, I see that all my data arrives correctly, passes >>> > through my Custom PropertyEditor, and just before calling save() in >>> the >>> > onSubmit() method, the values are correct. But no update is made. I >>> tried >>> > putting (cascade=CascadeType.ALL) on the getters in AAndBJoin, it >>> doesn't >>> > work. >>> > >>> > I turned Hibernate SQL debugging on and see that the table is being >>> > updated, but the line printed looks like this: >>> > >>> > update AAndBJoin set idObjectA=?, idObjectB=?, text=? where >>> idAAndBJoin=? >>> > >>> > Is there any way to see what values the PreparedStatement is using? >>> > >>> > As far as deleting goes, same deal. Just before saving I see the >>> correct >>> > number of AAndBJoin Objects associated with ObjectA. But after saving >>> and >>> > returning to the page, any AAndBJoin Object I have deleted reappears. >>> If >>> I >>> > delete ALL AAndBJoin Objects Spring doesn't even call my Custom >>> > PropertyEditor because there's no data to process. So the original >>> list >>> of >>> > AAndBJoin Object associated with ObjectA is left untouched. Bummer. >>> > >>> > I feel like the Godfather. Every time I think I'm out Hibernate sucks >>> me >>> > back in. :( >>> > >>> > Thanks for any help! >>> > Bob >>> > >>> > >>> > >>> > syg6 wrote: >>> >> >>> >> That was it! Makes perfect sense now that someone else suggested it >>> to >>> >> me! >>> >> >>> >> Thanks again! >>> >> >>> >> Bob >>> >> >>> >> >>> >> Mike Horwitz wrote: >>> >>> >>> >>> Are you passing the id in the call from your jsp? If you are not, >>> then >>> >>> Hibernate will think these are new entities and save rather than >>> update >>> >>> them. >>> >>> >>> >>> Mike >>> >>> >>> >>> On 10/22/07, syg6 <[EMAIL PROTECTED]> wrote: >>> >>>> >>> >>>> >>> >>>> Yep. I left it out for brevity. Besides, I don't think Hibernate >>> would >>> >>>> be >>> >>>> able to do the inserts if I hadn't defined a PK, no? >>> >>>> >>> >>>> Anyway, here it is: >>> >>>> >>> >>>> @Column(name="idAAndBJoin") >>> >>>> @Id @GeneratedValue(strategy = GenerationType.AUTO) >>> >>>> public Long getId() >>> >>>> >>> >>>> Man, I got quite excited for a minute there. But alas, that's not >>> the >>> >>>> problem ... >>> >>>> >>> >>>> Bob >>> >>>> >>> >>>> >>> >>>> Mike Horwitz wrote: >>> >>>> > >>> >>>> > Have you assigned a primary key field to your AAndBJoin object? >>> >>>> > >>> >>>> > Mike. >>> >>>> > >>> >>>> > On 10/22/07, syg6 <[EMAIL PROTECTED]> wrote: >>> >>>> >> >>> >>>> >> >>> >>>> >> Hello listers. >>> >>>> >> >>> >>>> >> I have posted on this before. I got no replies, either here or >>> over >>> >>>> at >>> >>>> >> hibernate.org. So I parked the issue and moved on but now I have >>> to >>> >>>> deal >>> >>>> >> with it. Because of my limited Hibernate knowledge, I am not >>> even >>> >>>> really >>> >>>> >> sure of what things to try anymore. Can someone shed some light >>> on >>> >>>> this >>> >>>> >> problem for me? >>> >>>> >> >>> >>>> >> I have two Objects, ObjectA and ObjectB. They have a ManyToMany >>> >>>> >> relationship >>> >>>> >> with attributes, meaning they have a join table with IdObjectA >>> and >>> >>>> >> IdObjectB >>> >>>> >> as foreign keys and some attributes. As recommended by the >>> Hibernate >>> >>>> >> docs, >>> >>>> >> I >>> >>>> >> create a class, AAndBJoin we'll call it. Here's what it all >>> looks >>> >>>> like: >>> >>>> >> >>> >>>> >> public class ObjectA >>> >>>> >> { >>> >>>> >> private Set<AAndBJoin> aAndBJoins; >>> >>>> >> >>> >>>> >> @OneToMany(mappedBy="ObjectA", fetch = FetchType.EAGER, cascade >>> = >>> { >>> >>>> >> CascadeType.ALL }) >>> >>>> >> public Set<AAndBJoin> getAAndBJoins () >>> >>>> >> >>> >>>> >> public class ObjectB >>> >>>> >> { >>> >>>> >> private Set<AAndBJoin> aAndBJoins; >>> >>>> >> >>> >>>> >> @OneToMany(mappedBy="ObjectB") >>> >>>> >> public Set<AAndBJoin> getAAndBJoins () >>> >>>> >> >>> >>>> >> public class AAndBJoin >>> >>>> >> { >>> >>>> >> private ObjectA objectA; >>> >>>> >> private ObjectB objectB; >>> >>>> >> >>> >>>> >> @ManyToOne >>> >>>> >> @JoinColumn(name="idObjectA") >>> >>>> >> public ObjectA getObjectA() >>> >>>> >> >>> >>>> >> @ManyToOne >>> >>>> >> @JoinColumn(name="idObjectB") >>> >>>> >> public ObjectB getObjectB() >>> >>>> >> >>> >>>> >> The problem is that when I save an ObjectA, with its associated >>> Set >>> >>>> of >>> >>>> >> AAndBJoin Objects, ONLY inserts are done in the AAndBJoin table >>> in >>> >>>> the >>> >>>> >> database, any existing information is NOT being updated or >>> deleted. >>> >>>> So >>> >>>> if >>> >>>> >> I >>> >>>> >> start off with 2 AAndBJoin Objects and add 1, instead of having >>> 3 >>> I >>> >>>> now >>> >>>> >> have >>> >>>> >> 5 because 3 inserts are performed, one for each AAndBJoin Object >>> >>>> sent >>> >>>> to >>> >>>> >> the >>> >>>> >> FormController from the .jsp, but the original 2 are left >>> untouched. >>> >>>> >> >>> >>>> >> Do I have to play with the AAndBJoin Object, like add Cascade >>> >>>> information >>> >>>> >> or >>> >>>> >> something else? In the example there was none, and I have tried >>> >>>> putting >>> >>>> >> Cascade.ALL for the two getters, no dice. Or perhaps I have to >>> do >>> >>>> >> something >>> >>>> >> manually, like call session.flush() or something, to get >>> Hibernate >>> >>>> to >>> >>>> >> (worst >>> >>>> >> case scenario) delete the info in the AAndBJoin table first >>> before >>> >>>> >> inserting? I am not sure even how to do this since Spring is in >>> the >>> >>>> mix. >>> >>>> >> I >>> >>>> >> have tried debugging Hibernate to see what's going on but since >>> >>>> Spring >>> >>>> is >>> >>>> >> involved and uses Reflection and AopProxies and all kinds of >>> stuff >>> I >>> >>>> >> haven't >>> >>>> >> a clue about, I never actually see any Hibernate code executed >>> ... >>> >>>> >> >>> >>>> >> I would really appreciate some help on this one! >>> >>>> >> >>> >>>> >> Bob >>> >>>> >> -- >>> >>>> >> View this message in context: >>> >>>> >> >>> >>>> >>> http://www.nabble.com/Many-to-many-collection-problem-tf4670322s2369.html#a13341640 >>> >>>> >> Sent from the AppFuse - User mailing list archive at Nabble.com. >>> >>>> >> >>> >>>> >> >>> >>>> >>> --------------------------------------------------------------------- >>> >>>> >> To unsubscribe, e-mail: [EMAIL PROTECTED] >>> >>>> >> For additional commands, e-mail: [EMAIL PROTECTED] >>> >>>> >> >>> >>>> >> >>> >>>> > >>> >>>> > >>> >>>> >>> >>>> -- >>> >>>> View this message in context: >>> >>>> >>> http://www.nabble.com/Many-to-many-collection-problem-tf4670322s2369.html#a13343413 >>> >>>> Sent from the AppFuse - User mailing list archive at Nabble.com. >>> >>>> >>> >>>> >>> --------------------------------------------------------------------- >>> >>>> To unsubscribe, e-mail: [EMAIL PROTECTED] >>> >>>> For additional commands, e-mail: [EMAIL PROTECTED] >>> >>>> >>> >>>> >>> >>> >>> >>> >>> >> >>> >> >>> > >>> > >>> >>> -- >>> View this message in context: >>> http://www.nabble.com/Many-to-many-collection-problem-tf4670322s2369.html#a13430091 >>> Sent from the AppFuse - User mailing list archive at Nabble.com. >>> >>> --------------------------------------------------------------------- >>> To unsubscribe, e-mail: [EMAIL PROTECTED] >>> For additional commands, e-mail: [EMAIL PROTECTED] >>> >>> >> >> > > -- View this message in context: http://www.nabble.com/Many-to-many-collection-problem-tf4670322s2369.html#a13463480 Sent from the AppFuse - User mailing list archive at Nabble.com. --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]