问题描述
此问题具有一定的相关http://stackoverflow.com/questions/305880/hibernate-annotation-placement-question.
This question is somewhat related to http://stackoverflow.com/questions/305880/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?
推荐答案
我preFER访问,因为我可以添加一些业务逻辑来访问我,每当我需要的。
这里有一个例子:
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转换lib或BeanMapper或推土机或其他豆类映射/克隆的基础上的getter / setter属性LIB)你必须保证该库在同步与持久性管理器(两者都使用的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注解 - 这是更好的,字段或属性的访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!