I first asked this on SO, but I figured that here might be a better place to ask. The following is more or less copied from there.
The situation is as follows: I have a class with a Map like this:
@Entity
public class Bar {
@Id
@GeneratedValue
private Long id;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "fooMap",
joinColumns = @JoinColumn(name = "Bar", referencedColumnName = "ID") ,
inverseJoinColumns = @JoinColumn(name = "Two_ID", referencedColumnName = "ID") )
@MapKeyJoinColumn(name = "One_id")
private Map<FooOne, FooTwo> fooMap;
}
with key/value-classes like this (FooTwo looks more or less the same):
@Entity
public class FooOne {
@Id
@GeneratedValue
private Long id;
@Basic
private String name;
}
If I populate the map and try to persist it, I get the following exception:
Quote: UnitOfWork(117839394)--java.lang.IllegalStateException: During synchronization >a new object was found through a relationship that was not marked cascade >PERSIST: jpa.test.minimalExample.FooOne\@2e4389ed.
because it cascades only to the target of the -toMany association which is the value entity.
Does JPA in general or EclipseLink as implementation offer any form of annotation to cascade actions to the key class of this map? What are my options here ?
I know adding a reference to FooOne in the FooTwo-class works (like this):
@Entity
public class FooTwo {
@Id
@GeneratedValue
private Long id;
@OneToOne(cascade = cascadeType.ALL)
private FooOne foo1;
}
This way, my table FooTwo has an additional column with the fk from FooOne (that I actually don't need, as I already have the map). I would rather have the FooOne table created without the additional relationship.