Assume entity classes Foo and Bar like:
class Foo {
@ManyToMany
@JoinTable(name = "FooBar",
joinColumns = @JoinColumn(name = "foo"),
inverseJoinColumns = @JoinColumn(name = "bar"))
public List<Bar> getBars() {
return bars;
}
}
class Bar {
// ...
}
When I save a Foo entity, I want to save bars list also, but only the mapping list, for example:
Bar oldBar = foo.getBars().get(0);
oldBar.setSomething("new-value"); // Change to the entity.
Bar newbar = new Bar();
foo.getBars().add(newbar); // Change to the list.
saveOrUpdate(foo);
I want saveOrUpdate persist the changes to the list, but not changes to the entity. Can I?
CascadeType.PERSISTsort of does what you want, but it depends on the starting state of your foo object. If the old foo is in the managed state, then changes you make to oldBar are persistent. That can’t really be ‘turned off.’ Why would you change a value on a persistent object if it’s not supposed to get changed? It doesn’t really make sense in an ORM context.