0

I'm havings simple form for creating user, but I'm still having following error, I'm struggling with it for a really long time.

 Bean property 'user_id' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

org.springframework.beans.NotReadablePropertyException:

Here is my AdminController methods for managing user's input:

@RequestMapping(value = "/add", method = RequestMethod.GET)
public String add(Model model) {
    User user = new User();
    model.addAttribute("user", user);
    model.addAttribute("users", userService.listUsers());
    return "teams";
}

@RequestMapping(value = "/userSave", method = RequestMethod.POST)
public String saveUser(@RequestParam("user_id") int user_id, @Valid User user, BindingResult result, Model model) {

    userService.create(user.getId(), user.getUsername(), user.getPassword(), user.getFirst_name(),
            user.getLast_name(), user.getRole(), user.getEmail(), user.getStart_date(), user.getId_team());
    return "home";
}

My teams.jsp form looks like this:

<c:url var="userSaveUrl" value="/teams/userSave" />
<form:form action="${userSaveUrl}" method="post"
  modelAttribute="user" id="user">
   <div class="form-group">
    <form:label path="user_id">ID</form:label>
    <form:input path="user_id" class="form-control" />
  </div> 
  <form:hidden path="user_id" />
  <div class="form-group">
    <form:label path="first_name">First name</form:label>                 
                <form:input path="first_name" class="form-control" />
  </div>
  <div class="form-group">
    <form:label path="last_name">Last name</form:label>
    <form:input path="last_name" class="form-control" />
  </div>
  <div class="form-group">
    <form:label path="email">email</form:label>
    <form:input path="email" class="form-control" />
  </div>
  <div class="form-group">
    <form:label path="start_date">Start date</form:label>
    <form:input path="start_date" class="form-control" />
  </div>
  <div class="form-group">
    <form:label path="username">Username</form:label>                 
                    <form:input path="username" class="form-control" />
  </div>
  <div class="form-group">
    <form:label path="password">Password</form:label>
    <form:input type="password" path="password" class="form-control" />
  </div>
  <div class="form-group">
    <form:label path="role">Role</form:label>
    <form:input path="role" class="form-control" />               
    </div>              
    <div class="form-group">
    <form:label path="id_team">Team ID</form:label>
    <form:input path="id_team" class="form-control" />
  </div>

   <input name="saveBtn" value="Save" type="submit" class="btn btn-primary">
</form:form>

User.java is following:

@Entity
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int user_id;

    @NotNull
    @Column(nullable = false)
    private String first_name;

    @NotNull
    @Column(nullable = false)
    private String last_name;

    @NotNull
    @Column(nullable = false)

    private String email;
    @NotNull
    @Column(nullable = false)
    private Date start_date;

    @NotNull
    @Column(nullable = false)
    private String username;

    @NotNull
    @Column(nullable = false)
    private String password;

    @NotNull
    @Column(nullable = false)
    private String role;

    @Column(nullable = false)
    private Integer id_team;

    public User() {
        // no-args constructor required by JPA spec
    }

    public User(String username, String password, String first_name, String last_name, String role, String email,
            Date date) {
        // this.id = nextİd.incrementAndGet();
        this.setUsername(username);
        this.setPassword(password);
        this.setFirst_name(first_name);
        this.setLast_name(last_name);
        this.setRole(role);
        this.setEmail(email);
        this.setStart_date(date);

    }

    public User(int id, String username, String password, String first_name, String last_name, String role,
            String email, Date date) {
        // this.id = nextİd.incrementAndGet();
        // this.user_id = id;
        this.setId(id);
        this.setUsername(username);
        this.setPassword(password);
        this.setFirst_name(first_name);
        this.setLast_name(last_name);
        this.setRole(role);
        this.setEmail(email);
        this.setStart_date(date);
    }

    public User(int id, String username, String password, String first_name, String last_name, String role,
            String email, Date date, Integer id_team) {
        // this.id = nextİd.incrementAndGet();
        // this.user_id = id;
        this.setId(id);
        this.setUsername(username);
        this.setPassword(password);
        this.setFirst_name(first_name);
        this.setLast_name(last_name);
        this.setRole(role);
        this.setEmail(email);
        this.setStart_date(date);
        this.setId_team(id_team);
    }

    public void setId(int id) {
        this.user_id = id;
    }

    public int getId() {
        return user_id;
    }

    @Override
    public String toString() {
        int ID = this.getId();
        String user = "ID: " + Integer.toString(this.getId()) + " Role:" + this.getRole() + "  Username:"
                + this.getUsername() + " Username: " + this.getUsername() + " Email: " + this.getEmail();
        return user;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getFirst_name() {
        return first_name;
    }

    public void setFirst_name(String first_name) {
        this.first_name = first_name;
    }

    public String getLast_name() {
        return last_name;
    }

    public void setLast_name(String last_name) {
        this.last_name = last_name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Date getStart_date() {
        return start_date;
    }

    public void setStart_date(Date start_date) {
        this.start_date = start_date;
    }

    public Integer getId_team() {
        return id_team;
    }

    public void setId_team(Integer id_team) {
        this.id_team = id_team;
    }
}

And finally UserJDBCTemplate.java method for creating an entitny in DB:

@Override
public void create(int id, String username, String password, String first_name, String last_name, String role,
    String email, Date date, int id_team) {
    String SQL = "insert into Users (id_user, username, password,first_name,last_name,role,email,date,id_team) values (?, ?, ?,?,?,?,?,?,?)";
    jdbcTemplateObject.update(SQL, id, username, password, first_name, last_name, role, email, date, id_team);
}

I've tried everything, but I can't get to a problem.

1
  • What happens if you change the name of the setter and the getter to setUser_id and getUser_id, to match the name of the field? Commented Oct 21, 2015 at 17:56

1 Answer 1

1

Please auto generate your getters and setters through ide, for example I do see that, At User class the getter method is

public int getId() {
      return user_id;
  }

This will be,

public int getUser_id() {
      return user_id;
  }
Sign up to request clarification or add additional context in comments.

3 Comments

Almost 3 minutes before you posted this, Lenusska commented that this makes no difference.
In my previous comment, I just refactored it, I didn't generate it. But after generating, it's working.
Cool!! Please you your ide every time that makes simpler to code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.