Naming Boolean variable in Spring MVC with respect to Database

We have a boolean column and we defined it as isActive.

The ajax request is success. However unable to bind the variable and exception is thrown

getter name is isActive() and setter name is setIsActive()

1. We don't want to rename the column as isActive to active
2. What should be the good setter and getter when the column is isActive ?
3. What should be the good setter and getter when the column is active ?

Thanks.
Software ProgrammerAsked:
Who is Participating?
 
mccarlConnect With a Mentor IT Business Systems Analyst / Software DeveloperCommented:
"just RowMapper" doesn't do ANY binding and so no, there isn't any specification. It is all up to your manual code, eg.


jdbcTemplate.query("select name, isActive from someTable", new RowMapper<SomePojo>() {
    public SomePojo mapRow(ResultSet rs, int rowNum) throws SQLException {
        SomePojo pojo = new SomePojo();
        pojo.setName(rs.getString(1));
        pojo.setActive(rs.getBoolean(2));
        return pojo;
    }
});

Open in new window


You can see even here, that there doesn't have to be a corelation between column name and pojo field names. In this case, the "isActive" column is the 2nd column selected in the SQL, and so you call ".setActive()" passing the value of the 2nd column. The column name or the setter method of the POJO could be entirely different.
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
What are you mapping with? Hibernate? something else?

As for typical ways of naming getters/setters, if you had a field called "active", you would have "isActive()" and "setActive(...)". If you had to have a field called "isActive", you would have "isIsActive()" and "setIsActive(...)". (Which sounds a bit funny, which is why you should have the first version, and then if you can't change the column name, you set up your mapping to map "isActive" column to "active" field)
0
 
Software ProgrammerAuthor Commented:
We don't use Hibernate. Instead using native SQL queries via Spring JDBCTemplate. I agree isIsActive()  doesn't seems to be good. But the question is

1. why isActive is not recognizable for isActive column ???
2. does the variable name matters in binding the object or the method name??

Some says specification says that...But i don't have clarity. can u please share your insights on the above two.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
We don't use Hibernate. Instead using native SQL queries via Spring JDBCTemplate

So, are you using code similar to what I gave you in another question of yours? Where I showed how to use the NamedParameterJdbcTemplate?

If so, then I explained in that question that it doesn't matter one bit what your column names are compared to your bean names. You can have a column named "Fred" and a Java POJO with a field named "Bob" and you can write the SQL query to map between the two.

In this case, you can keep you column name "isActive" and just write the SQL properly so that it maps to the "active" field in your POJO. I believe if you are using the BeanPropertySqlParameterSource that I showed you, then the getters/setters HAVE to be correctly named, ie. if you refer to a SQL parameter as    ":active"   in the SQL, then the field should be called active, the getter called isActive, and the setter called setActive.


As to your questions, they are not very clear, if the above hasn't cleared up your understanding, can you please try and restate the questions (preferrably with some code to demonstrate) ?
0
 
Software ProgrammerAuthor Commented:
I agree with what you are saying. We can close this if you could give some details on the java specification if any on the Boolean when we don't use the bean property mapper. instead binding Java Objects with just RowMapper.
0
 
Software ProgrammerAuthor Commented:
We got exception the type cannot be binded. When the column name is isActive in the database and we have the method name as isActive
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
You need to copy/paste the exact Exception that you are getting and post it here, include the full thing, stack trace and all. And the exact code that you are using.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.