Hibernate Annotations - Which is better, field or property access?(Hibernate Annotations - 哪个更好,字段访问还是属性访问?)
问题描述
这个问题与Hibernate Annotation Placement Question有些相关.
但我想知道哪个更好?通过属性访问还是通过字段访问?各有什么优缺点?
But I want to know which is better? Access via properties or access via fields? What are the advantages and disadvantages of each?
推荐答案
我更喜欢访问器,因为我可以在需要时向访问器添加一些业务逻辑.这是一个例子:
I prefer accessors, since I can add some business logic to my accessors whenever I need. Here's an example:
@Entity
public class Person {
@Column("nickName")
public String getNickName(){
if(this.name != null) return generateFunnyNick(this.name);
else return "John Doe";
}
}
此外,如果您将其他库放入混合中(例如一些 JSON 转换库或 BeanMapper 或 Dozer 或其他基于 getter/setter 属性的 bean 映射/克隆库),您将保证该库是同步的使用持久性管理器(都使用 getter/setter).
Besides, if you throw another libs into the mix (like some JSON-converting lib or BeanMapper or Dozer or other bean mapping/cloning lib based on getter/setter properties) you'll have the guarantee that the lib is in sync with the persistence manager (both use the getter/setter).
这篇关于Hibernate Annotations - 哪个更好,字段访问还是属性访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Hibernate Annotations - 哪个更好,字段访问还是属性访问?


基础教程推荐
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01