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

JavaHibernateGetter Setter

Java Problem Overview


We have some Hibernate getter methods annotated with both @Column and @Basic.

We get an exception if we don't have the corresponding setter. Why is this?

In our case we are deriving the value returned from the getter (to get stored in the DB) and the setter has no functional purpose. So we just have an empty method to get around the error condition..

Java Solutions


Solution 1 - Java

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).

Solution 2 - Java

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.

Solution 3 - Java

Set access="field" if you do not want to use setters:

<class name="com.demo.hibernate.Country" table="country">
  <id name="countryId" column="id" type="int">
    <generator class="increment" />
  </id>
  <property name="name" column="name" access="field" type="string" />
  <property name="countryCode" column="country_code" access="field" type="string" />
</class>

Solution 4 - Java

Hibernate uses set method to initialize the entity which you're reading from your DB.

Maybe, if you make access modifiers of entity fields default or protected or public then Hibernate will initialize fields directly without using setter (I read something about it but I'm not sure that it works). But using setter is much more preferred way.

Solution 5 - Java

If you don't use setters and use private attributes, Hibernate would have to retrieve Fields by reflection and do field.setAccessible(true). I don't think Hibernate does that.

I don't really know if we can tell to Hibernate to do that, but as far as i remember, default config is using setters... Put a log/sysout on a set and you'll see that it uses the setter.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarcus LeonView Question on Stackoverflow
Solution 1 - JavaJim HurneView Answer on Stackoverflow
Solution 2 - JavakhmarbaiseView Answer on Stackoverflow
Solution 3 - JavavsinghView Answer on Stackoverflow
Solution 4 - JavaRomanView Answer on Stackoverflow
Solution 5 - JavaSebastien LorberView Answer on Stackoverflow