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]

Reply via email to