I have a table with 2 main columns: name and name without accent.
I would like hibernate to update the 2nd one when 1st one is changed:
@Column(name = "name")
public String getName() {
return this.name;
}
public void setName(String s) {
this.name = s;
this.noAccentName = RemoveDiacritics(name); //remove diacritics from name
}
@Column(name = "noaccent_name")
public String getNoAccentName() {
return this.noAccentName;
}
public void setNoAccentName(String s) {
this.noAccentName = s;
}
This is working but it has an overhead: setter of the name sets object to dirty, and hibernate wants to update it all the time… 🙁
How can I ask hibernate to update ‘noAccentName’ when ‘name’ was changed in a clean way?
Thanks a lot.
You found the Interceptor, but as of Hibernate 4 the recommended way of doing things like this is to use Callback methods. In fact, I can’t find interceptors in Hibernate 4 at all, so you might not have an upgrade path if you go that way.
I believe what you want to do is this:
Hibernate will then take responsibility for updating the noAccentName just before saving the object. Note that I also made the setNoAccentName method private, as there is no way to convert from noAccentName to name and get the accents right.