What cause org.hibernate.PropertyAccessException: Exception occurred inside setter What cause org.hibernate.PropertyAccessException: Exception occurred inside setter spring spring

What cause org.hibernate.PropertyAccessException: Exception occurred inside setter


My guess is that Hibernate calls your setter with its own implementation of Set (PersistentSet) which implements lazy-loading, and is not initialized yet when the setter is called. Since you call a method on this set, it makes the set load itself while already being in a loading phase, which puts Hibernate in an inconsistent state.

That's why I prefer using field access over property access (i.e. put all the mapping annotations on fields rather than getters). You want to make a copy of the passed set when the method is called by "normal" code, but you don't want to do that when Hibernate itself calls the setter: it completely break lazy-loading.


From javadoc:

A problem occurred accessing a property of an instance of a persistent class by reflection, or via CGLIB. There are a number of possible underlying causes, including

- failure of a security check- an exception occurring inside the getter or setter method- a nullable database column was mapped to a primitive-type property- the Hibernate type was not castable to the property type (or vice-versa)

I would say, this.tags(field) or tags(parameter) is null, but in order to figure it out, you can print the entire stack-trace, tis would point the root cause.


You are passing null as argument of method, so for-each construct throws an exception. Use a guard condition and everything goes ok:

@ManyToMany(fetch = FetchType.EAGER)public void setTags(Set<Tags> tags) {    this.tags.clear();    if (tags != null) {        for (Tag tag : tags) {            addTag(tag);        }    }}

but I prefer:

@ManyToMany(fetch = FetchType.EAGER)public void setTags(Set<Tags> tags) {    this.tags = tags;}