tags:

views:

50

answers:

1

Why can't an entity class in JPA be final or have a final methods? Citing from here -

An entity class must follow these requirements:

...

The class must not be declared final. No methods or persistent instance variables must be declared final.

What is the reason? Is JPA subclassing the entity classes and redefining the methods?

+4  A: 

Nice question,

By definition and entity is a class that can be persisted to a db, so having a final field would make no sense in the context of something that will end up being a record in your db. The requirement of final class and no final methods has to do with the way in which JPA implementations actually deal with persisting instances of your entities classes.

It is common for implementations to subclass your class at runtime and/or add byte code dynamically to your classes, in order to detect when a change to an entity object has occurred. If your class is declared as final, then that would not be possible.

There is of course a lot more reasons than just those, here is an article that gives more information of how ORM in general work behind the covers, that might help you better understand the other requirements for JPA entities

StudiousJoseph
Thanks! I knew about the final fields, but I wasn't sure about the final methods/classes.
Karel Bílek
You're welcome ;)
StudiousJoseph
Note that *loading* immutable classes from a database does perfectly make sense.
Pascal Thivent