Does Hibernate always need a setter when there is a getter?

As others have mentioned, if you annotate a property getter method, then Hibernate uses the setter when reading values from the database. Basically, Hibernate assumes that anything that it is writing to the database will eventually need to be read from the database. This implies that if you annotate a getter, then it needs to call a setter when reading the object from the database.

You can make the setter private (Hibernate will use reflection to access the setter). This is great way to preserve the contract of your class while still using Hibernate for relational mapping.

If the field is derived from other properties in the class, then why are you storing it in the database? You can use the @Transient annotation to mark the field that it shouldn't be stored in the database. You can even use the @Formula annotation to have Hibernate derive the field for you (it does this by using the formula in the query it sends to the database).


You should annotate your classes with the @Entity(access = AccessType.FIELD) and annotate your attributes. This should solve your problem. The setter is the best way to support refactoring. And what's the problem with having the little setter there.