简体   繁体   English

为什么JPA中的实体类不能是最终的?

[英]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 declared final . 没有方法或持久化实例变量必须声明为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.

相关问题 为什么不能将JPA实体类及其方法声明为final? - Why JPA entity class and its methods must not be declared final? 为什么final类不能被继承,而final方法可以被继承? - Why can't a final class be inherited, but a final method can be inherited? 为什么我们不能嘲笑最后一堂课? - Why can't we mock a final class? 为什么 Java 类不能既是抽象类又是最终类 - Why can't a Java class be both abstract and final 为什么@SafeVarags无法应用于最终类中的实例方法? - Why can't @SafeVarags be applied to instance methods in a final class? 无法使用JPA删除实体 - Can't remove entity with JPA 为什么spring数据JPA中的关系属性不能也final呢? - Why can't we make a relationship attribute in spring data JPA final as well? 为什么我不能使用TYPE-WHERE子句查询与JPA 2.1中的实体类型匹配的实体? - Why can't I query an entity with a TYPE-WHERE clause matching the entity type in JPA 2.1? 为什么有一个带有实体名称的类加上 JPA​​ 实体类的下划线 - Why is there a class with entity name plus an underscore for JPA entity class 我们可以将JPA静态元模型类的成员变量声明为final吗? - Can we declare member variable of JPA static metamodel class as a final?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM