[英]Why can't entity class in JPA be final?
Why can't an entity class in JPA be final or have a final methods? 为什么JPA中的实体类不能是最终的或者有最终方法? Citing from here -
引用这里 -
An entity class must follow these requirements:
实体类必须遵循以下要求:
...
...
The class must not be declared
final
.不得将该类声明为
final
。 No methods or persistent instance variables must be declaredfinal
.没有方法或持久化实例变量必须声明为
final
。
What is the reason? 是什么原因? Is JPA subclassing the entity classes and redefining the methods?
JPA是否对实体类进行了子类化并重新定义了方法?
By definition, an entity is a class that can be persisted to a database, so having a final field would make no sense in the context of something that will end up being a record in your database. 根据定义,实体是一个可以持久化到数据库的类,因此在最终成为数据库记录的上下文中使用final字段是没有意义的。 The requirement of no 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.
没有最终类和最终方法的要求与JPA实现实际处理实体类的持久实例的方式有关。
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.
如果你的类被宣布为final,那就不可能了。
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. 当然有更多的原因而不仅仅是那些,这里有一篇文章提供了有关ORM一般在幕后工作的更多信息,这可能有助于您更好地理解JPA实体的其他要求。
Because persistence providers make proxies of objects. 因为持久性提供程序可以创建对象的代理。 These proxies are creating runtime subclasses of the entities/classes and also might be adding some flags to identify whether there is a change in the data.
这些代理正在创建实体/类的运行时子类,也可能添加一些标志来标识数据是否有变化。 so that is why your class should not be final to have the ability to change it.
所以这就是为什么你的班级不应该是最终有能力改变它的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.