Hibernate version: 3.2.5.ga
I have posted on this before. I got no replies, 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 between them 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, to represent the JOIN. Here's what it all looks like:
Code:
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 delete the info in the AAndBJoin table first before inserting, as I see it does (using log4j to debug the generated SQL) when using a 'normal' ManyToMany relationship, ie. a JOIN table without attributes and as such no need to create a class to represent the JOIN table, as is my case.
I have also looked at the Category / Item / CategorizedItem code in the CaveatEmptor app but it doesn't do the trick either.
I would really appreciate some help on this one!
Bob