2

I have a standard MySql DB with a User Table. In this table there is a TINYINT(4) named "isApproved"

For this user it is set to "1" (which i believe is true) My bean property looks like this:

private boolean isApproved;

and the appropriate getters/setters:

public boolean isApproved() {
    return isApproved;
}
public void setApproved(boolean isApproved) {
    this.isApproved = isApproved;
}

I have a jdbcTemplate that is pulling the correct user and it looks like this:

public UserBean findUserByUserName(String userName) {
    String sql = "SELECT * FROM User WHERE name = ?";
    return (UserBean)getJdbcTemplate().queryForObject(sql, new BeanPropertyRowMapper<UserBean>(UserBean.class), userName);
}

So I think I may have made a simple mistake and I add this to the logging:

logger.debug("User from DB isApproved:"+userFromDb.isApproved());

This is coming back "false", even when there is a "1" in the DB.

How do I make this mapping correct, the isApproved is "1" which should equate to true as a boolean.

mmaceachran
  • 3,178
  • 7
  • 53
  • 102
  • Not sure but I believe the issue happened to me. change isApproved to to approved and the method to getApproved(). The issue as I remember that the JDCB engine will look for set+variable name in your case. setIsApproved() so it will not respect java naming convestion for boolean. So just change the variable name to normal name with normal set and get. – Mohd Alomar Apr 06 '18 at 23:45
  • @MohdAlomar The name `isXxx` is appropriate for a *boolean* getter method. It's part of the bean naming standard. And it's not the JDBC engine doing this. It's the Spring `BeanPropertyRowMapper` class. – Andreas Apr 06 '18 at 23:52
  • @Andreas not sure but the issue happened to me using jersey, hibernate and entity. – Mohd Alomar Apr 06 '18 at 23:57
  • @MohdAlomar This question is about Spring Framework code using `JdbcTemplate` and `BeanPropertyRowMapper`. Just JDBC and Spring. No Jersey. No Hibernate. No JPA. So what does what you said have to do with this question, where `isApproved` is a perfectly fine name *(once OP using column alias to fix naming issue)*? Telling OP not to use `isApproved` is misleading. – Andreas Apr 07 '18 at 00:02

1 Answers1

7

Bean naming mean that you take the column name, then call the setter method setColumnName, and the getter method getColumnName, except for boolean where it is named isColumnName.

Your column is named isApproved, so standard bean name would be setIsApproved and isIsApproved. See the problem?

As the javadoc of BeanPropertyRowMapper says:

To facilitate mapping between columns and fields that don't have matching names, try using column aliases in the SQL statement like:

select fname as first_name from customer

This means you can't use SELECT *, but that's ok, because you should never use SELECT * in code. See Why is SELECT * considered harmful?

The * is convenience for ad-hoc queries, or queries for dynamic data where you use the meta-data to analyze the result columns. Since you need to map to a specific class, you need to select the columns that need to map.

Community
  • 1
  • 1
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • That extra is did it. I do however disagree with the select * when pulling ONE bean and using a mapper. When doing joins and such I never do that, but that's another discussion. Thanks for your help. – mmaceachran Apr 07 '18 at 00:25