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.
Java EE
Last Comment
mccarl
8/22/2022 - Mon
mccarl
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)
Software Programmer
ASKER
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.
mccarl
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) ?
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.
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
mccarl
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.
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)