简体   繁体   中英

why does set and get method in java persistence model should have the same name?

I wonder why methods of get and set in java persistence model should have the same name after the word get and set. As like this:

getCompanyName(){
}

setCompanyName(){
}

And I think this is not just in model, also in other configuration of get and set. Can anyone explain why? I really want to know.. Thanks in advance :)

Not necessarily.

Docs just recommending that format:

This class(bean) uses standard JavaBean naming conventions for property setter and setter methods, as well as private visibility for the fields. Although this is the recommended design, it is not required. Hibernate can also access fields directly, the benefit of accessor methods is robustness for re factoring.

My possible reason's to follow that format:

1)First of all Readability of your code.

2)Secondly,no one provide a name getCompanyName() to get username :)

Few things to note:

  • A hibernate persistent entity is POJO.
  • Set/Get method of a POJO enables a framework (like hibernate) to introspect the property of the POJO provided it follows a Javabean Convention .
  • In absence of any standard convention, it will be difficult to create generalized frameworks which can read the properties of a POJO.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM