8

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 ?

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
vaibhav bindroo
  • 149
  • 2
  • 3
  • 12
  • 2
    If your getter is `isActive()`, isn't your property name `active` then? – biziclop Jan 31 '11 at 13:27
  • 1
    @biziclop: no if you try it then you will see that getter is right. isActive() getter is created for isActive field. – Harry Joy Jan 31 '11 at 13:29
  • I think you're confusing property and instance field. The name of the property in your case is `active` , no matter by what instance field it's backed. It's even possible that there is no field backing your property, e.g. property `hours` (deprecated) in `java.util.Date`. – adamax Jan 31 '11 at 14:15

5 Answers5

12

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

fmucar
  • 14,361
  • 2
  • 45
  • 50
5

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.

Qwerky
  • 18,217
  • 6
  • 44
  • 80
0

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.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • this means that you're creating a bad workaround to create a property named "isActive". take a look at fatih's answer instead :) – davogotland Jan 31 '11 at 14:05
  • 1
    @davogotland - he *has* a property named `isActive` and doesn't want to change its name. That's how I understood the requirements. If he is willing to change property names: **Sure** - rename that beast to `active`. `isActive` is a getter name, not a field name ;-) – Andreas Dolk Jan 31 '11 at 14:08
  • haha, you're right. i didn't even see that. i just saw "isactive" and assumed that the property was actually called "active". – davogotland Jan 31 '11 at 14:15
0

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.

Tushar Tarkas
  • 1,592
  • 13
  • 25
0

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 !

vaibhav bindroo
  • 149
  • 2
  • 3
  • 12