I have two JPA objects, for example:
@Entity class Foo { @Id private long id; // ... } @Entity class Bar { @ElementCollection(targetClass = String.class, fetch = FetchType.LAZY) @MapKeyJoinColumn(name = "foo_id", referencedColumnName = "id") @MapKeyClass(Foo.class) @Column(name = "content") @CollectionTable(name = "bar_foo_content", joinColumns = @JoinColumn(name = "bar_id", referencedColumnName = "id")) @ManyToMany(cascade = CascadeType.ALL) private Map<Foo, String> fooContent = Maps.newHashMap(); // ... }
As you can see, the fooContent field forms a many-to-many relationship between Bar and Foo , so I thought it would be wise to use @ManyToMany to specify cascading for the field. However, when I try to save Bar with a pair of Foo β String values ββon the map, I get the following exception:
javax.persistence.RollbackException: java.lang.IllegalStateException: During synchronization a new object was found through a relationship that was not marked cascade PERSIST: <<instance of Foo>>
Obviously, EclipseLink does not cascade the persistence of my Foo instances. How should I comment on fooContent to work with cascading persistence?
java jpa eclipselink
dflemstr
source share