• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Both saveOrUpdate and merge won't work

 
Ray Xie
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there!

Greetings! I was wondering if somebody can help me with this. I'm using Spring and Hibernate 3.1.1.

I have a class, let's call it A, which has many-to-many relationship with B, and one-to-many relationship with C.



Then in the mapping file of A:



Now, I get A, and then populate a LazyValidatorForm with it, and display it in a JSP. In JSP, when I click Save, the modified form will get back into my Action, which will populate A, and call saveOrUpdate().

Here's the problem:

1. For all primitive types this works fine.
2. For many-to-many with B, this works too, if I remove a B from A, it gets deleted from DB.
3. However... if I delete a C, it doesn't work. I can *update* a C, e.g.: one of C's name, and it'll be reflected in the DB. However when I *delete* a C, this fact is never reflected in the DB at all.

Any idea why? I did a search high and low, in the end, I try to call merge() instead of saveOrUpdate... and I got a:



instead. Sigh! Can somebody help, kindly?

Thank you,
Ray
 
Ray Xie
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've also just upgraded my Hibernate to 3.1.3, same thing.

What I wonder is, what is the rationale behind this, if this is indeed as-designed behavior? Because apparently I'm missing something really big here--if it's really a bug, there's no way Hibernate can go to version 3 without this getting fixed... right?

Thank you in advance for any help/enlightenment,
Ray
 
Ray Xie
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, when I enabled the logging for hibernate generated SQL, I found that Hibernate only generates this:

update
C
set
A_ID=?
where
C_ID=?

Why won't Hibernate generate a delete statement for C instances that have been deleted?

Thanks,
Ray
 
Jerry You
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, Ray,

I have seen similar problems when dealing with one-to-many associations with hibernate. Regarding the deleting C objects from A object, how do you code it in your action class? If it is like below:

//some code to load A with Cs
(1)....
//create the new set from user input
Set newSet = new HashSet();
for every user chosen items
{
add to new set
}
(2)A.setC(newSet)
(3)save A

The problem with above code is, before (2), the set of C is acually a set of "proxy" objects of C, with persistence information. Line (2) eliminated the persistence info by resetting the set. Thus hibernate could not do the database work. So, we replaced "Set newSet = new HashSet()"
with "A.getCSet().clear()", which solved the "deleting" problem (although we had another problem because we had a unique index in C and Hibernate seems to like "insert-then-update-then-delete" and thus there was unique violation)


This is my two cents, although I don't think your case is exactly same as my experience.
 
Ray Xie
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jerry,

Yes, I think my situation is the same as yours. I did it slightly differently, I did it like this:

A.setC.retainAll(theModifiedSet);
A.setC.addAll(theModifiedSet);

The thing is that then it's not "clean" anymore, i.e.: you have to code each one-to-many differently--you can't do copyProperties to copy all the properties in one fell swoop for instance.

That's why I wonder because this is supposed to be a very common scenario, yet so difficult to find answers for this.

Thanks for replying! Cheers,
Ray

Originally posted by Jerry You:
Hey, Ray,

I have seen similar problems when dealing with one-to-many associations with hibernate. Regarding the deleting C objects from A object, how do you code it in your action class? If it is like below:

//some code to load A with Cs
(1)....
//create the new set from user input
Set newSet = new HashSet();
for every user chosen items
{
add to new set
}
(2)A.setC(newSet)
(3)save A

The problem with above code is, before (2), the set of C is acually a set of "proxy" objects of C, with persistence information. Line (2) eliminated the persistence info by resetting the set. Thus hibernate could not do the database work. So, we replaced "Set newSet = new HashSet()"
with "A.getCSet().clear()", which solved the "deleting" problem (although we had another problem because we had a unique index in C and Hibernate seems to like "insert-then-update-then-delete" and thus there was unique violation)


This is my two cents, although I don't think your case is exactly same as my experience.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic