我是 Hibernate 的新手。我正在浏览一些教程,我发现如果我们在 hibernate 配置文件中添加模型类,我们仍然需要在模型类中添加 @Entity
注释。
为什么会这样?
最佳答案
@Entity
是一个 JPA 注释。来自JPA specification , 第 2.1 节:
The entity class must be annotated with the Entity annotation or denoted in the XML descriptor as an entity.
因此,如果您对映射使用注解,@Entity
是规范强制要求的,Hibernate 必须遵守它。
JPA 设计者能否将其设为可选?可能是的,但是总是将它添加到实体类中以明确指定一个类是一个实体,从而使代码的 future 读者更容易立即看到该类的用途,这确实不需要花费太多精力。
关于java - 为什么在配置文件中定义类时需要@Entity注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35345740/