使用RowMapper和JdbcTemplate得到了NullPointerException

时间:2016-05-11 14:55:09

标签: java mysql spring spring-data jdbctemplate

我有两个实体 - 用户和访问组。当在JdbcTemplate中使用RowMapper从MySQL数据库获取用户实体时,我有NullPointerException。在UserRowMapper中不使用setter for accessgroup时,我没有NPE,但在AccessGroup accessGroup中为null。

表格


      CREATE TABLE `users` (
      `USER_ID` int(11) NOT NULL AUTO_INCREMENT,
      `USER_EMAIL` varchar(255) DEFAULT NULL,
      `USER_NAME` varchar(255) DEFAULT NULL,
      `USER_PWD` varchar(255) DEFAULT NULL,
      `ACCESSGROUP_GROUP_ID` int(11) DEFAULT NULL,
      PRIMARY KEY (`USER_ID`),
      KEY `FK_users_ACCESSGROUP_GROUP_ID` (`ACCESSGROUP_GROUP_ID`),
      CONSTRAINT `FK_users_ACCESSGROUP_GROUP_ID` FOREIGN KEY (`ACCESSGROUP_GROUP_ID`) REFERENCES `access_group` (`GROUP_ID`)
    ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;


    CREATE TABLE `access_group` (
      `GROUP_ID` int(11) NOT NULL AUTO_INCREMENT,
      `GROUP_NAME` varchar(255) NOT NULL DEFAULT '',
      PRIMARY KEY (`GROUP_ID`)
    ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;

实体


    @Entity
    @Table(name = "users")
    public class User implements Serializable {

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Basic(optional = false)
        @Column(name = "USER_ID")
        private Integer userId;
        @Column(name = "USER_EMAIL")
        private String userEmail;
        @Column(name = "USER_NAME")
        private String userName;
        @Column(name = "USER_PWD")
        private String userPwd;
        @JoinColumn(name = "ACCESSGROUP_GROUP_ID", referencedColumnName = "GROUP_ID")
        @ManyToOne
        private AccessGroup accessGroup;

    

    @Entity
    @Table(name = "access_group")
    public class AccessGroup implements Serializable {

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Basic(optional = false)
        @Column(name = "GROUP_ID")
        private Integer groupId;
        @Basic(optional = false)
        @Column(name = "GROUP_NAME")
        private String groupName;
        @OneToMany(mappedBy = "accessGroup")
        private Set users;


    @Repository("userDao")
    public class UserDaoImpl implements IUserDao {

        @Autowired
        private JdbcTemplate jdbcTemplate;

        @Value("${sql.user.get.email.pwd}")
        private String getByEmailAndPwd; 

    //sql.user.get.email.pwd=SELECT * FROM users WHERE user_email = ? AND user_pwd = ?

        @Transactional
        @Override
        public User getUserByEmailAndPwd(String email, String password) {
            return jdbcTemplate.queryForObject(getByEmailAndPwd, new Object[]{email, password}, new UserRowMapper());
        }


    @Repository("accessGroupDao")
    public class AccessGroupDaoImpl implements IAccessGroupDao {

        private JdbcTemplate jdbcTemplate;

        @Autowired
        public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
            this.jdbcTemplate = jdbcTemplate;
        }

        @Value("${sql.accessgroup.get.id}")
        private String getAccessGroupById;
      //sql.accessgroup.get.id=SELECT * FROM access_group WHERE GROUP_ID = ?

        @Transactional
        @Override
        public AccessGroup getGroupById(int id) {
            return jdbcTemplate.queryForObject(getAccessGroupById, new Object[]{id}, new AccessGroupRowMapper());
        }

RowMappers


    @Component
    public class UserRowMapper implements RowMapper {

        private AccessGroupService accessGroupService;

        @Autowired
        public void setAccessGroupService(AccessGroupService accessGroupService) {
            this.accessGroupService = accessGroupService;
        }

        @Override
        public User mapRow(ResultSet resultSet, int i) throws SQLException {
            User user = new User();
            user.setId(resultSet.getInt("USER_ID"));
            user.setEmail(resultSet.getString("USER_EMAIL"));
            user.setName(resultSet.getString("USER_NAME"));
            user.setpwd(resultSet.getString("USER_PWD"));
            //when adding here user.setAccessGroup(accessGroupService.getGroupById(resultSet.getInt("ACCESSGROUP_GROUP_ID"))); I have NPE
            return user;
        }


    public class AccessGroupRowMapper implements RowMapper {

        @Override
        public AccessGroup mapRow(ResultSet resultSet, int i) throws SQLException {
            AccessGroup accessGroup = new AccessGroup();
            accessGroup.setId(resultSet.getInt("GROUP_ID"));
            accessGroup.setName(resultSet.getString("GROUP_NAME"));
            return accessGroup;
        }
    }

1 个答案:

答案 0 :(得分:0)

1)将以下属性添加到@Entity类“User”&产生吸气剂和setter方法:

@Transient
private Integer userIdTransientField; 

@Transient表示该属性不会存储在数据库中

2)修改RowMapper类:

用以下内容替换此行:

user.setId(resultSet.getInt("USER_ID")); -> user.setUserIdTransientField(resultSet.getInt("USER_ID"));

3)当您引用“用户”对象并且想要为jdbcTemplate执行更新/删除或只是想获取Id值时,请使用getUserIdTransientField方法。