名为 isActive 的布尔变量的 setter
我的 pojo 类中有一个名为 isActive 的属性。当我使用 Eclipse IDE 生成此属性的访问器时,它会生成以下 getter 和 setter
Getter : isActive()
Setter : setActive()
但是,当我尝试使用 ibatis 框架通过将属性名称提及为 "isActive" 来编写此属性时,它会提示无法找到任何名为的可写属性'处于活动状态'。我认为问题在于无法通过将 setter 推断为 setIsActive() 来推断出正确的属性名称。
在不更改属性名称或 getter 的情况下解决此问题的最佳方法是什么?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
原始布尔字段 getter 创建为
isFieldName
。因此,在 Ibatis 中,您应该将属性名称指定为active
而不是isActive
primitive boolean field getters are created as
isFieldName
. So in Ibatis you should give the property name asactive
notisActive
pojo 命名约定期望名为
xxx
的boolean
类型具有方法isXxx
和setXxx
。在你的情况下,你的 pojo 应该看起来像;
您可以通过在 IDE 中创建一个类并定义
private boolean active
变量来亲自演示这一点,然后让 IDE 生成 getter 和 setter。The pojo naming convention expects
boolean
types calledxxx
to have methodsisXxx
andsetXxx
.In your case your pojo should look like;
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.有一条出路。
访问 Windows ->首选项-> Java->代码样式并取消选择“使用'is'前缀...”属性(当然,如果您不希望将其作为 Eclipse 中的全局行为,则可以在项目属性上更改此属性)。
在我看来,这将使行为变得
丑陋,但 ibatis 现在应该停止抱怨。
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
Ugly to my eyes but ibatis should stop complaining now.
我没有使用过 iBatis,但是 Hibernate 允许指定访问方法名称。您可以在此处覆盖 ORM 的默认行为,以计算设置属性的方法名称。
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.
感谢您的回复。根据我不想更改 pojo 类成员变量的要求,我使用的 ibatis 版本无法按预期工作。当我将版本从 2.3.0 升级到 2.3.4 时,问题得到解决,并且相同的代码可以无缝运行。我假设通过这次升级,如果 boolean 原语类型的属性定义为 isActive,他们会考虑生成 isActive() 和 setIsActive() 访问器的 java beans 约定。谢谢 !
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 !