为什么JPA中的实体类不能定型? [英] Why can't entity class in JPA be final?

查看:147
本文介绍了为什么JPA中的实体类不能定型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么JPA中的实体类不能为final或具有final方法?从此处-

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:

...

不得将类声明为final.不必声明任何方法或持久性实例变量.final.

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

原因是什么? JPA是否在继承实体类并重新定义方法?

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

推荐答案

按定义,实体是可以持久保存到数据库中的类,因此在最终内容中,使用final字段将毫无意义成为您数据库中的记录.没有最终类和最终方法的要求与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. 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.

实现通常会在运行时对类进行子类化和/或将字节码动态添加到类中,以检测何时对实体对象进行了更改.如果您的课程被宣布为最终课程,那将是不可能的.

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.

当然还有很多原因,这是一个文章,提供了有关ORM常规工作原理的更多信息,可以帮助您更好地了解JPA实体的其他要求.

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.

这篇关于为什么JPA中的实体类不能定型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆