gpt4 book ai didi

java - 为什么 JPA 实体中的属性不允许使用 "public"?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:51:59 25 4
gpt4 key购买 nike

JPA 2.0 specification第 22 页说:

The instance variables of a class must be private, protected, or package visibility independent of whether field access or property access is used. When property access is used, the property accessor methods must be public or protected.

为什么不允许公开访问?

最佳答案

对于公共(public)字段,代理将无法可靠地工作——如果有人直接访问一个字段,那么持久性框架就没有简单的方法来拦截该调用并(比如)初始化包含对象,如果这是一个代理。

如果无法访问字段,则可以在代理对象上生成合成的 getter 方法以拦截方法调用并执行任何所需的“幕后”工作。

关于java - 为什么 JPA 实体中的属性不允许使用 "public"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2040836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com