The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

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?