为什么“公开”?不允许JPA实体中的属性? [英] Why is "public" not allowed for properties in JPA entities?

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

问题描述

JPA 2.0规范在第22页上说明:


类的实例变量必须是
私有,受保护或包
可见性,与
字段无关访问或财产访问是使用
。使用属性访问时,
属性访问器方法必须是
public或protected。

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.

为什么不是'是否允许公共访问?

Why isn't public access allowed?

推荐答案

对于公共字段,代理无法可靠地工作 - 如果有人访问直接字段,然后持久化框架没有简单的方法来拦截该调用,并且(例如)初始化包含对象(如果它是代理)。

With public fields, there would be no way for proxying to work reliably -- if someone accesses a field directly, then there is no simple way for the persistence framework to intercept that call and (say) initialize the containing object, if it's a proxy.

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

If field access is not possible, synthetic getter methods can be generated on the proxy object to intercept the method calls and do any required 'behind-the-scenes' work.

这篇关于为什么“公开”?不允许JPA实体中的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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