setter for a boolean variable named like isActive

那年仲夏 提交于 2019-12-04 05:20:48

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

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.

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.

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.

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 !

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