How to access entity properties through non-standard getters and setters with Hibernate


#1

I have a base class for many other classes. In this base class I have two methods:

getProperty(String propName)

which returns the property value whose name is passed through propName.

setProperty(String propName, Object value)

which sets the value of property whose name is passed through propName

I need hibernate to access the properties through these methods for all sub classes.

I came across few interfaces PropertyAccess, PropertyAccessStrategy, Getter, and Setter that seems to be the key for what I need.

I couldn’t find much documentation about them.

Can anyone help me out on how to implement them and to indicate to hibernate to use them.

Thanks in advance.


#2

Why would you want to do something like that? You are going to lose type safety, the calls will be slower because you use Reflection and this is not how you are supposed to map your entities.

Also, JPA inheritance is not for reusing properties, it’s for varying behavior as explained in this article.

Therefore, the best solution to your problem is to rethink your design and to use entities just as they are meant to be used.


#3

Thank you Vlad for you answer.
Our products are currently built over an in-house developed platform, and we are working on a multi-stage plan to migrate them to hibernate.
We need this for step 1, in order to guarantee a smooth migration.
Is there a way to introduce our PropertyAccessStrategy?
Thanks again.


#4

Try checking the PropertyAccessStrategy or customizining the EntityPersister or the Tuplizer.