I have a entity call Company, and another entity call token.
1 Company will have many tokens.
So in my Company entity, I will have something like follow:
@OneToMany(mappedBy = "companyId")
public Set< Token > getTokens() {
return tokens;
}
However, I will have some logic during the return, it will change the token list before return, its something like follow:
@OneToMany(mappedBy = "companyId")
public Set< Token > getTokens() {
tokens.remove( token );
return tokens;
}
Because of I changing the value in token list, thus, every time I select the company object from db using hibernate, the company table will be updated automatically.
Base on my understanding, this is a behavior of hibernate dirty checking. So when Hibernate detect that there something changes on it, it will do the update to database.
Is there any way that can avoid this? For example maybe just call existing hibernate function, so that hibernate will know the token list is dirty, and it will no do the update.
Yes, I know the filtering logic in the getTokens() is not suitable. By right filtering logic should not apply in entity level. But because of currently there is many place using this method, if I change in this entity level, then there will impact others and if I change other place as well, it will need to retest whole application again.
Thus I am trying to find a better way on this.