Domanda

I'm new to hibernate and I'm having a problem in persist. In user class, it has first_name, last_name, gender, address, etc and I want only Address column field to ignore update to table when user's gender is only equals to Female. I googled and found out that I can use @Transient to ignore update but what I want is to ignore when certain condition is met and if not, I want to include Address in persist.

Is it possible to do in hibernate?

È stato utile?

Soluzione

Ignoring a field based of the value of another field in not the responsability of the Data Access Layer. It should be the responsibility of the Business Logic Layer.

When your condition is met, just erase the new address with a copy of the old one before persisting.

Altri suggerimenti

A possible way to do this would be to amend the setter for address field. like

public void setAddress(String address) { if(!this.gender.equals("female") { this.address = address; } }

So when you persist the User address value would not be there for female users.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top