setter for a boolean variable named like isActive

南楼画角 提交于 2019-12-06 00:18:41

问题


I have a property called isActive in my pojo class. When I generated the accessors for this property using Eclipse IDE, it generates following getters and setters

Getter : isActive()
Setter : setActive()

However, when I try to write this property using ibatis framework by mentioning property name as "isActive" , it cribs about not able to find any WRITEABLE propery named 'isActive'. The problem I think lies with not able to deduce the correct property name by inferring setter as setIsActive().

What is the best way to go about this without changing the property name or getter ?


回答1:


primitive boolean field getters are created as isFieldName. So in Ibatis you should give the property name as active not isActive




回答2:


The pojo naming convention expects boolean types called xxx to have methods isXxx and setXxx.

In your case your pojo should look like;

public class MyPojo
{
  private boolean active;

  public boolean isActive()
  {
    return active;
  }

  public void setActive(boolean active)
  {
    this.active = active;
  }
}

You can demonstrate this yourself by creating a class in your IDE and defining the private boolean active variable, and then getting the IDE to generate getters and setters.




回答3:


There's a way out.

Visit Windows -> Preferences -> Java -> Code Style and deselect the "Use 'is' prefix..." property (of course you can change this on project properties if you don't want this as a global behaviour in eclipse).

This will change the behaviour to

Getter : getIsActive()
Setter : setIsActive()

Ugly to my eyes but ibatis should stop complaining now.




回答4:


I have not used iBatis, but Hibernate allows you to specify the access method name. This is where you can override the default behavior of ORMs to compute method name for setting property.




回答5:


Thanks for the responses. Going by the requirements I had that I didn't wish to change my pojo class member variables, ibatis version that I was using wasn't working as expected. When I upgraded my version to 2.3.4 from 2.3.0 , the issue was resolved and same code worked seamlessly. I assume with this upgrade, they factored in the java beans convention of generating isActive() and setIsActive() accessors if property of type boolean primitive is defined as isActive. Thanks !



来源:https://stackoverflow.com/questions/4851337/setter-for-a-boolean-variable-named-like-isactive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!