olamattsson (7) [Avatar] Offline
Hi, on page 74 it is discouraged to "change the identity" of a collection when you accept it as a parameter in a setter method or return it from a getter method because Hibernate will be forced to perform an unnecessary SQL Update.
Isn't it possible to get around this limitation by letting Hibernate access the collection field directly, and completely bypass the accessor methods?
Simple example:
Code starts
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Blaha {

* Hibernate accesses this field directly.
private List whatEver;

* @return a read only copy of whatEver
public List getWhatEver() {
return Collections.unmodifiableList(whatEver);

* @param whatEver
public void setWhatEver(List whatEver) {
if (this.whatEver == null){
//A defensive copy
this.whatEver = new ArrayList(whatEver);
} else {
this.whatEver = whatEver;

private void performSanityCheck(List whatEver) throws RuntimeException {
//Check for insane values in the param
Code ends
In this example there is no way (except reflection of course) for another object to get hold of a reference to the internal whatEver field, a kind of defensive/paranoid programming that can be useful sometimes. But will it work well with Hibernate?
The whatEver field never changes it's identity once it has been initialized so if Hibernate uses direct field access to whatEver it shouldn't be concerned about how the parameter to the setter method or the return value from the getter method have different identities then the whatEver field, or am I wrong?